Browse Source

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 <aoldershaw@pivotal.io>
pull/4/head
Aidan Oldershaw 4 years ago
parent
commit
120b1a1cc0
  1. 64
      pipelines/multi-branch/tracker.yml
  2. 1
      terraform/staging/main.tf

64
pipelines/multi-branch/tracker.yml

@ -4,6 +4,11 @@ resource_types:
source: source:
repository: aoldershaw/git-branches-resource repository: aoldershaw/git-branches-resource
- name: terraform
type: registry-image
source:
repository: ljfranklin/terraform-resource
resources: resources:
- name: feature-branches - name: feature-branches
type: git-branches type: git-branches
@ -16,6 +21,15 @@ resources:
source: source:
uri: https://github.com/concourse/examples 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: jobs:
- name: set-feature-pipelines - name: set-feature-pipelines
plan: plan:
@ -35,26 +49,24 @@ jobs:
- name: cleanup-inactive-workspaces - name: cleanup-inactive-workspaces
plan: plan:
- in_parallel:
- get: feature-branches - get: feature-branches
passed: [set-feature-pipelines] passed: [set-feature-pipelines]
trigger: true trigger: true
- load_var: active_branches
file: feature-branches/branches.json
- task: cleanup
- get: examples
- task: find-inactive-workspaces
config: config:
platform: linux platform: linux
image_resource: image_resource:
type: registry-image type: registry-image
source: {repository: hashicorp/terraform} source: {repository: hashicorp/terraform}
inputs:
- name: feature-branches
outputs:
- name: extra-workspaces
params: params:
ACTIVE_BRANCHES: ((.:active_branches))
TERRAFORM_BACKEND_CONFIG: TERRAFORM_BACKEND_CONFIG:
terraform:
backend:
gcs:
bucket: concourse-examples
prefix: multi-branch/terraform
credentials: ((concourse_artifacts_json_key))
gcs: *terraform_backend_config
run: run:
path: sh path: sh
args: args:
@ -62,22 +74,26 @@ jobs:
- | - |
set -euo pipefail set -euo pipefail
apk add jq
active_features="$(echo "$ACTIVE_BRANCHES" | jq '[.[].groups.feature]')"
apk add -q jq
echo "$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(. != ""))')"
active_features="$(jq '[.[].groups.feature]' feature-branches/branches.json)"
jq -nr "$active_workspaces - $active_features | .[]" | while read extra_workspace
do
echo "deleting workspace $extra_workspace"
terraform workspace select "$extra_workspace"
jq -n "{terraform: {backend: $TERRAFORM_BACKEND_CONFIG}}" > backend.tf.json
terraform init 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

1
terraform/staging/main.tf

@ -1,5 +1,6 @@
variable "bundle_url" { variable "bundle_url" {
type = string type = string
default = ""
} }
resource "null_resource" "fake_deployment" { resource "null_resource" "fake_deployment" {

Loading…
Cancel
Save