From f513e81955130732fabd8e2c9783d2e0eb468dc0 Mon Sep 17 00:00:00 2001 From: Timo Furrer <tfurrer@gitlab.com> Date: Thu, 18 Jan 2024 17:19:40 +0100 Subject: [PATCH] Fix cleanup job --- Makefile | 2 ++ backports/OpenTofu/Base.gitlab-ci.yml | 7 +++++++ templates/full-pipeline.yml | 13 +++++++++++++ 3 files changed, 22 insertions(+) diff --git a/Makefile b/Makefile index 4af2e6c..ac8327a 100644 --- a/Makefile +++ b/Makefile @@ -35,6 +35,8 @@ backports: @sed -i $(BACKPORTS_BASE_FILE) -e 's/$$\[\[ inputs.state_name \]\]/$$TF_STATE_NAME/' @sed -i $(BACKPORTS_BASE_FILE) -e 's/$$\[\[ inputs.auto_apply \]\]/$$TF_AUTO_APPLY/' @sed -i $(BACKPORTS_BASE_FILE) -e 's/$$\[\[ inputs.auto_destroy \]\]/$$TF_AUTO_DESTROY/' + @sed -i $(BACKPORTS_BASE_FILE) -e 's/$$\[\[ inputs.create_destroy_job \]\]/$$TF_CREATE_DESTROY_JOB/' + @sed -i $(BACKPORTS_BASE_FILE) -e 's/$$\[\[ inputs.create_delete_state_job \]\]/$$TF_CREATE_DELETE_STATE_JOB/' @sed -i $(BACKPORTS_BASE_FILE) -e 's/\.default/.opentofu:default/' @sed -i $(BACKPORTS_BASE_FILE) -e 's/^fmt:$$/.opentofu:fmt:/' @sed -i $(BACKPORTS_BASE_FILE) -e 's/^validate:$$/.opentofu:validate:/' diff --git a/backports/OpenTofu/Base.gitlab-ci.yml b/backports/OpenTofu/Base.gitlab-ci.yml index beb26ab..081da91 100644 --- a/backports/OpenTofu/Base.gitlab-ci.yml +++ b/backports/OpenTofu/Base.gitlab-ci.yml @@ -109,6 +109,8 @@ variables: action: stop resource_group: $TF_STATE_NAME rules: + - if: '"$TF_CREATE_DESTROY_JOB" != "true"' + when: never - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && "$TF_AUTO_DESTROY" == "true"' - when: manual @@ -120,4 +122,9 @@ variables: - when: never script: - curl --request DELETE -u "gitlab-ci-token:$CI_JOB_TOKEN" "$CI_API_V4_URL/projects/$CI_PROJECT_ID/terraform/state/$TF_STATE_NAME" + rules: + - if: '"$TF_CREATE_DELETE_STATE_JOB" != "true"' + when: never + - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH' + - when: manual diff --git a/templates/full-pipeline.yml b/templates/full-pipeline.yml index cb5b834..d243a26 100644 --- a/templates/full-pipeline.yml +++ b/templates/full-pipeline.yml @@ -49,6 +49,12 @@ spec: auto_destroy: default: 'false' description: 'Whether the destroy job is manual or automatically run.' + create_destroy_job: + default: 'false' + description: 'Wheather the destroy job should be created or not.' + create_delete_state_job: + default: 'false' + description: 'Wheather the delete-state job should be created or not.' --- @@ -137,6 +143,8 @@ destroy: action: stop resource_group: $[[ inputs.state_name ]] rules: + - if: '"$[[ inputs.create_destroy_job ]]" != "true"' + when: never - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && "$[[ inputs.auto_destroy ]]" == "true"' - when: manual @@ -149,4 +157,9 @@ delete-state: - when: never script: - curl --request DELETE -u "gitlab-ci-token:$CI_JOB_TOKEN" "$CI_API_V4_URL/projects/$CI_PROJECT_ID/terraform/state/$[[ inputs.state_name ]]" + rules: + - if: '"$[[ inputs.create_delete_state_job ]]" != "true"' + when: never + - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH' + - when: manual -- GitLab