From c64a9b4a07e0aa59600b20187e000c55131accc8 Mon Sep 17 00:00:00 2001 From: Michael Foley Date: Thu, 10 Oct 2024 20:42:51 +1100 Subject: [PATCH] fix: handle single deployments and wait for meta-data set --- hooks/post-command | 13 ++++++++----- tests/post-command.bats | 9 ++++----- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/hooks/post-command b/hooks/post-command index d7b6786..6af02b3 100755 --- a/hooks/post-command +++ b/hooks/post-command @@ -26,6 +26,7 @@ function plugin_read_list_into_result() { } MODULE_DIR="$(echo "${BUILDKITE_PLUGIN_TERRAGRUNT_WORKSPACE_MODULE_DIR}" | sed 's/\/$//')" +FAIL_ON_NO_MODULES="${BUILDKITE_PLUGIN_TERRAGRUNT_WORKSPACE_FAIL_ON_NO_MODULES-"true"}" # Terragrunt extra args if plugin_read_list_into_result BUILDKITE_PLUGIN_TERRAGRUNT_WORKSPACE_TERRAGRUNT_ARGS ; then @@ -50,7 +51,7 @@ discovered_modules=() if [[ -z "${discovered_modules_list}" ]]; then printf "\U274C No modules disovered" - if [[ "${BUILDKITE_PLUGIN_TERRAGRUNT_WORKSPACE_FAIL_ON_NO_MODULES}" == "false" ]]; then + if [[ "${FAIL_ON_NO_MODULES}" == "false" ]]; then exit 0 else exit 1 @@ -136,7 +137,7 @@ done refresh_commands=("$(printf ' %s\n' "${refresh_commands[@]}")") # Plan encryption command -if [[ -n "${BUILDKITE_PLUGIN_TERRAGRUNT_WORKSPACE_PLAN_ENCRYPTION_KMS_KEY_ARN=""}" ]] ; then +if [[ -n "${BUILDKITE_PLUGIN_TERRAGRUNT_WORKSPACE_PLAN_ENCRYPTION_KMS_KEY_ARN-""}" ]] ; then plan_encryption_command="sops encrypt -i --kms \"${BUILDKITE_PLUGIN_TERRAGRUNT_WORKSPACE_PLAN_ENCRYPTION_KMS_KEY_ARN}\" \".plans/\$\${module}\"" plan_decryption_command="sops decrypt -i \".plans/\$\${module}\"" else @@ -152,7 +153,7 @@ if (( ${#deploy_modules[@]} == 0 )); then buildkite-agent annotate ":terragrunt: **${BUILDKITE_LABEL}**\nNo modules found for deployment" --style "warning" --context "${BUILDKITE_STEP_ID}" printf "\U274C No modules found for deployment" - if [[ "${BUILDKITE_PLUGIN_TERRAGRUNT_WORKSPACE_FAIL_ON_NO_MODULES}" == "false" ]]; then + if [[ "${FAIL_ON_NO_MODULES}" == "false" ]]; then exit 0 else exit 1 @@ -164,6 +165,8 @@ elif (( ${#deploy_modules[@]} == 1 )); then - label: \":terragrunt: [${BUILDKITE_LABEL}] Setting Module to Deploy\" command: |- buildkite-agent meta-data set modules \"${deploy_modules[@]}\" + +- wait: ~ " elif (( ${#deploy_modules[@]} > 1 )); then @@ -214,7 +217,7 @@ PIPELINE+=" ${refresh_commands[@]} mkdir -p .plans for module in \$\$(buildkite-agent meta-data get modules); do - echo \"--- planning \$\${module}\" + echo \"+++ planning \$\${module}\" printf '\n%s' \" - \$\${module}\" | buildkite-agent annotate --append --context \"${BUILDKITE_STEP_ID}\" terragrunt plan -out \"\$\${PWD}/.plans/\$\${module}\" --terragrunt-working-dir \"${MODULE_DIR}/\$\${module}\" ${terragrunt_args[@]} ${plan_encryption_command} @@ -231,7 +234,7 @@ ${refresh_commands[@]} mkdir -p .plans buildkite-agent artifact download '.plans/**' .plans for module in \$\$(buildkite-agent meta-data get modules); do - echo \"--- applying \$\${module}\" + echo \"+++ applying \$\${module}\" ${plan_decryption_command} terragrunt apply --terragrunt-working-dir \"${MODULE_DIR}/\$\${module}\" ${terragrunt_args[@]} \"\$\${PWD}/.plans/\$\${module}\" done diff --git a/tests/post-command.bats b/tests/post-command.bats index 4185b5b..a525b53 100755 --- a/tests/post-command.bats +++ b/tests/post-command.bats @@ -15,7 +15,6 @@ setup() { export BUILDKITE_STEP_ID="080b7d73-986d-4a39-a510-b34f9faf4710" export BUILDKITE_LABEL="testing" export BUILDKITE_PLUGIN_TERRAGRUNT_WORKSPACE_MODULE_DIR="test/test" - export BUILDKITE_PLUGIN_TERRAGRUNT_WORKSPACE_FAIL_ON_NO_MODULES="true" export BUILDKITE_PLUGINS="$( jq -c '.' $PWD/tests/data/buildkite_plugins.json)" export STEP_OUTPUT="$(jq -c '.' $PWD/tests/data/step.json )" } @@ -54,13 +53,13 @@ setup() { run yq '.steps[0].label' $BUILDKITE_PLUGIN_TERRAGRUNT_WORKSPACE_DEBUG_PIPELINE_OUTPUT assert_output ":terragrunt: [${BUILDKITE_LABEL}] Setting Module to Deploy" - run yq '.steps[1].label' $BUILDKITE_PLUGIN_TERRAGRUNT_WORKSPACE_DEBUG_PIPELINE_OUTPUT + run yq '.steps[2].label' $BUILDKITE_PLUGIN_TERRAGRUNT_WORKSPACE_DEBUG_PIPELINE_OUTPUT assert_output ":terragrunt: [${BUILDKITE_LABEL}] Plan Modules" - run yq '.steps[2].block' $BUILDKITE_PLUGIN_TERRAGRUNT_WORKSPACE_DEBUG_PIPELINE_OUTPUT + run yq '.steps[3].block' $BUILDKITE_PLUGIN_TERRAGRUNT_WORKSPACE_DEBUG_PIPELINE_OUTPUT assert_output ":terragrunt: [${BUILDKITE_LABEL}] Apply Changes?" - run yq '.steps[3].label' $BUILDKITE_PLUGIN_TERRAGRUNT_WORKSPACE_DEBUG_PIPELINE_OUTPUT + run yq '.steps[4].label' $BUILDKITE_PLUGIN_TERRAGRUNT_WORKSPACE_DEBUG_PIPELINE_OUTPUT assert_output ":terragrunt: [${BUILDKITE_LABEL}] Apply Modules" } @@ -95,7 +94,7 @@ setup() { refute_output --partial "terragrunt plan --terragrunt-working-dir ${BUILDKITE_PLUGIN_TERRAGRUNT_WORKSPACE_MODULE_DIR}/${BUILDKITE_PLUGIN_TERRAGRUNT_WORKSPACE_DATA_MODULES_0}" # The first command of the plan step should be a refresh - run yq '.steps[1].command' $BUILDKITE_PLUGIN_TERRAGRUNT_WORKSPACE_DEBUG_PIPELINE_OUTPUT + run yq '.steps[2].command' $BUILDKITE_PLUGIN_TERRAGRUNT_WORKSPACE_DEBUG_PIPELINE_OUTPUT assert_success assert_output --partial "terragrunt refresh --terragrunt-working-dir \"${BUILDKITE_PLUGIN_TERRAGRUNT_WORKSPACE_MODULE_DIR}/${BUILDKITE_PLUGIN_TERRAGRUNT_WORKSPACE_DATA_MODULES_0}\"" }