|
| 1 | +--- |
| 2 | +title: "pre-commit Hooks" |
| 3 | +description: "How to use pre-commit hooks with terraform-docs." |
| 4 | +menu: |
| 5 | + docs: |
| 6 | + parent: "how-to" |
| 7 | +weight: 209 |
| 8 | +toc: false |
| 9 | +--- |
| 10 | + |
| 11 | +Since `v0.12.0` |
| 12 | + |
| 13 | +With [`pre-commit`], you can ensure your Terraform module documentation is kept |
| 14 | +up-to-date each time you make a commit. |
| 15 | + |
| 16 | +1. simply create or update a `.pre-commit-config.yaml` |
| 17 | +in the root of your Git repo with at least the following content: |
| 18 | + |
| 19 | + ```yaml |
| 20 | + repos: |
| 21 | + - repo: https://github.com/terraform-docs/terraform-docs |
| 22 | + rev: "<VERSION, TAG, OR SHA TO USE>" # e.g. "v0.11.2" |
| 23 | + hooks: |
| 24 | + - id: terraform-docs-go |
| 25 | + args: ["ARGS", "TO PASS", "INCLUDING PATH"] # e.g. ["--output-file", "README.md", "./mymodule/path"] |
| 26 | + ``` |
| 27 | +
|
| 28 | + {{< alert type="info" >}} |
| 29 | + You can also include more than one entry under `hooks:` to update multiple docs. |
| 30 | + Just be sure to adjust the `args:` to pass the path you want terraform-docs to scan. |
| 31 | + {{< /alert >}} |
| 32 | + |
| 33 | +1. install [`pre-commit`] and run `pre-commit` to activate the hooks. |
| 34 | + |
| 35 | +1. make a Terraform change, `git add` and `git commit`. |
| 36 | +pre-commit will regenerate your Terraform docs, after which you can |
| 37 | +rerun `git add` and `git commit` to commit the code and doc changes together. |
| 38 | + |
| 39 | +You can also regenerate the docs manually by running `pre-commit -a terraform-docs`. |
| 40 | + |
| 41 | +### pre-commit via Docker |
| 42 | + |
| 43 | +The pre-commit hook can also be run via Docker, for those who don't have Go installed. |
| 44 | +Just use `id: terraform-docs-docker` in the previous example. |
| 45 | + |
| 46 | +This will build the Docker image from the repo, which can be quite slow. |
| 47 | +To download the pre-built image instead, change your `.pre-commit-config.yaml` to: |
| 48 | + |
| 49 | +```yaml |
| 50 | +repos: |
| 51 | + - repo: local |
| 52 | + hooks: |
| 53 | + - id: terraform-docs |
| 54 | + name: terraform-docs |
| 55 | + language: docker_image |
| 56 | + entry: quay.io/terraform-docs/terraform-docs:latest # or, change latest to pin to a specific version |
| 57 | + args: ["ARGS", "TO PASS", "INCLUDING PATH"] # e.g. ["--output-file", "README.md", "./mymodule/path"] |
| 58 | + pass_filenames: false |
| 59 | +``` |
| 60 | + |
| 61 | +## Git Hook |
| 62 | + |
| 63 | +A simple git hook (`.git/hooks/pre-commit`) added to your local terraform |
| 64 | +repository can keep your Terraform module documentation up to date whenever you |
| 65 | +make a commit. See also [git hooks] documentation. |
| 66 | + |
| 67 | +```sh |
| 68 | +#!/bin/sh |
| 69 | +
|
| 70 | +# Keep module docs up to date |
| 71 | +for d in modules/*; do |
| 72 | + if terraform-docs md "$d" > "$d/README.md"; then |
| 73 | + git add "./$d/README.md" |
| 74 | + fi |
| 75 | +done |
| 76 | +``` |
| 77 | + |
| 78 | +{{< alert type="warning" >}} |
| 79 | +This is very basic and higly simplified version of [pre-commit-terraform](https://github.com/antonbabenko/pre-commit-terraform). |
| 80 | +Please refer to it for complete examples and guides. |
| 81 | +{{< /alert >}} |
| 82 | + |
| 83 | +[`pre-commit`]: https://pre-commit.com/ |
| 84 | +[git hooks]: https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks |
0 commit comments