@@ -196,6 +196,170 @@ export AWS_BACKUP_ENABLE_LONG_RUNNING_TESTS=false
196
196
- Test audit framework compliance
197
197
- Validate organization policy enforcement
198
198
199
+ ## Pre-commit Configuration & Automation
200
+
201
+ ### Automated Code Quality with GitHub Actions
202
+
203
+ This module includes a comprehensive pre-commit GitHub Actions workflow (` .github/workflows/pre-commit.yml ` ) that automatically validates code quality and formatting. The workflow runs on:
204
+
205
+ - ** Pull requests** targeting the master branch with changes to ` .tf ` , ` .tfvars ` , ` .md ` , or ` .pre-commit-config.yaml ` files
206
+ - ** Pushes** to the master branch with changes to the same file types
207
+
208
+ #### Pre-commit Workflow Features
209
+
210
+ ** Automated Tools & Checks:**
211
+ - 🔧 ** Terraform formatting** (` terraform fmt ` )
212
+ - ✅ ** Terraform validation** (` terraform validate ` )
213
+ - 📚 ** Documentation generation** (` terraform-docs ` )
214
+ - 🔍 ** TFLint analysis** for best practices and errors
215
+ - 🧹 ** File formatting** (trailing whitespace, end-of-file fixes)
216
+ - 📋 ** YAML validation** for configuration files
217
+
218
+ ** Performance Optimizations:**
219
+ - ** Smart caching** of terraform-docs and tflint binaries
220
+ - ** Pre-commit hook caching** for faster subsequent runs
221
+ - ** Incremental checking** on pull requests (only changed files)
222
+ - ** Full validation** on master branch pushes
223
+ - ** 15-minute timeout** to prevent hung jobs
224
+
225
+ ** Workflow Intelligence:**
226
+ - ** Changed file detection** - Only runs pre-commit on relevant changed files in PRs
227
+ - ** Comprehensive summary** - Provides detailed results in GitHub Actions summary
228
+ - ** Tool installation verification** - Automatically installs and caches required tools
229
+ - ** Cross-platform compatibility** - Optimized for Ubuntu runners
230
+
231
+ #### Local Pre-commit Setup
232
+
233
+ ** Install pre-commit locally for development:**
234
+
235
+ ``` bash
236
+ # Install pre-commit (requires Python)
237
+ pip install pre-commit
238
+
239
+ # Install pre-commit hooks for this repository
240
+ pre-commit install
241
+
242
+ # Run pre-commit on all files manually
243
+ pre-commit run --all-files
244
+
245
+ # Run pre-commit on specific files
246
+ pre-commit run --files main.tf variables.tf
247
+ ```
248
+
249
+ ** Required Tools for Local Development:**
250
+ ``` bash
251
+ # Terraform (version 1.3.0+ recommended)
252
+ terraform --version
253
+
254
+ # terraform-docs for README generation
255
+ curl -sSLo ./terraform-docs.tar.gz https://terraform-docs.io/dl/v0.16.0/terraform-docs-v0.16.0-$( uname) -amd64.tar.gz
256
+ tar -xzf terraform-docs.tar.gz
257
+ sudo mv terraform-docs /usr/local/bin/
258
+
259
+ # TFLint for Terraform linting
260
+ curl -s https://raw.githubusercontent.com/terraform-linters/tflint/master/install_linux.sh | bash
261
+ ```
262
+
263
+ #### Pre-commit Configuration
264
+
265
+ The module uses ` .pre-commit-config.yaml ` with the following hooks:
266
+
267
+ ** Basic File Quality:**
268
+ - ` trailing-whitespace ` - Remove trailing whitespace
269
+ - ` end-of-file-fixer ` - Ensure files end with newline
270
+ - ` check-yaml ` - Validate YAML syntax
271
+
272
+ ** Terraform Quality:**
273
+ - ` terraform_fmt ` - Format Terraform files
274
+ - ` terraform_validate ` - Validate Terraform syntax and logic
275
+ - ` terraform_docs ` - Generate documentation
276
+ - ` terraform_tflint ` - Advanced Terraform linting
277
+
278
+ #### CI/CD Integration Benefits
279
+
280
+ ** Pull Request Automation:**
281
+ - ** Instant feedback** on code quality issues
282
+ - ** Prevents merge** of poorly formatted code
283
+ - ** Reduces review time** by catching common issues
284
+ - ** Maintains consistency** across contributors
285
+
286
+ ** Master Branch Protection:**
287
+ - ** Comprehensive validation** on all files after merge
288
+ - ** Documentation updates** automatically generated
289
+ - ** Quality gate** for production code
290
+
291
+ ** Development Experience:**
292
+ - ** Fast feedback loop** with incremental checking
293
+ - ** Clear error messages** with actionable guidance
294
+ - ** Automated fixes** for many formatting issues
295
+ - ** Consistent development environment** across team
296
+
297
+ ### Pre-commit Best Practices
298
+
299
+ #### Local Development Workflow
300
+ ``` bash
301
+ # Before committing changes
302
+ git add .
303
+ pre-commit run --files $( git diff --cached --name-only)
304
+
305
+ # If pre-commit fixes issues, add them and commit
306
+ git add .
307
+ git commit -m " feat: add backup vault lock configuration"
308
+ ```
309
+
310
+ #### Troubleshooting Pre-commit Issues
311
+
312
+ ** Common Issues & Solutions:**
313
+
314
+ ** Terraform Formatting Errors:**
315
+ ``` bash
316
+ # Fix formatting automatically
317
+ terraform fmt -recursive .
318
+
319
+ # Check specific file
320
+ terraform fmt -check main.tf
321
+ ```
322
+
323
+ ** Documentation Generation Errors:**
324
+ ``` bash
325
+ # Regenerate documentation
326
+ terraform-docs markdown table . > README.md
327
+
328
+ # Check terraform-docs configuration
329
+ terraform-docs --version
330
+ ```
331
+
332
+ ** TFLint Errors:**
333
+ ``` bash
334
+ # Run TFLint locally to see detailed errors
335
+ tflint
336
+
337
+ # Initialize TFLint if needed
338
+ tflint --init
339
+ ```
340
+
341
+ ** Pre-commit Hook Installation Issues:**
342
+ ``` bash
343
+ # Reinstall pre-commit hooks
344
+ pre-commit uninstall
345
+ pre-commit install
346
+
347
+ # Clear pre-commit cache if needed
348
+ pre-commit clean
349
+ ```
350
+
351
+ #### Performance Considerations
352
+
353
+ ** Large Repositories:**
354
+ - Pre-commit runs only on changed files in PRs (faster feedback)
355
+ - Tool binaries are cached between runs
356
+ - Pre-commit hooks are cached based on configuration hash
357
+
358
+ ** Network Issues:**
359
+ - Tools are installed once and cached
360
+ - Fallback installation methods for corporate networks
361
+ - Offline capability after initial tool installation
362
+
199
363
## Security Considerations
200
364
201
365
### AWS Backup-Specific Security Practices
0 commit comments