Skip to content

Clean up Flexion Azure Resources #344

Clean up Flexion Azure Resources

Clean up Flexion Azure Resources #344

name: Clean up Flexion Azure Resources
on:
delete:
workflow_dispatch:
inputs:
hashId:
description: "Hash id of target branch deployment"
default: ""
type: string
jobs:
list:
runs-on: ubuntu-latest
steps:
- uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
environment: ${{ vars.AZURE_ENVIRONMENT }}
- name: List existing branch deployments
run: |
echo "List Azure resource groups of current branch deployment"
az group list --query "[?tags.branchName].{ Name:name Branch:tags.branchName HashId:tags.branchHashId }" -o table
check:
runs-on: ubuntu-latest
if: inputs.hashId != '' || github.event.action == 'closed'
environment: "Develop"
outputs:
executeCleanup: ${{ steps.check.outputs.executeCleanup }}
targetBranchHashId: ${{ steps.check.outputs.targetBranchHashId }}
steps:
- uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
environment: ${{ vars.AZURE_ENVIRONMENT }}
- name: Genereate branch hash id
id: hash
if: github.event.action == 'closed'
run: |
hash=$(echo -n ${{ github.event.pull_request.head.ref }} | openssl sha256 | awk '{print $2}')
shortHash="${hash:0:6}"
echo "hashId=$shortHash" >> $GITHUB_OUTPUT
- name: Validate resources
id: check
run: |
target=${{ inputs.hashId == '' && steps.hash.outputs.hashId || inputs.hashId }}
echo "targetBranchHashId=$target" >> $GITHUB_OUTPUT
echo "Do resources with $target exist?"
count=$(az group list --query "length([?id.contains(@,'$target')])")
if [[ $count -eq 2 ]]; then
echo 'Expected resources found.'
echo "executeCleanup=true" >> $GITHUB_OUTPUT
else
echo 'Did not find existing Azure resources with matching hash id.'
echo "executeCleanup=false" >> $GITHUB_OUTPUT
fi
clean-up:
runs-on: ubuntu-latest
needs: [check]
if: needs.check.outputs.executeCleanup == 'true'
environment: "Develop"
env:
environment: "Develop"
steps:
- uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
environment: ${{ vars.AZURE_ENVIRONMENT }}
- uses: actions/checkout@v3
- name: Clean up e2e test database
run: |
echo "Adding suffix to database name.."
e2eDatabaseName="${{ secrets.AZ_COSMOS_DATABASE_NAME }}-e2e-${{ needs.check.outputs.targetBranchHashId }}"
az cosmosdb mongodb database delete \
--account-name ${{ secrets.AZ_COSMOS_MONGO_ACCOUNT_NAME }} \
--name "${e2eDatabaseName}" \
--resource-group ${{ secrets.AZURE_RG }} \
--yes
echo "Finished Removing Database..."
- run: ./ops/scripts/utility/az-delete-branch-resources.sh ${{ needs.check.outputs.targetBranchHashId }} false