This repo defines Git pre-commit hooks intended for use with pre-commit. The currently supported hooks are:
- terraform-fmt: Automatically run
terraform fmt
on all Terraform code (*.tf
files). - terraform-validate: Automatically run
terraform validate
on all Terraform code (*.tf
files). - tflint: Automatically run
tflint
on all Terraform code (*.tf
files). - terragrunt-fmt: Automatically format all Terragrunt code (
*.hcl
files).
Inspired by the corresponding gruntwork.io repository! For now it is tailored for the needs of the MLOps platform project, feel free to extend for general use!
In each of your repos, add a file called .pre-commit-config.yaml
with the following contents:
repos:
- repo: https://bitbucket.org/linkit-group/pre-commit
rev: <VERSION> # fill in the latest tag or master for bleeding edge
hooks:
- id: terraform-fmt
- id: terraform-validate
- id: tflint
- id: terragrunt-hclfmt
Next, have every developer:
- Install pre-commit. E.g.
brew install pre-commit
. - Run
pre-commit install
in the repo.
That’s it! Now every time you commit a code change, the hooks in the hooks:
config will execute.
If you'd like to format all of your code at once (rather than one file at a time), you can run:
pre-commit run terraform-fmt --all-files
If you'd like to enforce all your hooks, you can configure your CI build to fail if the code doesn't pass checks by adding the following to your build scripts:
pip install pre-commit
pre-commit install
pre-commit run --all-files
If all the hooks pass, the last command will exit with an exit code of 0. If any of the hooks make changes (e.g., because files are not formatted), the last command will exit with a code of 1, causing the build to fail.
You can optionally use code for setup/teardown of the validation, if required (e.g., if missing code is added at a later stage in the pipeline). Use the parameter
--setup-file <path>
, that points to a single file containing the setup/teardown code. This file will be added before validation and removed afterwards.
Example:
repos:
- repo: https://bitbucket.org/linkit-group/pre-commit
rev: <VERSION> # fill in the latest tag or master for bleeding edge
hooks:
- id: terraform-fmt
args: [--setup-file=test.tf]