From 57c829cb2f0b28399244cb7eb69341cf92fd544f Mon Sep 17 00:00:00 2001 From: Jon Burgess Date: Fri, 9 Sep 2022 14:57:54 +1000 Subject: [PATCH 1/2] Change back to original directory Plugins (such as artifacts plugin) make an assume about PWD. --- hooks/command | 2 ++ 1 file changed, 2 insertions(+) diff --git a/hooks/command b/hooks/command index 3fceb6f..be4742a 100755 --- a/hooks/command +++ b/hooks/command @@ -208,3 +208,5 @@ function terraform-run() { } terraform-run + +cd - From 6292b87ce6cc8d08d89da1a9fa6b056cc004a6a0 Mon Sep 17 00:00:00 2001 From: Jon Burgess Date: Mon, 12 Sep 2022 10:22:46 +1000 Subject: [PATCH 2/2] Use tf exit rather than parsing log output Previous method doesn't work for newer versions of terraform; this way should be more reliable. --- hooks/command | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/hooks/command b/hooks/command index be4742a..ef6dd23 100755 --- a/hooks/command +++ b/hooks/command @@ -169,17 +169,17 @@ function terraform-run() { if [[ "${APPLY_ONLY}" == false ]]; then echo "+++ :terraform: :buildkite: :hourglass: Running Terraform plan..." + set +e if [[ "${USE_WORKSPACES}" == true ]]; then - terraform-bin plan -input=false -out tfplan -var-file="${WORKSPACE}-terraform.tfvars" + terraform-bin plan -input=false -out tfplan -detailed-exitcode -var-file="${WORKSPACE}-terraform.tfvars" else - terraform-bin plan -input=false -out tfplan + terraform-bin plan -input=false -out tfplan -detailed-exitcode fi + TF_PLAN_EXIT_CODE=$? + set -x - # Capture plan output for setting variables and passing as artifacts. - terraform-bin show tfplan -no-color > tfplan.txt - terraform-bin show -json tfplan > tfplan.json - if grep -iFq "Plan: 0 to add, 0 to change, 0 to destroy." tfplan.txt; then + if [ $TF_PLAN_EXIT_CODE -eq 0 ]; then echo "" echo "--- :terraform: :buildkite: :white_check_mark: Exporting tf_diff=false to agent metadata." buildkite-agent meta-data set "tf_diff" "false"