|
| 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 |
0 commit comments