-
Notifications
You must be signed in to change notification settings - Fork 2
369 lines (326 loc) · 16.2 KB
/
cleanup.yml
File metadata and controls
369 lines (326 loc) · 16.2 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
name: Nightly E2E Resource Cleanup
on:
schedule:
- cron: '0 2 * * *' # 02:00 UTC every day
workflow_dispatch: # allow manual trigger
jobs:
cleanup:
name: Sweep orphaned E2E test resources
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.12'
- run: pip install boto3
# ── Single account ────────────────────────────────────────────────────
- name: Assume role — single account
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::${{ secrets.AWS_SINGLE_ACCOUNT_ID }}:role/GitHubActionsE2ERole
aws-region: ap-northeast-2
continue-on-error: true
# Guard: if any map-auto-tagger-e2e-pr* stack in this account is younger
# than the in-flight window or actively IN_PROGRESS, skip the sweep to
# avoid deleting a live E2E run's resources mid-test. Stacks skipped
# tonight are caught tomorrow night once they age past the window.
- name: Guard — detect in-flight E2E (single account)
id: guard_single
run: |
if python3 .github/scripts/nightly_cleanup_guard.py check-account; then
echo "skip=false" >> $GITHUB_OUTPUT
else
echo "skip=true" >> $GITHUB_OUTPUT
fi
continue-on-error: true
- name: Delete tagged resources — single account
if: steps.guard_single.outputs.skip != 'true'
working-directory: .github/scripts
run: |
# Sweep resources tagged with either the Lambda-applied
# `map-migrated` key or the pre-applied `e2e-run-id` key (PR #7.a
# switched E2E pre-tagging from map-migrated to e2e-run-id so
# verify_tags actually tests the Lambda). Empty --tag-value means
# "match any value" — covers every past run's per-run MPE/run-id tag.
python3 teardown.py \
--all \
--sweep-keys map-migrated,e2e-run-id \
--regions ap-northeast-2,us-east-1,us-west-2
continue-on-error: true
- name: Delete stale CF stacks — single account (ap-northeast-2)
if: steps.guard_single.outputs.skip != 'true'
run: |
for stack in $(aws cloudformation list-stacks \
--stack-status-filter CREATE_COMPLETE UPDATE_COMPLETE \
ROLLBACK_COMPLETE UPDATE_ROLLBACK_COMPLETE \
CREATE_FAILED UPDATE_FAILED ROLLBACK_FAILED \
--query 'StackSummaries[?starts_with(StackName, `map-auto-tagger-e2e-pr`)].StackName' \
--output text \
--region ap-northeast-2 2>/dev/null); do
echo "Deleting stack: $stack"
aws cloudformation delete-stack --stack-name "$stack" --region ap-northeast-2 || true
done
continue-on-error: true
- name: Delete stale CF stacks — single account (us-east-1)
if: steps.guard_single.outputs.skip != 'true'
run: |
for stack in $(aws cloudformation list-stacks \
--stack-status-filter CREATE_COMPLETE UPDATE_COMPLETE \
ROLLBACK_COMPLETE UPDATE_ROLLBACK_COMPLETE \
CREATE_FAILED UPDATE_FAILED ROLLBACK_FAILED \
--query 'StackSummaries[?starts_with(StackName, `map-auto-tagger-e2e-pr`)].StackName' \
--output text \
--region us-east-1 2>/dev/null); do
echo "Deleting stack: $stack"
aws cloudformation delete-stack --stack-name "$stack" --region us-east-1 || true
done
continue-on-error: true
- name: Delete stale CF stacks — single account (us-west-2)
if: steps.guard_single.outputs.skip != 'true'
run: |
for stack in $(aws cloudformation list-stacks \
--stack-status-filter CREATE_COMPLETE UPDATE_COMPLETE \
ROLLBACK_COMPLETE UPDATE_ROLLBACK_COMPLETE \
CREATE_FAILED UPDATE_FAILED ROLLBACK_FAILED \
--query 'StackSummaries[?starts_with(StackName, `map-auto-tagger-e2e-pr`)].StackName' \
--output text \
--region us-west-2 2>/dev/null); do
echo "Deleting stack: $stack"
aws cloudformation delete-stack --stack-name "$stack" --region us-west-2 || true
done
continue-on-error: true
# ── Management account (StackSets live here) ──────────────────────────
- name: Assume role — management account
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::${{ secrets.AWS_MGMT_ACCOUNT_ID }}:role/GitHubActionsE2ERole
aws-region: ap-northeast-2
continue-on-error: true
- name: Guard — detect in-flight E2E (management account)
id: guard_mgmt
run: |
if python3 .github/scripts/nightly_cleanup_guard.py check-account; then
echo "skip=false" >> $GITHUB_OUTPUT
else
echo "skip=true" >> $GITHUB_OUTPUT
fi
continue-on-error: true
- name: Delete tagged resources — management account
if: steps.guard_mgmt.outputs.skip != 'true'
working-directory: .github/scripts
run: |
# Sweep resources tagged with either the Lambda-applied
# `map-migrated` key or the pre-applied `e2e-run-id` key (PR #7.a
# switched E2E pre-tagging from map-migrated to e2e-run-id so
# verify_tags actually tests the Lambda). Empty --tag-value means
# "match any value" — covers every past run's per-run MPE/run-id tag.
python3 teardown.py \
--all \
--sweep-keys map-migrated,e2e-run-id \
--regions ap-northeast-2,us-east-1,us-west-2
continue-on-error: true
- name: Delete stale StackSets — management account
if: steps.guard_mgmt.outputs.skip != 'true'
run: |
for ss in $(aws cloudformation list-stack-sets \
--status ACTIVE \
--query 'Summaries[?starts_with(StackSetName, `map-auto-tagger-e2e-pr`)].StackSetName' \
--output text \
--region ap-northeast-2 2>/dev/null); do
echo "Removing all instances from StackSet: $ss"
# Delete all stack instances first (required before deleting the StackSet)
aws cloudformation delete-stack-instances \
--stack-set-name "$ss" \
--regions ap-northeast-2 \
--no-retain-stacks \
--deployment-targets 'OrganizationalUnitIds=[]' \
--region ap-northeast-2 2>/dev/null || \
aws cloudformation delete-stack-instances \
--stack-set-name "$ss" \
--accounts \
"${{ secrets.AWS_LINKED1_ACCOUNT_ID }}" \
"${{ secrets.AWS_LINKED2_ACCOUNT_ID }}" \
"${{ secrets.AWS_LINKED3_ACCOUNT_ID }}" \
"${{ secrets.AWS_LINKED4_ACCOUNT_ID }}" \
"${{ secrets.AWS_LINKED5_ACCOUNT_ID }}" \
--regions ap-northeast-2 \
--no-retain-stacks \
--region ap-northeast-2 2>/dev/null || true
sleep 5
echo "Deleting StackSet: $ss"
aws cloudformation delete-stack-set --stack-set-name "$ss" --region ap-northeast-2 || true
done
continue-on-error: true
# ── Linked account 1 ──────────────────────────────────────────────────
- name: Assume role — linked account 1
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::${{ secrets.AWS_LINKED1_ACCOUNT_ID }}:role/GitHubActionsE2ERole
aws-region: ap-northeast-2
continue-on-error: true
- name: Guard — detect in-flight E2E (linked account 1)
id: guard_l1
run: |
if python3 .github/scripts/nightly_cleanup_guard.py check-account; then
echo "skip=false" >> $GITHUB_OUTPUT
else
echo "skip=true" >> $GITHUB_OUTPUT
fi
continue-on-error: true
- name: Delete tagged resources — linked account 1
if: steps.guard_l1.outputs.skip != 'true'
working-directory: .github/scripts
run: |
# Sweep resources tagged with either the Lambda-applied
# `map-migrated` key or the pre-applied `e2e-run-id` key (PR #7.a
# switched E2E pre-tagging from map-migrated to e2e-run-id so
# verify_tags actually tests the Lambda). Empty --tag-value means
# "match any value" — covers every past run's per-run MPE/run-id tag.
python3 teardown.py \
--all \
--sweep-keys map-migrated,e2e-run-id \
--regions ap-northeast-2,us-east-1,us-west-2
continue-on-error: true
- name: Sweep orphaned map-auto-tagger IAM roles — linked account 1
if: steps.guard_l1.outputs.skip != 'true'
run: python3 .github/scripts/sweep_iam_roles.py
continue-on-error: true
# ── Linked account 2 ──────────────────────────────────────────────────
- name: Assume role — linked account 2
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::${{ secrets.AWS_LINKED2_ACCOUNT_ID }}:role/GitHubActionsE2ERole
aws-region: ap-northeast-2
continue-on-error: true
- name: Guard — detect in-flight E2E (linked account 2)
id: guard_l2
run: |
if python3 .github/scripts/nightly_cleanup_guard.py check-account; then
echo "skip=false" >> $GITHUB_OUTPUT
else
echo "skip=true" >> $GITHUB_OUTPUT
fi
continue-on-error: true
- name: Delete tagged resources — linked account 2
if: steps.guard_l2.outputs.skip != 'true'
working-directory: .github/scripts
run: |
# Sweep resources tagged with either the Lambda-applied
# `map-migrated` key or the pre-applied `e2e-run-id` key (PR #7.a
# switched E2E pre-tagging from map-migrated to e2e-run-id so
# verify_tags actually tests the Lambda). Empty --tag-value means
# "match any value" — covers every past run's per-run MPE/run-id tag.
python3 teardown.py \
--all \
--sweep-keys map-migrated,e2e-run-id \
--regions ap-northeast-2,us-east-1,us-west-2
continue-on-error: true
- name: Sweep orphaned map-auto-tagger IAM roles — linked account 2
if: steps.guard_l2.outputs.skip != 'true'
run: python3 .github/scripts/sweep_iam_roles.py
continue-on-error: true
# ── Linked account 3 ──────────────────────────────────────────────────
- name: Assume role — linked account 3
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::${{ secrets.AWS_LINKED3_ACCOUNT_ID }}:role/GitHubActionsE2ERole
aws-region: ap-northeast-2
continue-on-error: true
- name: Guard — detect in-flight E2E (linked account 3)
id: guard_l3
run: |
if python3 .github/scripts/nightly_cleanup_guard.py check-account; then
echo "skip=false" >> $GITHUB_OUTPUT
else
echo "skip=true" >> $GITHUB_OUTPUT
fi
continue-on-error: true
- name: Delete tagged resources — linked account 3
if: steps.guard_l3.outputs.skip != 'true'
working-directory: .github/scripts
run: |
# Sweep resources tagged with either the Lambda-applied
# `map-migrated` key or the pre-applied `e2e-run-id` key (PR #7.a
# switched E2E pre-tagging from map-migrated to e2e-run-id so
# verify_tags actually tests the Lambda). Empty --tag-value means
# "match any value" — covers every past run's per-run MPE/run-id tag.
python3 teardown.py \
--all \
--sweep-keys map-migrated,e2e-run-id \
--regions ap-northeast-2,us-east-1,us-west-2
continue-on-error: true
- name: Sweep orphaned map-auto-tagger IAM roles — linked account 3
if: steps.guard_l3.outputs.skip != 'true'
run: python3 .github/scripts/sweep_iam_roles.py
continue-on-error: true
# ── Linked account 4 ──────────────────────────────────────────────────
- name: Assume role — linked account 4
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::${{ secrets.AWS_LINKED4_ACCOUNT_ID }}:role/GitHubActionsE2ERole
aws-region: ap-northeast-2
continue-on-error: true
- name: Guard — detect in-flight E2E (linked account 4)
id: guard_l4
run: |
if python3 .github/scripts/nightly_cleanup_guard.py check-account; then
echo "skip=false" >> $GITHUB_OUTPUT
else
echo "skip=true" >> $GITHUB_OUTPUT
fi
continue-on-error: true
- name: Delete tagged resources — linked account 4
if: steps.guard_l4.outputs.skip != 'true'
working-directory: .github/scripts
run: |
# Sweep resources tagged with either the Lambda-applied
# `map-migrated` key or the pre-applied `e2e-run-id` key (PR #7.a
# switched E2E pre-tagging from map-migrated to e2e-run-id so
# verify_tags actually tests the Lambda). Empty --tag-value means
# "match any value" — covers every past run's per-run MPE/run-id tag.
python3 teardown.py \
--all \
--sweep-keys map-migrated,e2e-run-id \
--regions ap-northeast-2,us-east-1,us-west-2
continue-on-error: true
- name: Sweep orphaned map-auto-tagger IAM roles — linked account 4
if: steps.guard_l4.outputs.skip != 'true'
run: python3 .github/scripts/sweep_iam_roles.py
continue-on-error: true
# ── Linked account 5 ──────────────────────────────────────────────────
- name: Assume role — linked account 5
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::${{ secrets.AWS_LINKED5_ACCOUNT_ID }}:role/GitHubActionsE2ERole
aws-region: ap-northeast-2
continue-on-error: true
- name: Guard — detect in-flight E2E (linked account 5)
id: guard_l5
run: |
if python3 .github/scripts/nightly_cleanup_guard.py check-account; then
echo "skip=false" >> $GITHUB_OUTPUT
else
echo "skip=true" >> $GITHUB_OUTPUT
fi
continue-on-error: true
- name: Delete tagged resources — linked account 5
if: steps.guard_l5.outputs.skip != 'true'
working-directory: .github/scripts
run: |
# Sweep resources tagged with either the Lambda-applied
# `map-migrated` key or the pre-applied `e2e-run-id` key (PR #7.a
# switched E2E pre-tagging from map-migrated to e2e-run-id so
# verify_tags actually tests the Lambda). Empty --tag-value means
# "match any value" — covers every past run's per-run MPE/run-id tag.
python3 teardown.py \
--all \
--sweep-keys map-migrated,e2e-run-id \
--regions ap-northeast-2,us-east-1,us-west-2
continue-on-error: true
- name: Sweep orphaned map-auto-tagger IAM roles — linked account 5
if: steps.guard_l5.outputs.skip != 'true'
run: python3 .github/scripts/sweep_iam_roles.py
continue-on-error: true