diff --git a/src/gitlab-tofu.sh b/src/gitlab-tofu.sh index c553ecc2d9d5d7c3b63a2ae462e01d0055187f5a..97903566cd23a8302a3da255ad5d78a6411233d7 100644 --- a/src/gitlab-tofu.sh +++ b/src/gitlab-tofu.sh @@ -14,6 +14,7 @@ # GITLAB_TOFU_SOURCE: forces this script in source-mode. Required when source auto-detection fails. # GITLAB_TOFU_APPLY_NO_PLAN: if set to true, the apply command does not use a plan cache file. # GITLAB_TOFU_PLAN_NAME: the name of the plan cache and json files. Defaults to `plan`. +# GITLAB_TOFU_PLAN_CACHE: if set is the full path of the plan cache file. Defaults to `<root>/$GITLAB_TOFU_PLAN_NAME.cache` # # Respected OpenTofu Environment Variables: # > these are variables that are @@ -128,9 +129,9 @@ if [ -n "${TF_ROOT}" ]; then fi -# If TF_PLAN_CACHE is not set then use either the plan.cache file within TF_ROOT if set, or plan.cache in CWD -if [ -z "${TF_PLAN_CACHE}" ]; then - TF_PLAN_CACHE="${default_tf_plan_cache:-${GITLAB_TOFU_PLAN_NAME}.cache}" +# If GITLAB_TOFU_PLAN_CACHE is not set then use either the plan.cache file within TF_ROOT if set, or plan.cache in CWD +if [ -z "${GITLAB_TOFU_PLAN_CACHE}" ]; then + GITLAB_TOFU_PLAN_CACHE="${default_tf_plan_cache:-${GITLAB_TOFU_PLAN_NAME}.cache}" fi # If TF_PLAN_JSON is not set then use either the plan.json file within TF_ROOT if set, or plan.json in CWD @@ -234,7 +235,7 @@ if [ $sourced -eq 0 ]; then "apply") $TF_IMPLICIT_INIT && terraform_init if [ "$GITLAB_TOFU_APPLY_NO_PLAN" = false ]; then - tofu "${TF_CHDIR_OPT}" "${@}" -input=false -auto-approve "${TF_PLAN_CACHE}" + tofu "${TF_CHDIR_OPT}" "${@}" -input=false -auto-approve "${GITLAB_TOFU_PLAN_CACHE}" else # shellcheck disable=SC2086 tofu "${TF_CHDIR_OPT}" "${@}" -input=false -auto-approve ${var_file_args} @@ -260,11 +261,11 @@ if [ $sourced -eq 0 ]; then $TF_IMPLICIT_INIT && terraform_init # shellcheck disable=SC2086 - tofu "${TF_CHDIR_OPT}" "${@}" -input=false -out="${TF_PLAN_CACHE}" ${var_file_args} ${plan_args} && ret=$? || ret=$? + tofu "${TF_CHDIR_OPT}" "${@}" -input=false -out="${GITLAB_TOFU_PLAN_CACHE}" ${var_file_args} ${plan_args} && ret=$? || ret=$? if [ "${GITLAB_TOFU_PLAN_WITH_JSON}" = 'true' ]; then if [ "$ret" -eq 0 ] || [ "$ret" -eq 2 ]; then - if ! tofu "${TF_CHDIR_OPT}" show -json "${TF_PLAN_CACHE}" | jq -r "${JQ_PLAN}" > "${TF_PLAN_JSON}"; then + if ! tofu "${TF_CHDIR_OPT}" show -json "${GITLAB_TOFU_PLAN_CACHE}" | jq -r "${JQ_PLAN}" > "${TF_PLAN_JSON}"; then exit $? fi @@ -276,7 +277,7 @@ if [ $sourced -eq 0 ]; then exit "$ret" ;; "plan-json") - tofu "${TF_CHDIR_OPT}" show -json "${TF_PLAN_CACHE}" | jq -r "${JQ_PLAN}" > "${TF_PLAN_JSON}" + tofu "${TF_CHDIR_OPT}" show -json "${GITLAB_TOFU_PLAN_CACHE}" | jq -r "${JQ_PLAN}" > "${TF_PLAN_JSON}" ;; "validate") $TF_IMPLICIT_INIT && terraform_init -backend=false diff --git a/templates/destroy.yml b/templates/destroy.yml index e69b2d4e02439bb52eb57f7c4f98da00bfcee38c..f1540b2a93ddf36aebe798d6281a94b25c8839a8 100644 --- a/templates/destroy.yml +++ b/templates/destroy.yml @@ -75,7 +75,7 @@ spec: description: 'Whether a plan file should be used.' plan_name: default: 'destroy-plan' - description: 'The name of the plan file to use. Will be used for TF_PLAN_CACHE and TF_PLAN_JSON.' + description: 'The name of the plan file to use. Will be used for the cache and json file.' var_file: default: '' type: string diff --git a/tests/unit/gitlab-tofu.bats b/tests/unit/gitlab-tofu.bats index 5b00ef1879529ec32d0fea08091ea368675e0c83..9e35b2012daf6f6f63a0d487ba72f083347ee38e 100644 --- a/tests/unit/gitlab-tofu.bats +++ b/tests/unit/gitlab-tofu.bats @@ -82,9 +82,9 @@ EOF } @test "gitlab-tofu plan" { - export TF_PLAN_CACHE="test-plan.cache" + export GITLAB_TOFU_PLAN_CACHE="test-plan.cache" gitlab-tofu plan - if [ ! -f "$TF_ROOT/$TF_PLAN_CACHE" ]; then + if [ ! -f "$TF_ROOT/$GITLAB_TOFU_PLAN_CACHE" ]; then echo "expected to find a plan.cache file" exit 1 fi @@ -97,13 +97,13 @@ EOF } @test "gitlab-tofu apply" { - export TF_PLAN_CACHE="test-plan.cache" + export GITLAB_TOFU_PLAN_CACHE="test-plan.cache" gitlab-tofu plan gitlab-tofu apply } @test "gitlab-tofu destroy" { - export TF_PLAN_CACHE="test-plan.cache" + export GITLAB_TOFU_PLAN_CACHE="test-plan.cache" gitlab-tofu plan gitlab-tofu apply gitlab-tofu destroy