Skip to content

Commit

Permalink
fix: handle single deployments and wait for meta-data set (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelFoleyFZ authored Oct 10, 2024
1 parent d25af81 commit cf70982
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
13 changes: 8 additions & 5 deletions hooks/post-command
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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}
Expand All @@ -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
Expand Down
9 changes: 4 additions & 5 deletions tests/post-command.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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 )"
}
Expand Down Expand Up @@ -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"
}

Expand Down Expand Up @@ -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}\""
}
Expand Down

0 comments on commit cf70982

Please sign in to comment.