Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add terragrunt functions so we can work with tofu/terraform #119

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
exclude: \.+.terraform\/.*$
require_serial: true

- id: terragrunt-fmt
name: Terragrunt fmt
description: Rewrites all Terraform configuration files to a canonical format using terragrunt.
entry: hooks/terragrunt-fmt.sh
language: script
files: \.tf$
exclude: \.+.terraform\/.*$
require_serial: true

- id: terraform-validate
name: Terraform validate
description: Validates all Terraform configuration files
Expand All @@ -18,6 +27,15 @@
exclude: \.+.terraform\/.*$
require_serial: true

- id: terragrunt-validate
name: Terragrunt validate
description: Validates all Terraform configuration files using terragrunt.
entry: hooks/terragrunt-validate.sh
language: script
files: \.tf$
exclude: \.+.terraform\/.*$
require_serial: true

- id: packer-validate
name: Packer validate
description: Validates all Packer configuration files
Expand Down Expand Up @@ -46,6 +64,18 @@
.+\.terragrunt-cache\/.*$|
)$

- id: terragrunt-hclvalidate
name: Terragrunt hclvalidate
description: Validates all Terragrunt configuration files to a canonical format
entry: hooks/terragrunt-hclvalidate.sh
language: script
files: \.hcl$
exclude: >
(?x)^(
.+\.terraform\/.*$|
.+\.terragrunt-cache\/.*$|
)$

- id: shellcheck
name: Shellcheck Bash Linter
description: Performs linting on bash scripts
Expand Down
21 changes: 21 additions & 0 deletions hooks/terragrunt-fmt.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env bash

set -e

# OSX GUI apps do not pick up environment variables the same way as Terminal apps and there are no easy solutions,
# especially as Apple changes the GUI app behavior every release (see https://stackoverflow.com/q/135688/483528). As a
# workaround to allow GitHub Desktop to work, add this (hopefully harmless) setting here.
original_path=$PATH
export PATH=$PATH:/usr/local/bin

# Store and return last failure from fmt so this can validate every directory passed before exiting
FMT_ERROR=0

for file in "$@"; do
terragrunt --terragrunt-no-auto-init fmt -diff -check "$file" || FMT_ERROR=$?
done

# reset path to the original value
export PATH=$original_path

exit ${FMT_ERROR}
14 changes: 14 additions & 0 deletions hooks/terragrunt-hclvalidate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env bash

set -e

# OSX GUI apps do not pick up environment variables the same way as Terminal apps and there are no easy solutions,
# especially as Apple changes the GUI app behavior every release (see https://stackoverflow.com/q/135688/483528). As a
# workaround to allow GitHub Desktop to work, add this (hopefully harmless) setting here.
export PATH=$PATH:/usr/local/bin

for file in "$@"; do
pushd "$(dirname "$file")" >/dev/null
terragrunt hclvalidate "$(basename "$file")"
popd >/dev/null
done
24 changes: 24 additions & 0 deletions hooks/terragrunt-validate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash

set -e

# OSX GUI apps do not pick up environment variables the same way as Terminal apps and there are no easy solutions,
# especially as Apple changes the GUI app behavior every release (see https://stackoverflow.com/q/135688/483528). As a
# workaround to allow GitHub Desktop to work, add this (hopefully harmless) setting here.
export PATH=$PATH:/usr/local/bin

# Disable output not usually helpful when running in automation (such as guidance to run plan after init)
export TF_IN_AUTOMATION=1

# Store and return last failure from validate so this can validate every directory passed before exiting
VALIDATE_ERROR=0

for dir in $(echo "$@" | xargs -n1 dirname | sort -u | uniq); do
echo "--> Running 'terraform validate' in directory '$dir'"
pushd "$dir" >/dev/null
terragrunt init -backend=false || VALIDATE_ERROR=$?
terragrunt validate || VALIDATE_ERROR=$?
popd >/dev/null
done

exit ${VALIDATE_ERROR}