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. 70
      pipelines/multi-branch/tracker.yml
  2. 3
      terraform/staging/main.tf

70
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

3
terraform/staging/main.tf

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

Loading…
Cancel
Save