|
| 1 | +name: Validate Deployment |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_run: |
| 5 | + workflows: ["Build Docker and Optional Push"] |
| 6 | + types: |
| 7 | + - completed |
| 8 | + branches: |
| 9 | + - macae-v2 |
| 10 | + # - hotfix |
| 11 | + # - dev |
| 12 | + schedule: |
| 13 | + - cron: "0 11,23 * * *" # Runs at 11:00 AM and 11:00 PM GMT |
| 14 | + workflow_dispatch: #Allow manual triggering |
| 15 | +env: |
| 16 | + GPT_MIN_CAPACITY: 150 |
| 17 | + BRANCH_NAME: ${{ github.head_ref || github.ref_name }} |
| 18 | + |
| 19 | +jobs: |
| 20 | + deploy: |
| 21 | + runs-on: ubuntu-latest |
| 22 | + outputs: |
| 23 | + RESOURCE_GROUP_NAME: ${{ steps.check_create_rg.outputs.RESOURCE_GROUP_NAME }} |
| 24 | + WEBAPP_URL: ${{ steps.get_output.outputs.WEBAPP_URL }} |
| 25 | + DEPLOYMENT_SUCCESS: ${{ steps.deployment_status.outputs.SUCCESS }} |
| 26 | + MACAE_URL_API: ${{ steps.get_backend_url.outputs.MACAE_URL_API }} |
| 27 | + CONTAINER_APP: ${{steps.get_backend_url.outputs.CONTAINER_APP}} |
| 28 | + steps: |
| 29 | + - name: Checkout Code |
| 30 | + uses: actions/checkout@v3 |
| 31 | + |
| 32 | + - name: Run Quota Check |
| 33 | + id: quota-check |
| 34 | + run: | |
| 35 | + export AZURE_CLIENT_ID=${{ secrets.AZURE_CLIENT_ID }} |
| 36 | + export AZURE_TENANT_ID=${{ secrets.AZURE_TENANT_ID }} |
| 37 | + export AZURE_CLIENT_SECRET=${{ secrets.AZURE_CLIENT_SECRET }} |
| 38 | + export AZURE_SUBSCRIPTION_ID="${{ secrets.AZURE_SUBSCRIPTION_ID }}" |
| 39 | + export GPT_MIN_CAPACITY="150" |
| 40 | + export AZURE_REGIONS="${{ vars.AZURE_REGIONS }}" |
| 41 | +
|
| 42 | + chmod +x infra/scripts/checkquota.sh |
| 43 | + if ! infra/scripts/checkquota.sh; then |
| 44 | + # If quota check fails due to insufficient quota, set the flag |
| 45 | + if grep -q "No region with sufficient quota found" infra/scripts/checkquota.sh; then |
| 46 | + echo "QUOTA_FAILED=true" >> $GITHUB_ENV |
| 47 | + fi |
| 48 | + exit 1 # Fail the pipeline if any other failure occurs |
| 49 | + fi |
| 50 | +
|
| 51 | + - name: Send Notification on Quota Failure |
| 52 | + if: env.QUOTA_FAILED == 'true' |
| 53 | + run: | |
| 54 | + RUN_URL="https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}" |
| 55 | + EMAIL_BODY=$(cat <<EOF |
| 56 | + { |
| 57 | + "body": "<p>Dear Team,</p><p>The quota check has failed, and the pipeline cannot proceed.</p><p><strong>Build URL:</strong> ${RUN_URL}</p><p>Please take necessary action.</p><p>Best regards,<br>Your Automation Team</p>" |
| 58 | + } |
| 59 | + EOF |
| 60 | + ) |
| 61 | +
|
| 62 | + curl -X POST "${{ secrets.AUTO_LOGIC_APP_URL }}" \ |
| 63 | + -H "Content-Type: application/json" \ |
| 64 | + -d "$EMAIL_BODY" || echo "Failed to send notification" |
| 65 | +
|
| 66 | + - name: Fail Pipeline if Quota Check Fails |
| 67 | + if: env.QUOTA_FAILED == 'true' |
| 68 | + run: exit 1 |
| 69 | + |
| 70 | + - name: Set Deployment Region |
| 71 | + run: | |
| 72 | + echo "Selected Region: $VALID_REGION" |
| 73 | + echo "AZURE_LOCATION=$VALID_REGION" >> $GITHUB_ENV |
| 74 | +
|
| 75 | + - name: Setup Azure CLI |
| 76 | + run: | |
| 77 | + curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash |
| 78 | + az --version # Verify installation |
| 79 | +
|
| 80 | + - name: Login to Azure |
| 81 | + run: | |
| 82 | + az login --service-principal -u ${{ secrets.AZURE_CLIENT_ID }} -p ${{ secrets.AZURE_CLIENT_SECRET }} --tenant ${{ secrets.AZURE_TENANT_ID }} |
| 83 | +
|
| 84 | + - name: Install Bicep CLI |
| 85 | + run: az bicep install |
| 86 | + |
| 87 | + - name: Generate Resource Group Name |
| 88 | + id: generate_rg_name |
| 89 | + run: | |
| 90 | + ACCL_NAME="macae" |
| 91 | + SHORT_UUID=$(uuidgen | cut -d'-' -f1) |
| 92 | + UNIQUE_RG_NAME="arg-${ACCL_NAME}-${SHORT_UUID}" |
| 93 | + echo "RESOURCE_GROUP_NAME=${UNIQUE_RG_NAME}" >> $GITHUB_ENV |
| 94 | + echo "Generated Resource_GROUP_PREFIX: ${UNIQUE_RG_NAME}" |
| 95 | +
|
| 96 | + - name: Check and Create Resource Group |
| 97 | + id: check_create_rg |
| 98 | + run: | |
| 99 | + set -e |
| 100 | + rg_exists=$(az group exists --name ${{ env.RESOURCE_GROUP_NAME }}) |
| 101 | + if [ "$rg_exists" = "false" ]; then |
| 102 | + az group create --name ${{ env.RESOURCE_GROUP_NAME }} --location ${{ env.AZURE_LOCATION }} |
| 103 | + fi |
| 104 | + echo "RESOURCE_GROUP_NAME=${{ env.RESOURCE_GROUP_NAME }}" >> $GITHUB_OUTPUT |
| 105 | +
|
| 106 | + - name: Generate Unique Solution Prefix |
| 107 | + id: generate_solution_prefix |
| 108 | + run: | |
| 109 | + COMMON_PART="macae" |
| 110 | + TIMESTAMP=$(date +%s) |
| 111 | + UPDATED_TIMESTAMP=$(echo $TIMESTAMP | tail -c 6) |
| 112 | + UNIQUE_SOLUTION_PREFIX="${COMMON_PART}${UPDATED_TIMESTAMP}" |
| 113 | + echo "SOLUTION_PREFIX=${UNIQUE_SOLUTION_PREFIX}" >> $GITHUB_ENV |
| 114 | +
|
| 115 | + - name: Deploy Bicep Template |
| 116 | + id: deploy |
| 117 | + run: | |
| 118 | + if [[ "${{ env.BRANCH_NAME }}" == "macae-v2" ]]; then |
| 119 | + IMAGE_TAG="latest" |
| 120 | + # elif [[ "${{ env.BRANCH_NAME }}" == "dev" ]]; then |
| 121 | + # IMAGE_TAG="dev" |
| 122 | + # elif [[ "${{ env.BRANCH_NAME }}" == "hotfix" ]]; then |
| 123 | + # IMAGE_TAG="hotfix" |
| 124 | + # else |
| 125 | + # IMAGE_TAG="latest" |
| 126 | + fi |
| 127 | +
|
| 128 | + az deployment group create \ |
| 129 | + --resource-group ${{ env.RESOURCE_GROUP_NAME }} \ |
| 130 | + --template-file infra/main.bicep \ |
| 131 | + --parameters \ |
| 132 | + solutionName=${{ env.SOLUTION_PREFIX }} \ |
| 133 | + location="${{ env.AZURE_LOCATION }}" \ |
| 134 | + gptModelDeploymentType="GlobalStandard" \ |
| 135 | + gptModelName="gpt-4o" \ |
| 136 | + gptModelVersion="2024-08-06" \ |
| 137 | + backendContainerImageTag="${IMAGE_TAG}" \ |
| 138 | + frontendContainerImageTag="${IMAGE_TAG}" \ |
| 139 | + azureAiServiceLocation='${{ env.AZURE_LOCATION }}' \ |
| 140 | + gptModelCapacity=150 \ |
| 141 | + createdBy="Pipeline" \ |
| 142 | + --output json |
| 143 | +
|
| 144 | + - name: Extract Web App and API App URLs |
| 145 | + id: get_output |
| 146 | + run: | |
| 147 | + WEBAPP_NAMES=$(az webapp list --resource-group ${{ env.RESOURCE_GROUP_NAME }} --query "[].name" -o tsv) |
| 148 | + for NAME in $WEBAPP_NAMES; do |
| 149 | + if [[ $NAME == app-* ]]; then |
| 150 | + WEBAPP_URL="https://${NAME}.azurewebsites.net" |
| 151 | + echo "WEBAPP_URL=$WEBAPP_URL" >> $GITHUB_OUTPUT |
| 152 | + fi |
| 153 | + done |
| 154 | +
|
| 155 | + - name: Get Container App Backend URL |
| 156 | + id: get_backend_url |
| 157 | + run: | |
| 158 | + CONTAINER_APP_NAME=$(az containerapp list \ |
| 159 | + --resource-group ${{ env.RESOURCE_GROUP_NAME }} \ |
| 160 | + --query "[0].name" -o tsv) |
| 161 | +
|
| 162 | + MACAE_URL_API=$(az containerapp show \ |
| 163 | + --name "$CONTAINER_APP_NAME" \ |
| 164 | + --resource-group ${{ env.RESOURCE_GROUP_NAME }} \ |
| 165 | + --query "properties.configuration.ingress.fqdn" -o tsv) |
| 166 | +
|
| 167 | + echo "MACAE_URL_API=https://${MACAE_URL_API}" >> $GITHUB_OUTPUT |
| 168 | + echo "CONTAINER_APP=${CONTAINER_APP_NAME}" >> $GITHUB_OUTPUT |
| 169 | +
|
| 170 | + - name: Set Deployment Status |
| 171 | + id: deployment_status |
| 172 | + if: always() |
| 173 | + run: | |
| 174 | + if [ "${{ job.status }}" == "success" ]; then |
| 175 | + echo "SUCCESS=true" >> $GITHUB_OUTPUT |
| 176 | + else |
| 177 | + echo "SUCCESS=false" >> $GITHUB_OUTPUT |
| 178 | + fi |
| 179 | +
|
| 180 | + e2e-test: |
| 181 | + needs: deploy |
| 182 | + if: needs.deploy.outputs.DEPLOYMENT_SUCCESS == 'true' |
| 183 | + uses: ./.github/workflows/test-automation.yml |
| 184 | + with: |
| 185 | + MACAE_WEB_URL: ${{ needs.deploy.outputs.WEBAPP_URL }} |
| 186 | + MACAE_URL_API: ${{ needs.deploy.outputs.MACAE_URL_API }} |
| 187 | + MACAE_RG: ${{ needs.deploy.outputs.RESOURCE_GROUP_NAME }} |
| 188 | + MACAE_CONTAINER_APP: ${{ needs.deploy.outputs.CONTAINER_APP }} |
| 189 | + secrets: inherit |
| 190 | + |
| 191 | + cleanup-deployment: |
| 192 | + if: always() && needs.deploy.outputs.RESOURCE_GROUP_NAME != '' |
| 193 | + needs: [deploy, e2e-test] |
| 194 | + runs-on: ubuntu-latest |
| 195 | + env: |
| 196 | + RESOURCE_GROUP_NAME: ${{ needs.deploy.outputs.RESOURCE_GROUP_NAME }} |
| 197 | + steps: |
| 198 | + - name: Setup Azure CLI |
| 199 | + run: | |
| 200 | + curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash |
| 201 | + az --version |
| 202 | + - name: Login to Azure |
| 203 | + run: | |
| 204 | + az login --service-principal -u ${{ secrets.AZURE_CLIENT_ID }} -p ${{ secrets.AZURE_CLIENT_SECRET }} --tenant ${{ secrets.AZURE_TENANT_ID }} |
| 205 | + az account set --subscription "${{ secrets.AZURE_SUBSCRIPTION_ID }}" |
| 206 | +
|
| 207 | + - name: Extract AI Services and Key Vault Names |
| 208 | + if: always() |
| 209 | + run: | |
| 210 | + echo "Fetching AI Services and Key Vault names before deletion..." |
| 211 | +
|
| 212 | + # Get Key Vault name |
| 213 | + KEYVAULT_NAME=$(az resource list --resource-group "${{ env.RESOURCE_GROUP_NAME }}" --resource-type "Microsoft.KeyVault/vaults" --query "[].name" -o tsv) |
| 214 | + echo "Detected Key Vault: $KEYVAULT_NAME" |
| 215 | + echo "KEYVAULT_NAME=$KEYVAULT_NAME" >> $GITHUB_ENV |
| 216 | + # Extract AI Services names |
| 217 | + echo "Fetching AI Services..." |
| 218 | + AI_SERVICES=$(az resource list --resource-group '${{ env.RESOURCE_GROUP_NAME }}' --resource-type "Microsoft.CognitiveServices/accounts" --query "[].name" -o tsv) |
| 219 | + # Flatten newline-separated values to space-separated |
| 220 | + AI_SERVICES=$(echo "$AI_SERVICES" | paste -sd ' ' -) |
| 221 | + echo "Detected AI Services: $AI_SERVICES" |
| 222 | + echo "AI_SERVICES=$AI_SERVICES" >> $GITHUB_ENV |
| 223 | +
|
| 224 | + - name: Get OpenAI Resource from Resource Group |
| 225 | + id: get_openai_resource |
| 226 | + run: | |
| 227 | +
|
| 228 | + set -e |
| 229 | + echo "Fetching OpenAI resource from resource group ${{ env.RESOURCE_GROUP_NAME }}..." |
| 230 | +
|
| 231 | + # Run the az resource list command to get the OpenAI resource name |
| 232 | + openai_resource_name=$(az resource list --resource-group ${{ env.RESOURCE_GROUP_NAME }} --resource-type "Microsoft.CognitiveServices/accounts" --query "[0].name" -o tsv) |
| 233 | +
|
| 234 | + if [ -z "$openai_resource_name" ]; then |
| 235 | + echo "No OpenAI resource found in resource group ${{ env.RESOURCE_GROUP_NAME }}." |
| 236 | + exit 0 |
| 237 | + else |
| 238 | + echo "OPENAI_RESOURCE_NAME=${openai_resource_name}" >> $GITHUB_ENV |
| 239 | + echo "OpenAI resource name: ${openai_resource_name}" |
| 240 | + fi |
| 241 | +
|
| 242 | + - name: Delete Bicep Deployment |
| 243 | + if: always() |
| 244 | + run: | |
| 245 | + set -e |
| 246 | + echo "Checking if resource group exists..." |
| 247 | + rg_exists=$(az group exists --name ${{ env.RESOURCE_GROUP_NAME }}) |
| 248 | + if [ "$rg_exists" = "true" ]; then |
| 249 | + echo "Resource group exist. Cleaning..." |
| 250 | + az group delete \ |
| 251 | + --name ${{ env.RESOURCE_GROUP_NAME }} \ |
| 252 | + --yes \ |
| 253 | + --no-wait |
| 254 | + echo "Resource group deleted... ${{ env.RESOURCE_GROUP_NAME }}" |
| 255 | + else |
| 256 | + echo "Resource group does not exists." |
| 257 | + fi |
| 258 | +
|
| 259 | + - name: Wait for resource deletion to complete |
| 260 | + run: | |
| 261 | +
|
| 262 | + # Add resources to the array |
| 263 | + resources_to_check=("${{ env.OPENAI_RESOURCE_NAME }}") |
| 264 | +
|
| 265 | + echo "List of resources to check: ${resources_to_check[@]}" |
| 266 | +
|
| 267 | + # Maximum number of retries |
| 268 | + max_retries=3 |
| 269 | +
|
| 270 | + # Retry intervals in seconds (30, 60, 120) |
| 271 | + retry_intervals=(30 60 120) |
| 272 | +
|
| 273 | + # Retry mechanism to check resources |
| 274 | + retries=0 |
| 275 | + while true; do |
| 276 | + resource_found=false |
| 277 | +
|
| 278 | + # Get the list of resources in YAML format again on each retry |
| 279 | + resource_list=$(az resource list --resource-group ${{ env.RESOURCE_GROUP_NAME }} --output yaml) |
| 280 | +
|
| 281 | + # Iterate through the resources to check |
| 282 | + for resource in "${resources_to_check[@]}"; do |
| 283 | + echo "Checking resource: $resource" |
| 284 | + if echo "$resource_list" | grep -q "name: $resource"; then |
| 285 | + echo "Resource '$resource' exists in the resource group." |
| 286 | + resource_found=true |
| 287 | + else |
| 288 | + echo "Resource '$resource' does not exist in the resource group." |
| 289 | + fi |
| 290 | + done |
| 291 | +
|
| 292 | + # If any resource exists, retry |
| 293 | + if [ "$resource_found" = true ]; then |
| 294 | + retries=$((retries + 1)) |
| 295 | + if [ "$retries" -gt "$max_retries" ]; then |
| 296 | + echo "Maximum retry attempts reached. Exiting." |
| 297 | + break |
| 298 | + else |
| 299 | + # Wait for the appropriate interval for the current retry |
| 300 | + echo "Waiting for ${retry_intervals[$retries-1]} seconds before retrying..." |
| 301 | + sleep ${retry_intervals[$retries-1]} |
| 302 | + fi |
| 303 | + else |
| 304 | + echo "No resources found. Exiting." |
| 305 | + break |
| 306 | + fi |
| 307 | + done |
| 308 | +
|
| 309 | + - name: Purging the Resources |
| 310 | + if: always() |
| 311 | + run: | |
| 312 | +
|
| 313 | + set -e |
| 314 | + echo "Azure OpenAI: ${{ env.OPENAI_RESOURCE_NAME }}" |
| 315 | +
|
| 316 | + # Purge OpenAI Resource |
| 317 | + echo "Purging the OpenAI Resource..." |
| 318 | + if ! az resource delete --ids /subscriptions/${{ secrets.AZURE_SUBSCRIPTION_ID }}/providers/Microsoft.CognitiveServices/locations/eastus/resourceGroups/${{ env.RESOURCE_GROUP_NAME }}/deletedAccounts/${{ env.OPENAI_RESOURCE_NAME }} --verbose; then |
| 319 | + echo "Failed to purge openai resource: ${{ env.OPENAI_RESOURCE_NAME }}" |
| 320 | + else |
| 321 | + echo "Purged the openai resource: ${{ env.OPENAI_RESOURCE_NAME }}" |
| 322 | + fi |
| 323 | +
|
| 324 | + echo "Resource purging completed successfully" |
| 325 | +
|
| 326 | + - name: Send Notification on Failure |
| 327 | + if: failure() |
| 328 | + run: | |
| 329 | + RUN_URL="https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}" |
| 330 | +
|
| 331 | + # Construct the email body |
| 332 | + EMAIL_BODY=$(cat <<EOF |
| 333 | + { |
| 334 | + "body": "<p>Dear Team,</p><p>We would like to inform you that the Multi-Agent-Custom-Automation-Engine-Solution-Accelerator Automation process has encountered an issue and has failed to complete successfully.</p><p><strong>Build URL:</strong> ${RUN_URL}<br> ${OUTPUT}</p><p>Please investigate the matter at your earliest convenience.</p><p>Best regards,<br>Your Automation Team</p>" |
| 335 | + } |
| 336 | + EOF |
| 337 | + ) |
| 338 | +
|
| 339 | + # Send the notification |
| 340 | + curl -X POST "${{ secrets.LOGIC_APP_URL }}" \ |
| 341 | + -H "Content-Type: application/json" \ |
| 342 | + -d "$EMAIL_BODY" || echo "Failed to send notification" |
| 343 | + - name: Logout from Azure |
| 344 | + if: always() |
| 345 | + run: | |
| 346 | + az logout |
| 347 | + echo "Logged out from Azure." |
0 commit comments