As you work on the large codebase, you may want to run a set of checks before you submit a pull request for code review. This is a great way to ensure that your code is ready for review. Git hooks are a great way to do this. The pre-commit hook is a framework for creating git hooks that can be used to run a set of checks before you commit the code into a repository.
pre-commit hooks automatically scan the codebase and point out the issues with a code such as linting errors, style violations, missing semicolons, etc. This helps you to ensure that the reviewers can focus on the business logic and not on the trivial details. The pre-commit framework supports multiple programming languages and can be used to run a set of checks for multiple programming languages.
The pre-commit package manager can be installed using the pip command:
pip install pre-commit
Once done, verify that the pre-commit is installed by running the following command:
How to use pre-commit hooks
The pre-commit hooks can be used in any programming language. The only requirement is to have a
.pre-commit-config.yaml file in the root of the repository.
Inside this file, you can specify the hooks that you want to run.
The hooks are defined as a list of dictionaries. Each dictionary contains the following keys:
reposis the root key that contains the repository of the hook.
revis the revision of the hook.
hooksis the list of hooks that you want to run.
Each hook is defined as a dictionary. The hook contains the following keys:
idis the id of the hook.
nameis the name of the hook.
entryis the entry point of the hook.
languageis the programming language of the hook.
repos: - repo: https://github.com/pre-commit/pre-commit-hooks rev: v3.2.0 hooks: - id: check-yaml - id: end-of-file-fixer - id: trailing-whitespace - repo: https://github.com/psf/black rev: 19.3b0 hooks: - id: black
If we want to run pre-commit hooks on every commit, we need to install the pre-commit hook using the following command:
We can also run the pre-commit against all the files in the repository using the following command:
pre-commit run --all-files
How to use pre-commit hooks in Terraform
Let us now see how to use pre-commit hooks in Terraform. The project pre-commit-terraform already has the pre-commit hooks to take care of Terraform files.
Please Check out the code from the GitHub repository TerraformCode and then add the file
.pre-commit-config.yaml to the root of the repository with the following content:
repos: - repo: https://github.com/antonbabenko/pre-commit-terraform rev: v1.62.3 hooks: - id: terraform_fmt - id: terraform_validate
We are using the pre-commit-terraform repository to run the hooks such as
terraform fmt and
terraform fmt is a tool that formats the Terraform files.
terraform validate is a tool that validates the Terraform files. These two hooks are run on every commit.
We can install the pre-commit hooks using the following command:
Now, for every commit you make locally, it will check the above two hooks for formatting the terraform files and validating the terraform files. In this way, we can ensure that the terraform files are formatted and validated before we commit them. There are various hooks supported by pre-commit-terraform. Please refer to the pre-commit-terraform repository for more details.
Did you find this article valuable?
Support Rahul Lokurte by becoming a sponsor. Any amount is appreciated!