Skip to content

Commit

Permalink
feat: tfupdate integration (#328)
Browse files Browse the repository at this point in the history
Closes #328
  • Loading branch information
jrottenberg committed Feb 20, 2022
1 parent 458fb28 commit 2dd8fcb
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
10 changes: 10 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,13 @@
files: \.tf$
exclude: \.terraform\/.*$
require_serial: true

- id: tfupdate
name: tfupdate
description: Runs tfupdate on Terraform templates.
language: script
entry: hooks/tfupdate.sh
require_serial: true
files: \.tf$
pass_filenames: false
args: ["terraform"]
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ If you are using `pre-commit-terraform` already or want to support its developme
* [terraform_tfsec](#terraform_tfsec)
* [terraform_validate](#terraform_validate)
* [terrascan](#terrascan)
* [tfupdate](#tfupdate)
* [Authors](#authors)
* [License](#license)

Expand Down Expand Up @@ -224,7 +225,8 @@ There are several [pre-commit](https://pre-commit.com/) hooks to keep Terraform
| `terraform_validate` | Validates all Terraform configuration files. [Hook notes](#terraform_validate) | - |
| `terragrunt_fmt` | Reformat all [Terragrunt](https://github.com/gruntwork-io/terragrunt) configuration files (`*.hcl`) to a canonical format. | `terragrunt` |
| `terragrunt_validate` | Validates all [Terragrunt](https://github.com/gruntwork-io/terragrunt) configuration files (`*.hcl`) | `terragrunt` |
| `terrascan` | [terrascan](https://github.com/accurics/terrascan) Detect compliance and security violations. [Hook notes](#terrascan) | `terrascan` |
| `terrascan` | [terrascan](https://github.com/accurics/terrascan) Detect compliance and security violations. [Hook notes](#terrascan) | `terrascan` |
| `tfupdate` | [tfupdate](https://github.com/minamijoyo/tfupdate) Update version constraints of Terraform core, providers, and modules. [Hook notes](#tfupdate) | `tfupdate` |
<!-- markdownlint-enable no-inline-html -->

Check the [source file](https://github.com/antonbabenko/pre-commit-terraform/blob/master/.pre-commit-hooks.yaml) to know arguments used for each hook.
Expand Down Expand Up @@ -617,6 +619,27 @@ Example:
3. Use `--skip-rules="ruleID1,ruleID2"` parameter to skip one or more rules globally while scanning (e.g.: `--args=--skip-rules="ruleID1,ruleID2"`).
4. Use the syntax `#ts:skip=RuleID optional_comment` inside a resource to skip the rule for that resource.

### tfupdate

Out of the box tfupdate will pin the terraform version

```yaml
- id: tfupdate
```

But you can pass `tfupdate` custom commands like `provider ${PROVIDER_NAME}` :

```yaml
- id: tfupdate
name: tfupdate terraform
- id: tfupdate
name: tfupdate provider vsphere
args:
- provider
- vsphere
```
See the `tfupdate --help` command line help for available options. No need to pass `--recursive .` as it is added automatically

## Authors

This repository is managed by [Anton Babenko](https://github.com/antonbabenko) with help from these awesome contributors:
Expand Down
26 changes: 26 additions & 0 deletions hooks/tfupdate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env bash
set -eo pipefail

# shellcheck disable=SC2155 # No way to assign to readonly variable in separate lines
readonly SCRIPT_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
# shellcheck source=_common.sh
source "$SCRIPT_DIR/_common.sh"

function main {
common::initialize "$SCRIPT_DIR"
tfupdate_ "$@"
}

#######################################################################
# tfupdate_
#######################################################################
function tfupdate_ {
local -r args=$*
# pass the arguments to hook
# shellcheck disable=SC2086 # Double quote to prevent globbing and word splitting.
tfupdate ${args} --recursive .
local exit_code=$?
return $exit_code
}

[ "${BASH_SOURCE[0]}" != "$0" ] || main "$@"

0 comments on commit 2dd8fcb

Please sign in to comment.