Skip to content

Commit f11f057

Browse files
authored
AEA-3233 add action to delete old stacks (#91)
1 parent 139fa53 commit f11f057

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
name: 'Delete old cloudformation stacks'
2+
3+
# Controls when the action will run - in this case triggered manually
4+
on:
5+
workflow_dispatch:
6+
schedule:
7+
- cron: "0 0 * * *"
8+
9+
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
10+
jobs:
11+
# This workflow contains a single job called "combine-prs"
12+
delete-old-cloudformation-stacks:
13+
# The type of runner that the job will run on
14+
runs-on: ubuntu-latest
15+
permissions:
16+
id-token: write
17+
contents: read
18+
19+
# Steps represent a sequence of tasks that will be executed as part of the job
20+
steps:
21+
- name: Configure AWS Credentials
22+
uses: aws-actions/configure-aws-credentials@v2
23+
with:
24+
aws-region: eu-west-2
25+
role-to-assume: ${{ secrets.DEV_CLOUD_FORMATION_DEPLOY_ROLE }}
26+
role-session-name: github-actions
27+
28+
- name: delete stacks
29+
shell: bash
30+
run: |
31+
ACTIVE_STACKS=$(aws cloudformation list-stacks |
32+
jq -r '.StackSummaries[] |
33+
select ( .StackStatus != "DELETE_COMPLETE" ) |
34+
select( .StackName | capture("^PR-(sandbox-)?(\\d+)$") )
35+
| .StackName ')
36+
37+
ACTIVE_STACKS_ARRAY=( $ACTIVE_STACKS )
38+
39+
for i in "${ACTIVE_STACKS_ARRAY[@]}"
40+
do
41+
echo "Checking if stack $i has open pull request"
42+
PULL_REQUEST=${i//PR-/}
43+
PULL_REQUEST=${PULL_REQUEST//sandbox-/}
44+
echo "Checking pull request id ${PULL_REQUEST}"
45+
URL="https://api.github.com/repos/NHSDigital/prescriptionsforpatients/pulls/${PULL_REQUEST}"
46+
RESPONSE=$(curl ${URL} 2>/dev/null)
47+
STATE=$(echo ${RESPONSE} | jq -r .state)
48+
if [ "$STATE" == "closed" ]; then
49+
echo "** going to delete stack $i as state is ${STATE} **"
50+
aws cloudformation delete-stack --stack-name ${i}
51+
else
52+
echo "not going to delete stack $i as state is ${STATE}"
53+
fi
54+
done

cloudformation/ci_resources.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Resources:
5050
- "cloudformation:ExecuteChangeSet"
5151
- "cloudformation:DescribeStackEvents"
5252
- "cloudformation:GetTemplateSummary"
53+
- "cloudformation:ListStacks"
5354
Resource: "*"
5455
- PolicyName: AssumeExecutionRole
5556
PolicyDocument:

0 commit comments

Comments
 (0)