This repo defines Git pre-commit hooks intended for use with pre-commit. The currently supported hooks are:
- terraform-fmt: Checks that all Terraform files (
*.tf) are properly formatted (terraform fmt --check -diff). - terraform-validate: Runs
terraform init -backend=falseand thenterraform validate.Notes: directories requiring a private registry and lacking credentials are marked as skipped (do not fail the commit). Both hooks ignore
.terraform/andexamples/.
In each of your repos, add a file called .pre-commit-config.yaml with the following contents:
repos:
- repo: [email protected]:craftech-io/pre-commit.git # or https://github.com/craftech-io/pre-commit.git
rev: <VERSION>
hooks:
- id: terraform-fmt
- id: terraform-validate
verbose: trueNext, have every developer:
- Install pre-commit.
- macOS:
brew install pre-commit - Linux:
pipx install pre-commit(orpip install --user pre-commit)
- macOS:
- Run
pre-commit installin the repo.
That's it! Now every time you commit a code change (.tf file), the hooks in the hooks: config will execute.
If any hook fails, the commit is aborted; if all pass, the commit succeeds.
If you'd like to run the hooks across the whole repo (useful the first time), you can run:
# Check formatting for all Terraform files
pre-commit run terraform-fmt --all-files
# Validate all Terraform directories
pre-commit run terraform-validate --all-files
# Or run every configured hook across the repo
pre-commit run --all-filesTip: for detailed output on demand, use
-v, e.g.pre-commit run -v terraform-validate --all-files.
This code is released under the Apache 2.0 License. Please see LICENSE for more details.