-
Notifications
You must be signed in to change notification settings - Fork 0
244 lines (203 loc) · 9.93 KB
/
Copy pathCI.yml
File metadata and controls
244 lines (203 loc) · 9.93 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
name: Validate Deployment
on:
push:
branches:
- main # Adjust this to the branch you want to trigger the deployment on
- dev
- demo
schedule:
- cron: '0 10,22 * * *' # Runs at 10:00 AM and 10:00 PM GMT
jobs:
deploy:
runs-on: windows-latest # Use a Windows runner for PowerShell scripts
steps:
- name: Checkout Code
uses: actions/checkout@v4 # Checks out your repository
# Install Azure CLI
- name: Install Azure CLI
shell: pwsh
run: |
Invoke-WebRequest -Uri https://aka.ms/installazurecliwindows -OutFile AzureCLI.msi
Start-Process msiexec.exe -ArgumentList '/I AzureCLI.msi /quiet' -Wait
# Install kubectl (Windows method)
- name: Install kubectl
shell: pwsh
run: |
Invoke-WebRequest -Uri https://dl.k8s.io/release/v1.28.0/bin/windows/amd64/kubectl.exe -OutFile kubectl.exe
Move-Item -Path ./kubectl.exe -Destination "C:\kubectl.exe"
[Environment]::SetEnvironmentVariable('PATH', $env:PATH + ';C:\', [System.EnvironmentVariableTarget]::Machine)
# Install Helm (Windows method)
- name: Install Helm
shell: pwsh
run: |
Invoke-WebRequest -Uri https://get.helm.sh/helm-v3.13.0-windows-amd64.zip -OutFile helm.zip
Expand-Archive helm.zip -DestinationPath helm
Move-Item -Path ./helm/windows-amd64/helm.exe -Destination "C:\helm.exe"
[Environment]::SetEnvironmentVariable('PATH', $env:PATH + ';C:\', [System.EnvironmentVariableTarget]::Machine)
- name: Set Docker environment variables
run: echo "DOCKER_BUILDKIT=0" >> $GITHUB_ENV
# Set up Docker
- name: Set up Docker
uses: docker/setup-buildx-action@v3
with:
driver: docker
- name: Setup PowerShell
shell: pwsh
run: |
$PSVersionTable.PSVersion
# Run Quota Check Script
- 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 }}
AZURE_TENANT_ID: ${{ secrets.AZURE_TENANT_ID }}
AZURE_CLIENT_ID: ${{ secrets.AZURE_CLIENT_ID }}
AZURE_CLIENT_SECRET: ${{ secrets.AZURE_CLIENT_SECRET }}
GPT_MIN_CAPACITY: '10'
TEXT_EMBEDDING_MIN_CAPACITY: '10'
AZURE_REGIONS: "${{ vars.AZURE_REGIONS }}"
# Send Notification on Quota Failure
- 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> $RUN_URL</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: Run Deployment Script with Input
shell: pwsh
run: |
cd Deployment
$input = @"
${{ secrets.AZURE_SUBSCRIPTION_ID }}
CanadaCentral
${{ env.VALID_REGION }}
${{ secrets.EMAIL }}
yes
"@
$input | pwsh ./resourcedeployment.ps1
echo "Resource Group Name is ${{ env.rg_name }}"
echo "Kubernetes resource group are ${{ env.krg_name }}"
env:
AZURE_SUBSCRIPTION_ID: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
AZURE_TENANT_ID: ${{ secrets.AZURE_TENANT_ID }}
AZURE_CLIENT_ID: ${{ secrets.AZURE_CLIENT_ID }}
AZURE_CLIENT_SECRET: ${{ secrets.AZURE_CLIENT_SECRET }}
- name: Cleanup Resource Group
if: always() # Ensures this step runs even if the deployment fails
shell: pwsh
run: |
az login --service-principal --username ${{ secrets.AZURE_CLIENT_ID }} --password ${{ secrets.AZURE_CLIENT_SECRET }} --tenant ${{ secrets.AZURE_TENANT_ID }}
az group delete --name ${{ env.rg_name }} --yes --no-wait
az group delete --name ${{ env.krg_name }} --yes --no-wait
env:
AZURE_SUBSCRIPTION_ID: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
AZURE_TENANT_ID: ${{ secrets.AZURE_TENANT_ID }}
AZURE_CLIENT_ID: ${{ secrets.AZURE_CLIENT_ID }}
AZURE_CLIENT_SECRET: ${{ secrets.AZURE_CLIENT_SECRET }}
- name: Wait for Resource Deletion to Complete
shell: pwsh
run: |
$retries = 0
$maxRetries = 3
$sleepIntervals = @(700, 200, 200)
while ($retries -lt $maxRetries) {
$rgStatus = az group exists --name ${{ env.rg_name }}
$krgStatus = az group exists --name ${{ env.krg_name }}
# if (-not $rgStatus -and -not $krgStatus) {
# Write-Host "Both resource groups deleted successfully."
# break
# }
if ($rgStatus -eq "false" -and $krgStatus -eq "false") {
Write-Host "Both resource groups deleted successfully."
break
}
$retries++
if ($retries -eq $maxRetries) {
Write-Host "Resource groups deletion not confirmed after $maxRetries attempts. Exiting."
exit 1
}
Write-Host "Resource groups still exist. Retrying in $($sleepIntervals[$retries - 1]) seconds..."
Start-Sleep -Seconds $sleepIntervals[$retries - 1]
}
- name: Purging the Resources
if: success()
shell: pwsh
run: |
# Set variables using GitHub Actions environment values
$solutionPrefix = "${{ env.SOLUTION_PREFIX }}"
$subscriptionId = "${{ secrets.AZURE_SUBSCRIPTION_ID }}"
$resourceGroupName = "${{ env.rg_name }}"
$openai_name = "openaiservice-$solutionPrefix"
$cognitiveservice_name = "cognitiveservice-$solutionPrefix"
# Debug: Print resource names
Write-Host "Purging OpenAI resource: $openai_name"
Write-Host "Purging CognitiveService Account: $cognitiveservice_name"
# Construct resource IDs
$openaiResourceId = "/subscriptions/$subscriptionId/providers/Microsoft.CognitiveServices/locations/${{ env.VALID_REGION }}/resourceGroups/$resourceGroupName/deletedAccounts/$openai_name"
$cognitiveResourceId = "/subscriptions/$subscriptionId/providers/Microsoft.CognitiveServices/locations/${{ env.VALID_REGION }}/resourceGroups/$resourceGroupName/deletedAccounts/$cognitiveservice_name"
# Debug: Print constructed resource IDs
Write-Host "Command to purge OpenAI resource: az resource delete --ids `"$openaiResourceId`" --verbose"
Write-Host "Command to purge CognitiveService Account: az resource delete --ids `"$cognitiveResourceId`" --verbose"
# Purge OpenAI Resource
az resource delete --ids $openaiResourceId --verbose
if (-not $?) {
Write-Host "Failed to purge OpenAI resource: $openaiResourceId"
}
# Purge CognitiveService Account
az resource delete --ids $cognitiveResourceId --verbose
if (-not $?) {
Write-Host "Failed to purge CognitiveService Account."
}
- name: Send Notification on Failure
if: 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."
}