pre-commit is a framework that automatically runs code quality checks before Git commits. The cloud-sandbox project uses it to validate Terragrunt configuration files.
# macOS
brew install pre-commit
# Or using pip (all OS)
pip install pre-commit# macOS
brew install terragrunt
# Or individually:
# - Terragrunt: https://terragrunt.gruntwork.io/docs/getting-started/install/cd cloud-sandbox
chmod +x .pre-commit-setup.sh
./.pre-commit-setup.shcd cloud-sandbox
pre-commit install
pre-commit install --hook-type commit-msg
pre-commit autoupdate# Hooks run automatically on git commit
git add .
git commit -m "feat: add new stack"
# → pre-commit automatically runs checks# Run on specific files
pre-commit run --files aws/10-plt/03-networking/terragrunt.hcl
# Run on all files (takes time)
pre-commit run --all-files
# Run specific hooks only
pre-commit run trailing-whitespace --all-files
pre-commit run check-yaml --all-files# Bypass hooks if needed (not recommended)
git commit --no-verify
# Or using environment variable
SKIP=trailing-whitespace git commit -m "..."| Hook | Purpose |
|---|---|
trailing-whitespace |
Remove trailing whitespace |
end-of-file-fixer |
Ensure files end with newline |
check-yaml |
Validate YAML syntax |
check-json |
Validate JSON syntax |
check-merge-conflict |
Detect merge conflict markers |
detect-private-key |
Detect private key files |
| Hook | Rule |
|---|---|
conventional-pre-commit |
Enforce Conventional Commits format |
Format Examples:
feat: add echo worker deployment
fix: resolve SQS policy timeout
docs: update architecture diagram
chore: update Terragrunt version
Edit .pre-commit-config.yaml to customize:
# Disable specific hooks
- repo: https://github.com/compilerla/conventional-pre-commit
rev: v4.3.0
hooks:
- id: conventional-pre-commit
stages: [manual] # Change to manual execution# Make setup script executable
chmod +x .pre-commit-setup.sh
# Re-run setup
./.pre-commit-setup.sh# Use Conventional Commits format
# Examples: feat:, fix:, docs:, chore:, refactor:
git commit -m "feat: describe your change"
# Or skip for now
SKIP=conventional-pre-commit git commit -m "..."# Pre-commit auto-fixed formatting issues
# Review and stage the changes
git add .
git commit -m "..."Pre-commit runs only on staged files, not entire repo.
Edit .pre-commit-config.yaml:
exclude: |
(?x)^(
\.terragrunt-cache/|
\.terraform/|
node_modules/
)# Run hooks before push instead of commit
pre-commit install -t pre-push
# Then add to .pre-commit-config.yaml:
# stages: [pre-push]GitHub Actions already run checks:
terragrunt-check.yml- Runs validation on PRsterragrunt-fmt-fix.yml- Auto-fixes formatting on main branch
Local pre-commit hooks are additional tools to speed up local development.
Keep all developers in sync:
# Update to latest hook versions (regularly)
pre-commit autoupdate
# Commit updated versions
git add .pre-commit-config.yaml
git commit -m "chore: update pre-commit hooks"