-
Notifications
You must be signed in to change notification settings - Fork 18
343 lines (319 loc) · 14.2 KB
/
ci.yml
File metadata and controls
343 lines (319 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
name: Validate Deployment
permissions:
contents: read
actions: read
id-token: write
on:
push:
branches:
- main
- dev
- demo
paths:
- 'infra/**'
- 'azure.yaml'
- 'azure_custom.yaml'
- 'scripts/**'
- '.github/workflows/ci.yml'
pull_request:
branches:
- dev
paths:
- 'infra/**'
- 'azure.yaml'
- 'azure_custom.yaml'
- 'scripts/**'
- '.github/workflows/ci.yml'
schedule:
- cron: '0 9,21 * * *' # Runs at 9:00 AM and 9:00 PM GMT
workflow_dispatch: # Allow manual triggering
env:
GPT_MIN_CAPACITY: 150
BRANCH_NAME: ${{ github.event.workflow_run.head_branch || github.head_ref || github.ref_name }}
jobs:
deploy:
runs-on: ubuntu-latest
environment: production
outputs:
RESOURCE_GROUP_NAME: ${{ steps.check_create_rg.outputs.RESOURCE_GROUP_NAME }}
DEPLOYMENT_SUCCESS: ${{ steps.deployment_status.outputs.SUCCESS }}
steps:
- name: Checkout Code
uses: actions/checkout@v4
- name: Login to Azure
uses: azure/login@v2
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
- name: Run Quota Check
id: quota-check
env:
AZURE_SUBSCRIPTION_ID: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
AZURE_REGIONS: ${{ vars.AZURE_REGIONS }}
GPT_MIN_CAPACITY: ${{ env.GPT_MIN_CAPACITY }}
run: |
chmod +x scripts/checkquota.sh
if ! scripts/checkquota.sh; then
# If quota check fails due to insufficient quota, set the flag
if grep -q "No region with sufficient quota found" scripts/checkquota.sh; then
echo "QUOTA_FAILED=true" >> $GITHUB_ENV
fi
exit 1 # Fail the pipeline if any other failure occurs
fi
- name: Send Notification on Quota Failure
if: env.QUOTA_FAILED == 'true'
run: |
RUN_URL="https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
EMAIL_BODY=$(cat <<EOF
{
"body": "<p>Dear Team,</p><p>The Container Migration quota check has failed, and the pipeline cannot proceed.</p><p><strong>Build URL:</strong> <a href=\"${RUN_URL}\">${RUN_URL}</a></p><p>Please take necessary action.</p><p>Best regards,<br>Your Automation Team</p>",
"subject": "Container Migration Deployment - Quota Check Failed"
}
EOF
)
curl -X POST "${{ secrets.LOGIC_APP_URL }}" \
-H "Content-Type: application/json" \
-d "$EMAIL_BODY" || echo "Failed to send notification"
- name: Fail Pipeline if Quota Check Fails
if: env.QUOTA_FAILED == 'true'
run: exit 1
- name: Install Bicep CLI
run: az bicep install
- name: Set Deployment Region
run: |
echo "Selected Region: $VALID_REGION"
echo "AZURE_LOCATION=$VALID_REGION" >> $GITHUB_ENV
- name: Generate Resource Group Name
id: generate_rg_name
run: |
echo "Generating a unique resource group name..."
ACCL_NAME="CM"
SHORT_UUID=$(uuidgen | cut -d'-' -f1)
UNIQUE_RG_NAME="arg-${ACCL_NAME}-${SHORT_UUID}"
echo "RESOURCE_GROUP_NAME=${UNIQUE_RG_NAME}" >> $GITHUB_ENV
echo "Generated RESOURCE_GROUP_NAME: ${UNIQUE_RG_NAME}"
- name: Check and Create Resource Group
id: check_create_rg
run: |
set -e
echo "Checking if resource group exists..."
rg_exists=$(az group exists --name ${{ env.RESOURCE_GROUP_NAME }})
if [ "$rg_exists" = "false" ]; then
echo "Resource group does not exist. Creating..."
az group create --name ${{ env.RESOURCE_GROUP_NAME }} --location ${{ env.AZURE_LOCATION }} || { echo "Error creating resource group"; exit 1; }
else
echo "Resource group already exists."
fi
echo "RESOURCE_GROUP_NAME=${{ env.RESOURCE_GROUP_NAME }}" >> $GITHUB_OUTPUT
- name: Generate Unique Solution Prefix
id: generate_solution_prefix
run: |
set -e
COMMON_PART="Cm"
TIMESTAMP=$(date +%s)
UPDATED_TIMESTAMP=$(echo $TIMESTAMP | tail -c 6)
UNIQUE_SOLUTION_PREFIX="${COMMON_PART}${UPDATED_TIMESTAMP}"
echo "SOLUTION_PREFIX=${UNIQUE_SOLUTION_PREFIX}" >> $GITHUB_ENV
echo "Generated SOLUTION_PREFIX: ${UNIQUE_SOLUTION_PREFIX}"
- name: Deploy Bicep Template
id: deploy
run: |
set -e
az deployment group create \
--resource-group ${{ env.RESOURCE_GROUP_NAME }} \
--template-file infra/main.bicep \
--parameters solutionName=${{env.SOLUTION_PREFIX}} \
--parameters location=${{ env.AZURE_LOCATION }} \
--parameters aiDeploymentLocation=${{ env.AZURE_LOCATION }} \
--parameters azureAiServiceLocation=${{ env.AZURE_LOCATION }} \
--parameters createdBy="pipeline" \
- name: Extract AI Services and Key Vault Names
if: always()
run: |
echo "Fetching AI Services and Key Vault names before deletion..."
# Get Key Vault name
KEYVAULT_NAME=$(az resource list --resource-group ${{ env.RESOURCE_GROUP_NAME }} --resource-type "Microsoft.KeyVault/vaults" --query "[].name" -o tsv)
echo "Detected Key Vault: $KEYVAULT_NAME"
echo "KEYVAULT_NAME=$KEYVAULT_NAME" >> $GITHUB_ENV
# Get AI Services names and convert them into a space-separated string
AI_SERVICES=$(az resource list --resource-group ${{ env.RESOURCE_GROUP_NAME }} --resource-type "Microsoft.CognitiveServices/accounts" --query "[].name" -o tsv | tr '\n' ' ')
echo "Detected AI Services: $AI_SERVICES"
echo "AI_SERVICES=$AI_SERVICES" >> $GITHUB_ENV
- name: Set Deployment Status
id: deployment_status
if: always()
run: |
if [ "${{ job.status }}" == "success" ]; then
echo "SUCCESS=true" >> $GITHUB_OUTPUT
else
echo "SUCCESS=false" >> $GITHUB_OUTPUT
fi
- name: Logout from Azure
if: always()
run: |
az logout
echo "Logged out from Azure."
cleanup-deployment:
if: always() && needs.deploy.outputs.RESOURCE_GROUP_NAME != ''
needs: [deploy]
runs-on: ubuntu-latest
environment: production
env:
RESOURCE_GROUP_NAME: ${{ needs.deploy.outputs.RESOURCE_GROUP_NAME }}
steps:
- name: Login to Azure
uses: azure/login@v2
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
- name: Extract AI Services and Key Vault Names
if: always()
run: |
echo "Fetching AI Services and Key Vault names before deletion..."
# Get Key Vault name
KEYVAULT_NAME=$(az resource list --resource-group "${{ env.RESOURCE_GROUP_NAME }}" --resource-type "Microsoft.KeyVault/vaults" --query "[].name" -o tsv)
echo "Detected Key Vault: $KEYVAULT_NAME"
echo "KEYVAULT_NAME=$KEYVAULT_NAME" >> $GITHUB_ENV
# Extract AI Services names
echo "Fetching AI Services..."
AI_SERVICES=$(az resource list --resource-group '${{ env.RESOURCE_GROUP_NAME }}' --resource-type "Microsoft.CognitiveServices/accounts" --query "[].name" -o tsv)
# Flatten newline-separated values to space-separated
AI_SERVICES=$(echo "$AI_SERVICES" | paste -sd ' ' -)
echo "Detected AI Services: $AI_SERVICES"
echo "AI_SERVICES=$AI_SERVICES" >> $GITHUB_ENV
- name: Delete Bicep Deployment
if: always()
run: |
set -e
echo "Checking if resource group exists..."
rg_exists=$(az group exists --name ${{ env.RESOURCE_GROUP_NAME }})
if [ "$rg_exists" = "true" ]; then
echo "Resource group exists. Cleaning..."
az group delete \
--name ${{ env.RESOURCE_GROUP_NAME }} \
--yes \
--no-wait
echo "Resource group deleted... ${{ env.RESOURCE_GROUP_NAME }}"
else
echo "Resource group does not exist."
fi
- name: Wait for Resource Deletion to Complete
if: always()
run: |
echo "Waiting for all deployed resources (including AI Services) to be deleted..."
# Convert AI_SERVICES space-separated string into an array
IFS=' ' read -r -a resources_to_check <<< "${{ env.AI_SERVICES }}"
echo "Resources to check for deletion:"
printf '%s\n' "${resources_to_check[@]}"
# Get the current resource list in YAML
resource_list=$(az resource list --subscription "${{ secrets.AZURE_SUBSCRIPTION_ID }}" --output yaml)
# Set up retry logic
max_retries=3
retry_intervals=(30 60 120)
retries=0
while true; do
resource_found=false
for resource in "${resources_to_check[@]}"; do
echo "Checking if resource '$resource' still exists..."
if echo "$resource_list" | grep -q "name: $resource"; then
echo "Resource '$resource' still exists."
resource_found=true
else
echo "Resource '$resource' has been deleted."
fi
done
if [ "$resource_found" = true ]; then
retries=$((retries + 1))
if [ "$retries" -ge "$max_retries" ]; then
echo "Reached max retry attempts. Exiting wait loop."
break
else
echo "Some resources still exist. Waiting for ${retry_intervals[$((retries-1))]} seconds..."
sleep "${retry_intervals[$((retries-1))]}"
resource_list=$(az resource list --subscription "${{ secrets.AZURE_SUBSCRIPTION_ID }}" --output yaml)
fi
else
echo "All resources have been deleted."
break
fi
done
- name: Wait for Soft Deletion of Key Vault and AI Services
if: always()
run: |
echo "Waiting for resources to be soft deleted..."
# Wait for Key Vault to be soft deleted
if [ -n "${{ env.KEYVAULT_NAME }}" ]; then
while true; do
DELETED_VAULT=$(az keyvault show-deleted --name ${{ env.KEYVAULT_NAME }} --query "id" -o tsv 2>/dev/null || echo "")
if [ -n "$DELETED_VAULT" ]; then
echo "Key Vault soft deleted!"
break
fi
echo "Key Vault not yet soft deleted. Retrying in 15s..."
sleep 15
done
fi
# Wait for AI Services to be soft deleted
for AI_SERVICE in ${{ env.AI_SERVICES }}; do
while true; do
DELETED_AI_SERVICE=$(az cognitiveservices account list-deleted --query "[?name=='$AI_SERVICE'].id" -o tsv 2>/dev/null || echo "")
if [ -n "$DELETED_AI_SERVICE" ]; then
echo "AI Service $AI_SERVICE is soft deleted!"
break
fi
echo "AI Service $AI_SERVICE not yet soft deleted. Retrying in 15s..."
sleep 15
done
done
- name: Purge Key Vault and AI Services
if: always()
run: |
echo "Purging soft deleted resources..."
# Ensure AI_SERVICES is properly split into individual services
IFS=' ' read -r -a SERVICES <<< "${{ env.AI_SERVICES }}"
for AI_SERVICE in "${SERVICES[@]}"; do
echo "Checking location for AI Service: $AI_SERVICE"
# Fetch AI Service location
SERVICE_LOCATION=$(az cognitiveservices account list-deleted --query "[?name=='$AI_SERVICE'].location" -o tsv 2>/dev/null || echo "")
if [ -n "$SERVICE_LOCATION" ]; then
echo "Purging AI Service $AI_SERVICE in $SERVICE_LOCATION"
az cognitiveservices account purge --location "$SERVICE_LOCATION" --resource-group "${{ env.RESOURCE_GROUP_NAME }}" --name "$AI_SERVICE"
else
echo "Could not determine location for AI Service: $AI_SERVICE. Skipping purge."
fi
done
# Purge Key Vaults
echo "Starting purge for Key Vaults..."
IFS=' ' read -r -a VAULTS <<< "${{ env.KEYVAULT_NAME }}"
for VAULT in "${VAULTS[@]}"; do
echo "Checking location for Key Vault: $VAULT"
# Fetch Key Vault location
VAULT_LOCATION=$(az keyvault list-deleted --query "[?name=='$VAULT'].properties.location" -o tsv 2>/dev/null || echo "")
if [ -n "$VAULT_LOCATION" ]; then
echo "Purging Key Vault $VAULT in $VAULT_LOCATION"
az keyvault purge --name "$VAULT" --location "$VAULT_LOCATION"
else
echo "Could not determine location for Key Vault: $VAULT. Skipping purge."
fi
done
- name: Send Notification on Failure
if: failure() || needs.deploy.result == 'failure'
run: |
RUN_URL="https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
EMAIL_BODY=$(cat <<EOF
{
"body": "<p>Dear Team,</p><p>We would like to inform you that the Container Migration Deployment Automation process has encountered an issue and has failed to complete successfully.</p><p><strong>Build URL:</strong> <a href=\"${RUN_URL}\">${RUN_URL}</a><br></p><p>Please investigate the matter at your earliest convenience.</p><p>Best regards,<br>Your Automation Team</p>",
"subject": "Container Migration - Pipeline Failed"
}
EOF
)
curl -X POST "${{ secrets.LOGIC_APP_URL }}" \
-H "Content-Type: application/json" \
-d "$EMAIL_BODY" || echo "Failed to send notification"
- name: Logout from Azure
if: always()
run: |
az logout
echo "Logged out from Azure."