From 120b1a1cc0c802e676bfd0f6c18ada19f09a395f Mon Sep 17 00:00:00 2001 From: Aidan Oldershaw Date: Tue, 20 Jul 2021 11:37:31 -0400 Subject: [PATCH] use terraform resource to destroy old workspaces a bit torn on whether to do this, since it's possibly easier to follow just doing it in the shell script but hey, at least it uncovered a bug: https://github.com/concourse/concourse/pull/7310 Signed-off-by: Aidan Oldershaw --- pipelines/multi-branch/tracker.yml | 70 ++++++++++++++++++------------ terraform/staging/main.tf | 3 +- 2 files changed, 45 insertions(+), 28 deletions(-) diff --git a/pipelines/multi-branch/tracker.yml b/pipelines/multi-branch/tracker.yml index 88506622..82e0c19d 100644 --- a/pipelines/multi-branch/tracker.yml +++ b/pipelines/multi-branch/tracker.yml @@ -4,6 +4,11 @@ resource_types: source: repository: aoldershaw/git-branches-resource +- name: terraform + type: registry-image + source: + repository: ljfranklin/terraform-resource + resources: - name: feature-branches type: git-branches @@ -16,6 +21,15 @@ resources: source: uri: https://github.com/concourse/examples +- name: staging-env + type: terraform + source: + backend_type: gcs + backend_config: &terraform_backend_config + bucket: concourse-examples + prefix: multi-branch/terraform + credentials: ((concourse_artifacts_json_key)) + jobs: - name: set-feature-pipelines plan: @@ -35,26 +49,24 @@ jobs: - name: cleanup-inactive-workspaces plan: - - get: feature-branches - passed: [set-feature-pipelines] - trigger: true - - load_var: active_branches - file: feature-branches/branches.json - - task: cleanup + - in_parallel: + - get: feature-branches + passed: [set-feature-pipelines] + trigger: true + - get: examples + - task: find-inactive-workspaces config: platform: linux image_resource: type: registry-image source: {repository: hashicorp/terraform} + inputs: + - name: feature-branches + outputs: + - name: extra-workspaces params: - ACTIVE_BRANCHES: ((.:active_branches)) TERRAFORM_BACKEND_CONFIG: - terraform: - backend: - gcs: - bucket: concourse-examples - prefix: multi-branch/terraform - credentials: ((concourse_artifacts_json_key)) + gcs: *terraform_backend_config run: path: sh args: @@ -62,22 +74,26 @@ jobs: - | set -euo pipefail - apk add jq + apk add -q jq - active_features="$(echo "$ACTIVE_BRANCHES" | jq '[.[].groups.feature]')" + active_features="$(jq '[.[].groups.feature]' feature-branches/branches.json)" - echo "$TERRAFORM_BACKEND_CONFIG" > backend.tf.json + jq -n "{terraform: {backend: $TERRAFORM_BACKEND_CONFIG}}" > backend.tf.json terraform init - active_workspaces="$(terraform workspace list | grep -v '^[*]' | tr -d ' ' | jq --raw-input --slurp 'split("\n") | map(select(. != ""))')" - jq -nr "$active_workspaces - $active_features | .[]" | while read extra_workspace - do - echo "deleting workspace $extra_workspace" - terraform workspace select "$extra_workspace" - terraform init - - terraform destroy -auto-approve + # List all active workspaces, ignoring the default workspace + active_workspaces="$(terraform workspace list | grep -v '^[*]' | tr -d ' ' | jq --raw-input --slurp 'split("\n") | map(select(. != ""))')" - terraform workspace select default - terraform workspace delete "$extra_workspace" - done + jq -n "$active_workspaces - $active_features" > extra-workspaces/workspaces.json + - load_var: extra_workspaces + file: extra-workspaces/workspaces.json + - across: + - var: workspace + values: ((.:extra_workspaces)) + put: staging-env + params: + terraform_source: examples/terraform/staging + env_name: ((.:workspace)) + action: destroy + get_params: + action: destroy diff --git a/terraform/staging/main.tf b/terraform/staging/main.tf index b625d07d..df8fdaf5 100644 --- a/terraform/staging/main.tf +++ b/terraform/staging/main.tf @@ -1,5 +1,6 @@ variable "bundle_url" { - type = string + type = string + default = "" } resource "null_resource" "fake_deployment" {