From a787da3a868ec3a2d7ed6f61599df32084a0283f Mon Sep 17 00:00:00 2001 From: David Alger Date: Tue, 8 Dec 2020 09:07:54 -0600 Subject: [PATCH] Store and return last failure from validate vs exit on first failure --- hooks/terraform-validate.sh | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/hooks/terraform-validate.sh b/hooks/terraform-validate.sh index f6d98b90..b145ace3 100755 --- a/hooks/terraform-validate.sh +++ b/hooks/terraform-validate.sh @@ -7,9 +7,18 @@ set -e # 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 - terraform init -backend=false - terraform validate + terraform init -backend=false || VALIDATE_ERROR=$? + terraform validate || VALIDATE_ERROR=$? popd >/dev/null done + +exit ${VALIDATE_ERROR}