|
| 1 | +name: Cleanup Deployment Job |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + runner_os: |
| 7 | + description: 'Runner OS (ubuntu-latest or windows-latest)' |
| 8 | + required: true |
| 9 | + type: string |
| 10 | + trigger_type: |
| 11 | + description: 'Trigger type (workflow_dispatch, pull_request, schedule)' |
| 12 | + required: true |
| 13 | + type: string |
| 14 | + cleanup_resources: |
| 15 | + description: 'Cleanup Deployed Resources' |
| 16 | + required: false |
| 17 | + default: false |
| 18 | + type: boolean |
| 19 | + existing_webapp_url: |
| 20 | + description: 'Existing Container WebApp URL (Skips Deployment)' |
| 21 | + required: false |
| 22 | + default: '' |
| 23 | + type: string |
| 24 | + RESOURCE_GROUP_NAME: |
| 25 | + description: 'Resource Group Name to cleanup' |
| 26 | + required: true |
| 27 | + type: string |
| 28 | + AZURE_LOCATION: |
| 29 | + description: 'Azure Location' |
| 30 | + required: true |
| 31 | + type: string |
| 32 | + AZURE_ENV_OPENAI_LOCATION: |
| 33 | + description: 'Azure OpenAI Location' |
| 34 | + required: true |
| 35 | + type: string |
| 36 | + ENV_NAME: |
| 37 | + description: 'Environment Name' |
| 38 | + required: true |
| 39 | + type: string |
| 40 | + IMAGE_TAG: |
| 41 | + description: 'Docker Image Tag' |
| 42 | + required: true |
| 43 | + type: string |
| 44 | + secrets: |
| 45 | + AZURE_CLIENT_ID: |
| 46 | + required: true |
| 47 | + AZURE_CLIENT_SECRET: |
| 48 | + required: true |
| 49 | + AZURE_TENANT_ID: |
| 50 | + required: true |
| 51 | + AZURE_SUBSCRIPTION_ID: |
| 52 | + required: true |
| 53 | + |
| 54 | +jobs: |
| 55 | + cleanup-deployment: |
| 56 | + if: inputs.existing_webapp_url == '' && (inputs.trigger_type != 'workflow_dispatch' || inputs.cleanup_resources == true || inputs.cleanup_resources == null) |
| 57 | + runs-on: ${{ inputs.runner_os }} |
| 58 | + continue-on-error: true |
| 59 | + env: |
| 60 | + RESOURCE_GROUP_NAME: ${{ inputs.RESOURCE_GROUP_NAME }} |
| 61 | + AZURE_LOCATION: ${{ inputs.AZURE_LOCATION }} |
| 62 | + AZURE_ENV_OPENAI_LOCATION: ${{ inputs.AZURE_ENV_OPENAI_LOCATION }} |
| 63 | + ENV_NAME: ${{ inputs.ENV_NAME }} |
| 64 | + IMAGE_TAG: ${{ inputs.IMAGE_TAG }} |
| 65 | + steps: |
| 66 | + - name: Setup Azure CLI |
| 67 | + shell: bash |
| 68 | + run: | |
| 69 | + if [[ "${{ runner.os }}" == "Linux" ]]; then |
| 70 | + curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash |
| 71 | + fi |
| 72 | + az --version |
| 73 | +
|
| 74 | + - name: Login to Azure |
| 75 | + shell: bash |
| 76 | + run: | |
| 77 | + az login --service-principal -u ${{ secrets.AZURE_CLIENT_ID }} -p ${{ secrets.AZURE_CLIENT_SECRET }} --tenant ${{ secrets.AZURE_TENANT_ID }} |
| 78 | + az account set --subscription ${{ secrets.AZURE_SUBSCRIPTION_ID }} |
| 79 | +
|
| 80 | + - name: Delete Resource Group (Optimized Cleanup) |
| 81 | + id: delete_rg |
| 82 | + shell: bash |
| 83 | + run: | |
| 84 | + set -e |
| 85 | + echo "🗑️ Starting optimized resource cleanup..." |
| 86 | + echo "Deleting resource group: ${{ env.RESOURCE_GROUP_NAME }}" |
| 87 | + |
| 88 | + az group delete \ |
| 89 | + --name "${{ env.RESOURCE_GROUP_NAME }}" \ |
| 90 | + --yes \ |
| 91 | + --no-wait |
| 92 | + |
| 93 | + echo "✅ Resource group deletion initiated (running asynchronously)" |
| 94 | + echo "Note: Resources will be cleaned up in the background" |
| 95 | +
|
| 96 | + - name: Logout from Azure |
| 97 | + if: always() |
| 98 | + shell: bash |
| 99 | + run: | |
| 100 | + azd auth logout || true |
| 101 | + az logout || echo "Warning: Failed to logout from Azure CLI" |
| 102 | + echo "Logged out from Azure." |
| 103 | + |
| 104 | + - name: Generate Cleanup Job Summary |
| 105 | + if: always() |
| 106 | + shell: bash |
| 107 | + run: | |
| 108 | + echo "## 🧹 Cleanup Job Summary" >> $GITHUB_STEP_SUMMARY |
| 109 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 110 | + echo "| Field | Value |" >> $GITHUB_STEP_SUMMARY |
| 111 | + echo "|-------|--------|" >> $GITHUB_STEP_SUMMARY |
| 112 | + echo "| **Resouce Group deletion Status** | ${{ steps.delete_rg.outcome == 'success' && '✅ Initiated' || '❌ Failed' }} |" >> $GITHUB_STEP_SUMMARY |
| 113 | + echo "| **Resource Group** | \`${{ env.RESOURCE_GROUP_NAME }}\` |" >> $GITHUB_STEP_SUMMARY |
| 114 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 115 | + if [[ "${{ steps.delete_rg.outcome }}" == "success" ]]; then |
| 116 | + echo "### ✅ Cleanup Details" >> $GITHUB_STEP_SUMMARY |
| 117 | + echo "- Successfully initiated deletion for Resource Group \`${{ env.RESOURCE_GROUP_NAME }}\`" >> $GITHUB_STEP_SUMMARY |
| 118 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 119 | + else |
| 120 | + echo "### ❌ Cleanup Failed" >> $GITHUB_STEP_SUMMARY |
| 121 | + echo "- Cleanup process encountered an error" >> $GITHUB_STEP_SUMMARY |
| 122 | + echo "- Manual cleanup may be required for:" >> $GITHUB_STEP_SUMMARY |
| 123 | + echo " - Resource Group: \`${{ env.RESOURCE_GROUP_NAME }}\`" >> $GITHUB_STEP_SUMMARY |
| 124 | + echo "- Check the cleanup-deployment job logs for detailed error information" >> $GITHUB_STEP_SUMMARY |
| 125 | + fi |
0 commit comments