|
| 1 | +name: Validate WAF Deployment |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + schedule: |
| 8 | + - cron: '0 11,23 * * *' # Runs at 11:00 AM and 11:00 PM GMT |
| 9 | + |
| 10 | +jobs: |
| 11 | + deploy: |
| 12 | + runs-on: ubuntu-latest |
| 13 | + steps: |
| 14 | + - name: Checkout Code |
| 15 | + uses: actions/checkout@v3 |
| 16 | + |
| 17 | + - name: Run Quota Check |
| 18 | + id: quota-check |
| 19 | + run: | |
| 20 | + export AZURE_CLIENT_ID=${{ secrets.AZURE_CLIENT_ID }} |
| 21 | + export AZURE_TENANT_ID=${{ secrets.AZURE_TENANT_ID }} |
| 22 | + export AZURE_CLIENT_SECRET=${{ secrets.AZURE_CLIENT_SECRET }} |
| 23 | + export AZURE_SUBSCRIPTION_ID="${{ secrets.AZURE_SUBSCRIPTION_ID }}" |
| 24 | + export GPT_MIN_CAPACITY="50" |
| 25 | + export AZURE_REGIONS="${{ vars.AZURE_REGIONS }}" |
| 26 | +
|
| 27 | + chmod +x infra/scripts/checkquota.sh |
| 28 | + if ! infra/scripts/checkquota.sh; then |
| 29 | + # If quota check fails due to insufficient quota, set the flag |
| 30 | + if grep -q "No region with sufficient quota found" infra/scripts/checkquota.sh; then |
| 31 | + echo "QUOTA_FAILED=true" >> $GITHUB_ENV |
| 32 | + fi |
| 33 | + exit 1 # Fail the pipeline if any other failure occurs |
| 34 | + fi |
| 35 | +
|
| 36 | + - name: Send Notification on Quota Failure |
| 37 | + if: env.QUOTA_FAILED == 'true' |
| 38 | + run: | |
| 39 | + RUN_URL="https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}" |
| 40 | + EMAIL_BODY=$(cat <<EOF |
| 41 | + { |
| 42 | + "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>" |
| 43 | + } |
| 44 | + EOF |
| 45 | + ) |
| 46 | +
|
| 47 | + curl -X POST "${{ secrets.AUTO_LOGIC_APP_URL }}" \ |
| 48 | + -H "Content-Type: application/json" \ |
| 49 | + -d "$EMAIL_BODY" || echo "Failed to send notification" |
| 50 | +
|
| 51 | + - name: Fail Pipeline if Quota Check Fails |
| 52 | + if: env.QUOTA_FAILED == 'true' |
| 53 | + run: exit 1 |
| 54 | + |
| 55 | + - name: Set Deployment Region |
| 56 | + run: | |
| 57 | + echo "Selected Region: $VALID_REGION" |
| 58 | + echo "AZURE_LOCATION=$VALID_REGION" >> $GITHUB_ENV |
| 59 | +
|
| 60 | + - name: Setup Azure CLI |
| 61 | + run: | |
| 62 | + curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash |
| 63 | + az --version # Verify installation |
| 64 | +
|
| 65 | + - name: Login to Azure |
| 66 | + run: | |
| 67 | + az login --service-principal -u ${{ secrets.AZURE_CLIENT_ID }} -p ${{ secrets.AZURE_CLIENT_SECRET }} --tenant ${{ secrets.AZURE_TENANT_ID }} |
| 68 | +
|
| 69 | + - name: Install Bicep CLI |
| 70 | + run: az bicep install |
| 71 | + |
| 72 | + - name: Generate Resource Group Name |
| 73 | + id: generate_rg_name |
| 74 | + run: | |
| 75 | + echo "Generating a unique resource group name..." |
| 76 | + ACCL_NAME="macae" # Account name as specified |
| 77 | + SHORT_UUID=$(uuidgen | cut -d'-' -f1) |
| 78 | + UNIQUE_RG_NAME="arg-${ACCL_NAME}-${SHORT_UUID}" |
| 79 | + echo "RESOURCE_GROUP_NAME=${UNIQUE_RG_NAME}" >> $GITHUB_ENV |
| 80 | + echo "Generated Resource_GROUP_PREFIX: ${UNIQUE_RG_NAME}" |
| 81 | + |
| 82 | + |
| 83 | + - name: Check and Create Resource Group |
| 84 | + id: check_create_rg |
| 85 | + run: | |
| 86 | + set -e |
| 87 | + echo "Checking if resource group exists..." |
| 88 | + rg_exists=$(az group exists --name ${{ env.RESOURCE_GROUP_NAME }}) |
| 89 | + if [ "$rg_exists" = "false" ]; then |
| 90 | + echo "Resource group does not exist. Creating..." |
| 91 | + az group create --name ${{ env.RESOURCE_GROUP_NAME }} --location ${{ env.AZURE_LOCATION }} || { echo "Error creating resource group"; exit 1; } |
| 92 | + else |
| 93 | + echo "Resource group already exists." |
| 94 | + fi |
| 95 | +
|
| 96 | +
|
| 97 | + - name: Deploy Bicep Template |
| 98 | + id: deploy |
| 99 | + run: | |
| 100 | + set -e |
| 101 | + az deployment group create \ |
| 102 | + --resource-group ${{ env.RESOURCE_GROUP_NAME }} \ |
| 103 | + --template-file infra/main.bicep \ |
| 104 | + --parameters @main.waf-aligned.bicepparam azureOpenAILocation=${{ env.AZURE_LOCATION }} |
| 105 | +
|
| 106 | +
|
| 107 | + - name: Send Notification on Failure |
| 108 | + if: failure() |
| 109 | + run: | |
| 110 | + RUN_URL="https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}" |
| 111 | + |
| 112 | + # Construct the email body |
| 113 | + EMAIL_BODY=$(cat <<EOF |
| 114 | + { |
| 115 | + "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>" |
| 116 | + } |
| 117 | + EOF |
| 118 | + ) |
| 119 | + |
| 120 | + # Send the notification |
| 121 | + curl -X POST "${{ secrets.LOGIC_APP_URL }}" \ |
| 122 | + -H "Content-Type: application/json" \ |
| 123 | + -d "$EMAIL_BODY" || echo "Failed to send notification" |
| 124 | + |
| 125 | + |
| 126 | + - name: Get OpenAI Resource from Resource Group |
| 127 | + id: get_openai_resource |
| 128 | + run: | |
| 129 | +
|
| 130 | +
|
| 131 | + set -e |
| 132 | + echo "Fetching OpenAI resource from resource group ${{ env.RESOURCE_GROUP_NAME }}..." |
| 133 | + |
| 134 | + # Run the az resource list command to get the OpenAI resource name |
| 135 | + openai_resource_name=$(az resource list --resource-group ${{ env.RESOURCE_GROUP_NAME }} --resource-type "Microsoft.CognitiveServices/accounts" --query "[0].name" -o tsv) |
| 136 | +
|
| 137 | + if [ -z "$openai_resource_name" ]; then |
| 138 | + echo "No OpenAI resource found in resource group ${{ env.RESOURCE_GROUP_NAME }}." |
| 139 | + exit 1 |
| 140 | + else |
| 141 | + echo "OPENAI_RESOURCE_NAME=${openai_resource_name}" >> $GITHUB_ENV |
| 142 | + echo "OpenAI resource name: ${openai_resource_name}" |
| 143 | + fi |
| 144 | +
|
| 145 | + |
| 146 | +
|
| 147 | + |
| 148 | + - name: Delete Bicep Deployment |
| 149 | + if: always() |
| 150 | + run: | |
| 151 | + set -e |
| 152 | + echo "Checking if resource group exists..." |
| 153 | + rg_exists=$(az group exists --name ${{ env.RESOURCE_GROUP_NAME }}) |
| 154 | + if [ "$rg_exists" = "true" ]; then |
| 155 | + echo "Resource group exist. Cleaning..." |
| 156 | + az group delete \ |
| 157 | + --name ${{ env.RESOURCE_GROUP_NAME }} \ |
| 158 | + --yes \ |
| 159 | + --no-wait |
| 160 | + echo "Resource group deleted... ${{ env.RESOURCE_GROUP_NAME }}" |
| 161 | + else |
| 162 | + echo "Resource group does not exists." |
| 163 | + fi |
| 164 | +
|
| 165 | +
|
| 166 | + - name: Wait for resource deletion to complete |
| 167 | + run: | |
| 168 | +
|
| 169 | + |
| 170 | + # Add resources to the array |
| 171 | + resources_to_check=("${{ env.OPENAI_RESOURCE_NAME }}") |
| 172 | +
|
| 173 | + echo "List of resources to check: ${resources_to_check[@]}" |
| 174 | +
|
| 175 | + # Maximum number of retries |
| 176 | + max_retries=3 |
| 177 | +
|
| 178 | + # Retry intervals in seconds (30, 60, 120) |
| 179 | + retry_intervals=(30 60 120) |
| 180 | +
|
| 181 | + # Retry mechanism to check resources |
| 182 | + retries=0 |
| 183 | + while true; do |
| 184 | + resource_found=false |
| 185 | +
|
| 186 | + # Get the list of resources in YAML format again on each retry |
| 187 | + resource_list=$(az resource list --resource-group ${{ env.RESOURCE_GROUP_NAME }} --output yaml) |
| 188 | +
|
| 189 | + # Iterate through the resources to check |
| 190 | + for resource in "${resources_to_check[@]}"; do |
| 191 | + echo "Checking resource: $resource" |
| 192 | + if echo "$resource_list" | grep -q "name: $resource"; then |
| 193 | + echo "Resource '$resource' exists in the resource group." |
| 194 | + resource_found=true |
| 195 | + else |
| 196 | + echo "Resource '$resource' does not exist in the resource group." |
| 197 | + fi |
| 198 | + done |
| 199 | +
|
| 200 | + # If any resource exists, retry |
| 201 | + if [ "$resource_found" = true ]; then |
| 202 | + retries=$((retries + 1)) |
| 203 | + if [ "$retries" -gt "$max_retries" ]; then |
| 204 | + echo "Maximum retry attempts reached. Exiting." |
| 205 | + break |
| 206 | + else |
| 207 | + # Wait for the appropriate interval for the current retry |
| 208 | + echo "Waiting for ${retry_intervals[$retries-1]} seconds before retrying..." |
| 209 | + sleep ${retry_intervals[$retries-1]} |
| 210 | + fi |
| 211 | + else |
| 212 | + echo "No resources found. Exiting." |
| 213 | + break |
| 214 | + fi |
| 215 | + done |
| 216 | +
|
| 217 | + |
| 218 | + - name: Purging the Resources |
| 219 | + if: always() |
| 220 | + run: | |
| 221 | +
|
| 222 | + set -e |
| 223 | + echo "Azure OpenAI: ${{ env.OPENAI_RESOURCE_NAME }}" |
| 224 | +
|
| 225 | + # Purge OpenAI Resource |
| 226 | + echo "Purging the OpenAI Resource..." |
| 227 | + 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 |
| 228 | + echo "Failed to purge openai resource: ${{ env.OPENAI_RESOURCE_NAME }}" |
| 229 | + else |
| 230 | + echo "Purged the openai resource: ${{ env.OPENAI_RESOURCE_NAME }}" |
| 231 | + fi |
| 232 | +
|
| 233 | + echo "Resource purging completed successfully" |
0 commit comments