-
Notifications
You must be signed in to change notification settings - Fork 0
67 lines (63 loc) · 2.81 KB
/
close-pr.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
name: Close Pull Request
# only trigger on pull request closed/merged events
on:
pull_request:
types: [ closed, merged ]
# only one PR lifecycle workflow at a time (open/close)
concurrency:
group: ${{ github.ref }}
# These permissions are needed to interact with GitHub's OIDC Token endpoint.
permissions:
id-token: write
contents: read
jobs:
call-metadata-workflow:
uses: ./.github/workflows/meta.yml
secrets: inherit
with:
pr-prefix: "ntno-mkdocs-demo-ci-pr-"
destroy-pr-environment:
needs: [call-metadata-workflow]
runs-on: ubuntu-latest
steps:
- name: Configure AWS credentials
uses: aws-actions/[email protected]
with:
aws-region: us-east-1
role-to-assume: ${{ secrets.INTEGRATION_IAM_ROLE_ARN }}
- name: Set Env Vars
id: set-env-vars
run: |
echo "stack-name=${{ needs.call-metadata-workflow.outputs.pr-env }}" >> $GITHUB_ENV
echo "bucket-name=${{ needs.call-metadata-workflow.outputs.pr-env }}" >> $GITHUB_ENV
- name: Empty Bucket
id: empty-bucket
shell: bash {0}
run: |
checkBucketStatus="aws s3api head-bucket --bucket ${{ env.bucket-name }} 2>&1"
bucketStatus=$(eval "${checkBucketStatus}")
if echo "${bucketStatus}" | grep 'Not Found'; then
echo "${{ env.bucket-name }} bucket does not exist";
elif echo "${bucketStatus}" | grep 'Forbidden'; then
echo "${{ env.bucket-name }} bucket exists but not owned"
elif echo "${bucketStatus}" | grep 'Bad Request'; then
echo "${{ env.bucket-name }} bucket name specified is less than 3 or greater than 63 characters"
else
echo "forcing non-empty bucket delete: s3://${{ env.bucket-name }}"
aws s3 rb "s3://${{ env.bucket-name }}" --force
fi
- name: Delete PR Environment
shell: bash {0}
run: |
checkStackStatus="aws cloudformation describe-stacks --stack-name ${{ env.stack-name }} 2>&1"
stackStatus=$(eval "${checkStackStatus}")
if echo "${stackStatus}" | grep 'does not exist'; then
echo "${{ env.stack-name }} stack does not exist";
echo "::notice title=Skipped PR Environment Destroy::cloudformation stack ${{ env.stack-name }} does not exist"
else
aws cloudformation wait stack-create-complete --stack-name "${{ env.stack-name }}"
echo "deleting cloudformation stack: ${{ env.stack-name }}"
aws cloudformation delete-stack --stack-name "${{ env.stack-name }}"
aws cloudformation wait stack-delete-complete --stack-name "${{ env.stack-name }}"
echo "::notice title=Destroyed PR Environment::deleted cloudformation stack ${{ env.stack-name }}"
fi