Skip to content

Commit

Permalink
use terraform resource to destroy old workspaces
Browse files Browse the repository at this point in the history
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: concourse/concourse#7310

Signed-off-by: Aidan Oldershaw <[email protected]>
  • Loading branch information
aoldershaw committed Jul 20, 2021
1 parent 64a226a commit 120b1a1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 28 deletions.
70 changes: 43 additions & 27 deletions pipelines/multi-branch/tracker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -35,49 +49,51 @@ 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:
- -c
- |
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
3 changes: 2 additions & 1 deletion terraform/staging/main.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
variable "bundle_url" {
type = string
type = string
default = ""
}

resource "null_resource" "fake_deployment" {
Expand Down

0 comments on commit 120b1a1

Please sign in to comment.