Skip to content

Commit

Permalink
feat: Add --retry-once-with-cleanup to terraform_validate
Browse files Browse the repository at this point in the history
  • Loading branch information
baolsen committed Oct 21, 2022
1 parent 07c6764 commit a7e3d90
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,17 @@ Example:
- --tf-init-args=-lockfile=readonly
```

3. It may happen that Terraform working directory (`.terraform`) already exists but not in the best condition (eg, not initialized modules, wrong version of Terraform, etc.). To solve this problem, you can find and delete all `.terraform` directories in your repository:
3. It may happen that Terraform working directory (`.terraform`) already exists but not in the best condition (eg, not initialized modules, wrong version of Terraform, etc.). To solve this problem, you can delete broken `.terraform` directories in your repository:

```yaml
- id: terraform_validate
args:
- --hook-config=--retry-once-with-cleanup=true # Boolean. true or false
```

If `--retry-once-with-cleanup=true`, then in each failed directory the `.terraform` directory will first be deleted before retrying once more.

An alternative solution is to find and delete all `.terraform` directories in your repository:

```bash
echo "
Expand All @@ -666,7 +676,7 @@ Example:

`terraform_validate` hook will try to reinitialize them before running the `terraform validate` command.

**Warning:** If you use Terraform workspaces, DO NOT use this workaround ([details](https://github.com/antonbabenko/pre-commit-terraform/issues/203#issuecomment-918791847)). Wait to [`force-init`](https://github.com/antonbabenko/pre-commit-terraform/issues/224) option implementation.
**Warning:** If you use Terraform workspaces, DO NOT use these workarounds ([details](https://github.com/antonbabenko/pre-commit-terraform/issues/203#issuecomment-918791847)). Wait to [`force-init`](https://github.com/antonbabenko/pre-commit-terraform/issues/224) option implementation.

4. `terraform_validate` in a repo with Terraform module, written using Terraform 0.15+ and which uses provider `configuration_aliases` ([Provider Aliases Within Modules](https://www.terraform.io/language/modules/develop/providers#provider-aliases-within-modules)), errors out.

Expand Down
48 changes: 44 additions & 4 deletions hooks/terraform_validate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,57 @@ function per_dir_hook_unique_part {
local -a -r args=("$@")

local exit_code
local validate_output

common::terraform_init 'terraform validate' "$dir_path" || {
#
# Get hook settings
#
local retry_once_with_cleanup=false

IFS=";" read -r -a configs <<< "${HOOK_CONFIG[*]}"

for c in "${configs[@]}"; do

IFS="=" read -r -a config <<< "$c"
key=${config[0]}
value=${config[1]}

case $key in
--retry-once-with-cleanup)
retry_once_with_cleanup=$value
;;
esac
done

function do_validate {

local exit_code
local validate_output

common::terraform_init 'terraform validate' "$dir_path" || {
exit_code=$?
return $exit_code
}

# pass the arguments to hook
validate_output=$(terraform validate "${args[@]}" 2>&1)
exit_code=$?

return $exit_code
}

# pass the arguments to hook
validate_output=$(terraform validate "${args[@]}" 2>&1)
do_validate
exit_code=$?

if [ $exit_code -ne 0 ] && [ "$retry_once_with_cleanup" = true ]; then
if [ -d .terraform ]; then
# Will only be displayed if validation fails again.
common::colorify "yellow" "Validation failed. Re-initialising: $dir_path"
rm -r .terraform
do_validate
exit_code=$?
fi
fi

if [ $exit_code -ne 0 ]; then
common::colorify "red" "Validation failed: $dir_path"
echo -e "$validate_output\n\n"
Expand Down

0 comments on commit a7e3d90

Please sign in to comment.