generated from ipdxco/github-as-code
-
Notifications
You must be signed in to change notification settings - Fork 4
96 lines (94 loc) · 3.2 KB
/
clean.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
name: Clean
on:
workflow_dispatch:
inputs:
workspaces:
description: Space separated list of workspaces to clean (leave blank to clean all)
required: false
regex:
description: Regex string to use to find the resources to remove from the state
required: false
default: .*
dry-run:
description: Whether to only print out what would've been removed
required: false
default: "true"
lock:
description: Whether to acquire terraform state lock during clean
required: false
default: "true"
jobs:
prepare:
name: Prepare
runs-on: ubuntu-latest
outputs:
workspaces: ${{ steps.workspaces.outputs.this }}
defaults:
run:
shell: bash
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Discover workspaces
id: workspaces
env:
WORKSPACES: ${{ github.event.inputs.workspaces }}
run: |
if [[ -z "${WORKSPACES}" ]]; then
workspaces="$(ls github | jq --raw-input '[.[0:-4]]' | jq -sc add)"
else
workspaces="$(echo "${WORKSPACES}" | jq --raw-input 'split(" ")')"
fi
echo "this=${workspaces}" >> $GITHUB_OUTPUT
clean:
needs: [prepare]
if: needs.prepare.outputs.workspaces != ''
permissions:
contents: write
strategy:
fail-fast: false
matrix:
workspace: ${{ fromJson(needs.prepare.outputs.workspaces) }}
name: Prepare
runs-on: ubuntu-latest
env:
TF_IN_AUTOMATION: 1
TF_INPUT: 0
TF_LOCK: ${{ github.event.inputs.lock }}
TF_WORKSPACE_OPT: ${{ matrix.workspace }}
AWS_ACCESS_KEY_ID: ${{ secrets.RW_AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.RW_AWS_SECRET_ACCESS_KEY }}
GITHUB_APP_ID: ${{ secrets.RW_GITHUB_APP_ID }}
GITHUB_APP_INSTALLATION_ID: ${{ secrets[format('RW_GITHUB_APP_INSTALLATION_ID_{0}', matrix.workspace)] || secrets.RW_GITHUB_APP_INSTALLATION_ID }}
GITHUB_APP_PEM_FILE: ${{ secrets.RW_GITHUB_APP_PEM_FILE }}
TF_VAR_write_delay_ms: 300
defaults:
run:
shell: bash
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup terraform
uses: hashicorp/setup-terraform@633666f66e0061ca3b725c73b2ec20cd13a8fdd1 # v2.0.3
with:
terraform_version: 1.2.9
terraform_wrapper: false
- name: Initialize terraform
run: terraform init -upgrade
working-directory: terraform
- name: Select terraform workspace
run: |
terraform workspace select "${TF_WORKSPACE_OPT}" || terraform workspace new "${TF_WORKSPACE_OPT}"
echo "TF_WORKSPACE=${TF_WORKSPACE_OPT}" >> $GITHUB_ENV
working-directory: terraform
- name: Clean
env:
DRY_RUN: ${{ github.event.inputs.dry-run }}
REGEX: ^${{ github.event.inputs.regex }}$
run: |
dryRunFlag=''
if [[ "${DRY_RUN}" == 'true' ]]; then
dryRunFlag='-dry-run'
fi
terraform state list | grep -E "${REGEX}" | sed 's/"/\\"/g' | xargs -I {} terraform state rm -lock="${TF_LOCK}" ${dryRunFlag} {}
working-directory: terraform