-
Notifications
You must be signed in to change notification settings - Fork 111
491 lines (430 loc) · 19.5 KB
/
CI.yml
File metadata and controls
491 lines (430 loc) · 19.5 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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
name: Deploy-Test-Cleanup Pipeline
on:
push:
branches:
- main # Adjust this to the branch you want to trigger the deployment on
- dev
- demo
paths:
- 'infra/**'
- 'App/**'
- 'Deployment/**'
- 'azure.yaml'
- '.github/workflows/CI.yml'
- '.github/workflows/test-automation.yml'
- 'tests/**'
schedule:
- cron: "0 10,22 * * *" # Runs at 10:00 AM and 10:00 PM GMT
permissions:
id-token: write
contents: read
actions: read
env:
GPT_CAPACITY: 150
TEXT_EMBEDDING_CAPACITY: 200
jobs:
deploy:
runs-on: ubuntu-latest
environment: production
outputs:
RESOURCE_GROUP_NAME: ${{ steps.get_webapp_url.outputs.RESOURCE_GROUP_NAME }}
KUBERNETES_RESOURCE_GROUP_NAME: ${{ steps.get_webapp_url.outputs.KUBERNETES_RESOURCE_GROUP_NAME }}
WEBAPP_URL: ${{ steps.get_webapp_url.outputs.WEBAPP_URL }}
OPENAI_RESOURCE_NAME: ${{ steps.get_webapp_url.outputs.OPENAI_RESOURCE_NAME }}
DOCUMENT_INTELLIGENCE_RESOURCE_NAME: ${{ steps.get_webapp_url.outputs.DOCUMENT_INTELLIGENCE_RESOURCE_NAME }}
VALID_REGION: ${{ steps.get_webapp_url.outputs.VALID_REGION }}
steps:
- name: Checkout Code
uses: actions/checkout@v5 # Checks out your repository
- name: Install Kubernetes CLI (kubectl)
shell: bash
run: |
az aks install-cli
az extension add --name aks-preview
- name: Install Helm
shell: bash
run: |
# If helm is already available on the runner, print version and skip installation
if command -v helm >/dev/null 2>&1; then
echo "helm already installed: $(helm version --short 2>/dev/null || true)"
exit 0
fi
# Ensure prerequisites are present
sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates curl gnupg lsb-release
# Ensure keyrings dir exists
sudo mkdir -p /usr/share/keyrings
# Add Helm GPG key (use -fS to fail fast on curl errors)
curl -fsSL https://baltocdn.com/helm/signing.asc | gpg --dearmor | sudo tee /usr/share/keyrings/helm.gpg >/dev/null
# Add the Helm apt repository
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/helm.gpg] https://baltocdn.com/helm/stable/debian/ all main" | sudo tee /etc/apt/sources.list.d/helm-stable-debian.list
# Install helm
sudo apt-get update
sudo apt-get install -y helm
# Verify
echo "Installed helm version:"
helm version
- name: Set up Docker
uses: docker/setup-buildx-action@v3
with:
driver: docker
- 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 }}
enable-AzPSSession: true
- name: Run Quota Check
id: quota-check
shell: pwsh
run: |
$ErrorActionPreference = "Stop" # Ensure that any error stops the pipeline
# Path to the PowerShell script for quota check
$quotaCheckScript = "Deployment/checkquota.ps1"
# Check if the script exists and is executable (not needed for PowerShell like chmod)
if (-not (Test-Path $quotaCheckScript)) {
Write-Host "❌ Error: Quota check script not found."
exit 1
}
# Run the script
.\Deployment\checkquota.ps1
# If the script fails, check for the failure message
$quotaFailedMessage = "No region with sufficient quota found"
$output = Get-Content "Deployment/checkquota.ps1"
if ($output -contains $quotaFailedMessage) {
echo "QUOTA_FAILED=true" >> $GITHUB_ENV
}
env:
AZURE_SUBSCRIPTION_ID: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
GPT_MIN_CAPACITY: ${{ env.GPT_CAPACITY }}
TEXT_EMBEDDING_MIN_CAPACITY: ${{ env.TEXT_EMBEDDING_CAPACITY }}
AZURE_REGIONS: "${{ vars.AZURE_REGIONS }}"
- name: Send Notification on Quota Failure
if: env.QUOTA_FAILED == 'true'
shell: pwsh
run: |
$RUN_URL = "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
# Construct the email body
$EMAIL_BODY = @"
{
"body": "<p>Dear Team,</p><p>The 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>"
}
"@
# Send the notification
try {
$response = Invoke-RestMethod -Uri "${{ secrets.LOGIC_APP_URL }}" -Method Post -ContentType "application/json" -Body $EMAIL_BODY
Write-Host "Notification sent successfully."
} catch {
Write-Host "❌ 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: Install azd
uses: Azure/setup-azd@v2
- 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="dkm" # Account name as specified
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="psldkm"
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
# Generate current timestamp in desired format: YYYY-MM-DDTHH:MM:SS.SSSSSSSZ
current_date=$(date -u +"%Y-%m-%dT%H:%M:%S.%7NZ")
az deployment group create \
--name ${{ env.SOLUTION_PREFIX }}-deployment \
--resource-group ${{ env.RESOURCE_GROUP_NAME }} \
--template-file infra/main.bicep \
--parameters \
solutionName="${{ env.SOLUTION_PREFIX }}" \
location=${{ env.AZURE_LOCATION }} \
aiDeploymentsLocation=${{ env.AZURE_LOCATION }} \
gptModelDeploymentType="GlobalStandard" \
gptModelName="gpt-4.1-mini" \
gptModelCapacity=${{ env.GPT_CAPACITY }} \
gptModelVersion="2025-04-14" \
embeddingModelName="text-embedding-3-large" \
embeddingModelCapacity=${{ env.TEXT_EMBEDDING_CAPACITY }} \
embeddingModelVersion="1" \
enablePrivateNetworking=false \
enableMonitoring=false \
enableTelemetry=true \
enableRedundancy=false \
enableScalability=false \
createdBy="Pipeline" \
tags="{'Purpose':'Deploying and Cleaning Up Resources for Validation','CreatedDate':'$current_date'}"
- name: Get Deployment Output and extract Values
id: get_output
run: |
set -e
echo "Fetching deployment output..."
BICEP_OUTPUT=$(az deployment group show \
--name ${{ env.SOLUTION_PREFIX }}-deployment \
--resource-group ${{ env.RESOURCE_GROUP_NAME }} \
--query "properties.outputs" -o json)
echo "Deployment outputs:"
echo "$BICEP_OUTPUT"
# Write outputs to GitHub env
# Loop through keys, normalize to uppercase, and export
for key in $(echo "$BICEP_OUTPUT" | jq -r 'keys[]'); do
value=$(echo "$BICEP_OUTPUT" | jq -r ".[\"$key\"].value")
upper_key=$(echo "$key" | tr '[:lower:]' '[:upper:]')
echo "$upper_key=$value" >> $GITHUB_ENV
done
- name: Run Deployment Script with Input
shell: pwsh
run: |
cd Deployment
$input = @"
${{ secrets.EMAIL }}
yes
"@
$input | pwsh ./resourcedeployment.ps1
Write-Host "Resource Group Name is ${{ env.RESOURCE_GROUP_NAME }}"
Write-Host "Kubernetes resource group is ${{ env.AZURE_AKS_NAME }}"
env:
# From GitHub secrets
AZURE_SUBSCRIPTION_ID: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
# From deployment outputs step (these come from $GITHUB_ENV)
RESOURCE_GROUP_NAME: ${{ env.RESOURCE_GROUP_NAME }}
AZURE_RESOURCE_GROUP_ID: ${{ env.AZURE_RESOURCE_GROUP_ID }}
STORAGE_ACCOUNT_NAME: ${{ env.STORAGE_ACCOUNT_NAME }}
AZURE_SEARCH_SERVICE_NAME: ${{ env.AZURE_SEARCH_SERVICE_NAME }}
AZURE_AKS_NAME: ${{ env.AZURE_AKS_NAME }}
AZURE_AKS_MI_ID: ${{ env.AZURE_AKS_MI_ID }}
AZURE_CONTAINER_REGISTRY_NAME: ${{ env.AZURE_CONTAINER_REGISTRY_NAME }}
AZURE_COGNITIVE_SERVICE_NAME: ${{ env.AZURE_COGNITIVE_SERVICE_NAME }}
AZURE_COGNITIVE_SERVICE_ENDPOINT: ${{ env.AZURE_COGNITIVE_SERVICE_ENDPOINT }}
AZURE_OPENAI_SERVICE_NAME: ${{ env.AZURE_OPENAI_SERVICE_NAME }}
AZURE_OPENAI_SERVICE_ENDPOINT: ${{ env.AZURE_OPENAI_SERVICE_ENDPOINT }}
AZURE_COSMOSDB_NAME: ${{ env.AZURE_COSMOSDB_NAME }}
AZ_GPT4O_MODEL_NAME: ${{ env.AZ_GPT4O_MODEL_NAME }}
AZ_GPT4O_MODEL_ID: ${{ env.AZ_GPT4O_MODEL_ID }}
AZ_GPT_EMBEDDING_MODEL_NAME: ${{ env.AZ_GPT_EMBEDDING_MODEL_NAME }}
AZ_GPT_EMBEDDING_MODEL_ID: ${{ env.AZ_GPT_EMBEDDING_MODEL_ID }}
AZURE_APP_CONFIG_ENDPOINT: ${{ env.AZURE_APP_CONFIG_ENDPOINT }}
AZURE_APP_CONFIG_NAME: ${{ env.AZURE_APP_CONFIG_NAME }}
- name: Extract Web App URL and Increase TPM
id: get_webapp_url
shell: bash
run: |
# Save the resource group name and Kubernetes resource group name to GITHUB_OUTPUT
echo "RESOURCE_GROUP_NAME=${{ env.RESOURCE_GROUP_NAME }}" >> $GITHUB_OUTPUT
echo "KUBERNETES_RESOURCE_GROUP_NAME=${{ env.krg_name }}" >> $GITHUB_OUTPUT
echo "VALID_REGION=${{ env.VALID_REGION }}" >> $GITHUB_OUTPUT
echo "OPENAI_RESOURCE_NAME=${{ env.AZURE_OPENAI_SERVICE_NAME }}" >> $GITHUB_OUTPUT
echo "DOCUMENT_INTELLIGENCE_RESOURCE_NAME=${{ env.AZURE_COGNITIVE_SERVICE_NAME }}" >> $GITHUB_OUTPUT
if az account show &> /dev/null; then
echo "Azure CLI is authenticated."
else
echo "Azure CLI is not authenticated. Please check the OIDC login step."
exit 1
fi
# Get the Web App URL and save it to GITHUB_OUTPUT
echo "Retrieving Web App URL..."
public_ip_name=$(az network public-ip list --resource-group ${{ env.krg_name }} --query "[?contains(name, 'kubernetes-')].name" -o tsv)
fqdn=$(az network public-ip show --resource-group ${{ env.krg_name }} --name $public_ip_name --query "dnsSettings.fqdn" -o tsv)
if [ -n "$fqdn" ]; then
echo "WEBAPP_URL=https://$fqdn" >> $GITHUB_OUTPUT
echo "Web App URL is https://$fqdn"
else
echo "Failed to retrieve Web App URL."
exit 1
fi
- name: Validate Deployment
shell: bash
run: |
webapp_url="${{ steps.get_webapp_url.outputs.WEBAPP_URL }}"
echo "Validating web app at: $webapp_url"
# Enhanced health check with retry logic
max_attempts=7
attempt=1
success=false
while [ $attempt -le $max_attempts ] && [ "$success" = false ]; do
echo "Attempt $attempt/$max_attempts: Checking web app health..."
# Check if web app responds
http_code=$(curl -s -o /dev/null -w "%{http_code}" "$webapp_url" || echo "000")
if [ "$http_code" -eq 200 ]; then
echo "✅ Web app is healthy (HTTP $http_code)"
success=true
elif [ "$http_code" -eq 404 ]; then
echo "❌ Web app not found (HTTP 404)"
break
elif [ "$http_code" -eq 503 ] || [ "$http_code" -eq 502 ]; then
echo "⚠️ Web app temporarily unavailable (HTTP $http_code), retrying..."
sleep 20
else
echo "⚠️ Web app returned HTTP $http_code, retrying..."
sleep 20
fi
attempt=$((attempt + 1))
done
if [ "$success" = false ]; then
echo "❌ Web app validation failed after $max_attempts attempts"
exit 1
fi
- name: Run Post Deployment Script
shell: pwsh
continue-on-error: true
run: |
Write-Host "Running post deployment script to upload files..."
cd Deployment
try {
.\uploadfiles.ps1 -EndpointUrl ${{ steps.get_webapp_url.outputs.WEBAPP_URL }}
Write-Host "ExitCode: $LASTEXITCODE"
if ($LASTEXITCODE -eq $null -or $LASTEXITCODE -eq 0) {
Write-Host "✅ Post deployment script completed successfully."
} else {
Write-Host "❌ Post deployment script failed with exit code: $LASTEXITCODE"
exit 1
}
}
catch {
Write-Host "❌ Post deployment script failed with error: $($_.Exception.Message)"
exit 1
}
- name: Logout from Azure
if: always()
shell: bash
run: |
if az account show &> /dev/null; then
echo "Logging out from Azure..."
az logout
echo "Logged out from Azure successfully."
else
echo "Azure CLI is not authenticated. Skipping logout."
fi
e2e-test:
needs: deploy
uses: ./.github/workflows/test-automation.yml
with:
DKM_URL: ${{ needs.deploy.outputs.WEBAPP_URL }}
secrets: inherit
cleanup-deployment:
if: always()
needs: [deploy, e2e-test]
runs-on: ubuntu-latest
environment: production
env:
RESOURCE_GROUP_NAME: ${{ needs.deploy.outputs.RESOURCE_GROUP_NAME }}
KUBERNETES_RESOURCE_GROUP_NAME: ${{ needs.deploy.outputs.KUBERNETES_RESOURCE_GROUP_NAME }}
OPENAI_RESOURCE_NAME: ${{ needs.deploy.outputs.OPENAI_RESOURCE_NAME }}
DOCUMENT_INTELLIGENCE_RESOURCE_NAME: ${{ needs.deploy.outputs.DOCUMENT_INTELLIGENCE_RESOURCE_NAME }}
VALID_REGION: ${{ needs.deploy.outputs.VALID_REGION }}
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: Delete Resource Groups
if: env.RESOURCE_GROUP_NAME != ''
shell: bash
run: |
az group delete --name ${{ env.RESOURCE_GROUP_NAME }} --yes --no-wait
az group delete --name ${{ env.KUBERNETES_RESOURCE_GROUP_NAME }} --yes --no-wait
- name: Wait for Resource Deletion to Complete
shell: bash
run: |
echo "Waiting for Azure OpenaAI and Document Intelligence resources to be deleted..."
sleep 60
retries=0
max_retries=3
sleep_duration=60
while [ $retries -lt $max_retries ]; do
aoai_exists=$(az resource list --resource-group ${{ env.RESOURCE_GROUP_NAME }} --name ${{ env.OPENAI_RESOURCE_NAME }} --query "[0].name" -o tsv)
di_exists=$(az resource list --resource-group ${{ env.RESOURCE_GROUP_NAME }} --name ${{ env.DOCUMENT_INTELLIGENCE_RESOURCE_NAME }} --query "[0].name" -o tsv)
if [ -z "$aoai_exists" ] && [ -z "$di_exists" ]; then
echo "Resources deleted successfully."
break
else
echo "Resources still exist, retrying in $((sleep_duration * (retries + 1))) seconds..."
sleep $((sleep_duration * (retries + 1)))
retries=$((retries + 1))
fi
done
- name: Purging the Resources
if: success()
shell: bash
run: |
echo "Purging the Azure OpenAI and Document Intelligence resources..."
if [ -z "${{ env.OPENAI_RESOURCE_NAME }}" ]; then
echo "No Azure OpenAI resource to purge."
else
echo "Purging Azure OpenAI resource..."
az cognitiveservices account purge --name ${{ env.OPENAI_RESOURCE_NAME }} --resource-group ${{ env.RESOURCE_GROUP_NAME }} --location ${{ env.VALID_REGION }}
fi
if [ -z "${{ env.DOCUMENT_INTELLIGENCE_RESOURCE_NAME }}" ]; then
echo "No Azure Document Intelligence resource to purge."
else
echo "Purging Azure Document Intelligence resources..."
az cognitiveservices account purge --name ${{ env.DOCUMENT_INTELLIGENCE_RESOURCE_NAME }} --resource-group ${{ env.RESOURCE_GROUP_NAME }} --location ${{ env.VALID_REGION }}
fi
- name: Send Notification on Failure
if: failure() || needs.deploy.result == 'failure'
shell: pwsh
run: |
# Define the RUN_URL variable
$RUN_URL = "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
# Construct the email body using a Here-String
$EMAIL_BODY = @"
{
"body": "<p>Dear Team,</p><p>The Document Knowledge Mining Automation process encountered an issue.</p><p><strong>Build URL:</strong> <a href='$RUN_URL'>$RUN_URL</a></p><p>Please investigate promptly.</p><p>Best regards,<br>Your Automation Team</p>"
}
"@
# Send the notification with error handling
try {
curl -X POST "${{ secrets.LOGIC_APP_URL }}" `
-H "Content-Type: application/json" `
-d "$EMAIL_BODY"
} catch {
Write-Output "Failed to send notification."
}
- name: Logout from Azure
if: always()
shell: bash
run: |
if az account show &> /dev/null; then
echo "Logging out from Azure..."
az logout
echo "Logged out from Azure successfully."
else
echo "Azure CLI is not authenticated. Skipping logout."
fi