Skip to content

Commit d8af1cc

Browse files
Merge branch 'main' into dev
2 parents d154800 + 3f076fa commit d8af1cc

File tree

2 files changed

+170
-3
lines changed

2 files changed

+170
-3
lines changed

.github/workflows/CI.yml

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,76 @@ jobs:
5656
run: |
5757
$PSVersionTable.PSVersion
5858
59+
# Run Quota Check Script
60+
- name: Run Quota Check
61+
id: quota-check
62+
shell: pwsh
63+
run: |
64+
$ErrorActionPreference = "Stop" # Ensure that any error stops the pipeline
65+
66+
# Path to the PowerShell script for quota check
67+
$quotaCheckScript = "Deployment/checkquota.ps1"
68+
69+
# Check if the script exists and is executable (not needed for PowerShell like chmod)
70+
if (-not (Test-Path $quotaCheckScript)) {
71+
Write-Host "❌ Error: Quota check script not found."
72+
exit 1
73+
}
74+
75+
# Run the script
76+
.\Deployment\checkquota.ps1
77+
78+
# If the script fails, check for the failure message
79+
$quotaFailedMessage = "No region with sufficient quota found"
80+
$output = Get-Content "Deployment/checkquota.ps1"
81+
82+
if ($output -contains $quotaFailedMessage) {
83+
echo "QUOTA_FAILED=true" >> $GITHUB_ENV
84+
}
85+
env:
86+
AZURE_SUBSCRIPTION_ID: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
87+
AZURE_TENANT_ID: ${{ secrets.AZURE_TENANT_ID }}
88+
AZURE_CLIENT_ID: ${{ secrets.AZURE_CLIENT_ID }}
89+
AZURE_CLIENT_SECRET: ${{ secrets.AZURE_CLIENT_SECRET }}
90+
GPT_MIN_CAPACITY: '10'
91+
TEXT_EMBEDDING_MIN_CAPACITY: '10'
92+
AZURE_REGIONS: "${{ vars.AZURE_REGIONS }}"
93+
94+
95+
# Send Notification on Quota Failure
96+
- name: Send Notification on Quota Failure
97+
if: env.QUOTA_FAILED == 'true'
98+
shell: pwsh
99+
run: |
100+
$RUN_URL = "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
101+
102+
# Construct the email body
103+
$EMAIL_BODY = @"
104+
{
105+
"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>"
106+
}
107+
"@
108+
109+
# Send the notification
110+
try {
111+
$response = Invoke-RestMethod -Uri "${{ secrets.LOGIC_APP_URL }}" -Method Post -ContentType "application/json" -Body $EMAIL_BODY
112+
Write-Host "Notification sent successfully."
113+
} catch {
114+
Write-Host "❌ Failed to send notification."
115+
}
116+
117+
- name: Fail Pipeline if Quota Check Fails
118+
if: env.QUOTA_FAILED == 'true'
119+
run: exit 1
120+
59121
- name: Run Deployment Script with Input
60122
shell: pwsh
61123
run: |
62124
cd Deployment
63125
$input = @"
64126
${{ secrets.AZURE_SUBSCRIPTION_ID }}
65127
CanadaCentral
66-
WestUS3
128+
${{ env.VALID_REGION }}
67129
${{ secrets.EMAIL }}
68130
yes
69131
"@
@@ -136,8 +198,8 @@ jobs:
136198
Write-Host "Purging CognitiveService Account: $cognitiveservice_name"
137199
138200
# Construct resource IDs
139-
$openaiResourceId = "/subscriptions/$subscriptionId/providers/Microsoft.CognitiveServices/locations/westus3/resourceGroups/$resourceGroupName/deletedAccounts/$openai_name"
140-
$cognitiveResourceId = "/subscriptions/$subscriptionId/providers/Microsoft.CognitiveServices/locations/eastus/resourceGroups/$resourceGroupName/deletedAccounts/$cognitiveservice_name"
201+
$openaiResourceId = "/subscriptions/$subscriptionId/providers/Microsoft.CognitiveServices/locations/${{ env.VALID_REGION }}/resourceGroups/$resourceGroupName/deletedAccounts/$openai_name"
202+
$cognitiveResourceId = "/subscriptions/$subscriptionId/providers/Microsoft.CognitiveServices/locations/${{ env.VALID_REGION }}/resourceGroups/$resourceGroupName/deletedAccounts/$cognitiveservice_name"
141203
142204
# Debug: Print constructed resource IDs
143205
Write-Host "Command to purge OpenAI resource: az resource delete --ids `"$openaiResourceId`" --verbose"

Deployment/checkquota.ps1

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# List of Azure regions to check for quota (update as needed)
2+
$AZURE_REGIONS = "$env:AZURE_REGIONS"
3+
# Ensure regions are correctly split and trimmed
4+
$REGIONS = ($AZURE_REGIONS -split '[,\s]') | ForEach-Object { $_.Trim() } | Where-Object { $_ -ne "" }
5+
6+
Write-Output "📍 Processed Regions: $($REGIONS -join ', ')"
7+
8+
$SUBSCRIPTION_ID = $env:AZURE_SUBSCRIPTION_ID
9+
$GPT_MIN_CAPACITY = $env:GPT_MIN_CAPACITY
10+
$TEXT_EMBEDDING_MIN_CAPACITY = $env:TEXT_EMBEDDING_MIN_CAPACITY
11+
$AZURE_CLIENT_ID = $env:AZURE_CLIENT_ID
12+
$AZURE_TENANT_ID = $env:AZURE_TENANT_ID
13+
$AZURE_CLIENT_SECRET = $env:AZURE_CLIENT_SECRET
14+
15+
# Authenticate using Service Principal
16+
Write-Host "Authentication using Service Principal..."
17+
# Ensure Azure PowerShell module is installed and imported
18+
Install-Module -Name Az -AllowClobber -Force -Scope CurrentUser
19+
Import-Module Az
20+
21+
# Create a PSCredential object for authentication
22+
$creds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $AZURE_CLIENT_ID, (ConvertTo-SecureString $AZURE_CLIENT_SECRET -AsPlainText -Force)
23+
24+
# Attempt to connect using Service Principal
25+
try {
26+
Connect-AzAccount -ServicePrincipal -TenantId $AZURE_TENANT_ID -Credential $creds
27+
} catch {
28+
Write-Host "❌ Error: Failed to authenticate using Service Principal. $_"
29+
exit 1
30+
}
31+
32+
Write-Host "🔄 Validating required environment variables..."
33+
if (-not $SUBSCRIPTION_ID -or -not $GPT_MIN_CAPACITY -or -not $TEXT_EMBEDDING_MIN_CAPACITY) {
34+
Write-Host "❌ ERROR: Missing required environment variables."
35+
exit 1
36+
}
37+
38+
Write-Host "🔄 Setting Azure subscription..."
39+
$setSubscriptionResult = Set-AzContext -SubscriptionId $SUBSCRIPTION_ID
40+
if ($setSubscriptionResult -eq $null) {
41+
Write-Host "❌ ERROR: Invalid subscription ID or insufficient permissions."
42+
exit 1
43+
}
44+
Write-Host "✅ Azure subscription set successfully."
45+
46+
# Define models and their minimum required capacities
47+
$MIN_CAPACITY = @{
48+
"OpenAI.Standard.gpt-4o-mini" = $GPT_MIN_CAPACITY
49+
"OpenAI.Standard.text-embedding-3-large" = $TEXT_EMBEDDING_MIN_CAPACITY
50+
}
51+
52+
$VALID_REGION = ""
53+
54+
foreach ($REGION in $REGIONS) {
55+
Write-Host "----------------------------------------"
56+
Write-Host "🔍 Checking region: $REGION"
57+
58+
# Get the Cognitive Services usage information for the region
59+
$QUOTA_INFO = Get-AzCognitiveServicesUsage -Location $REGION
60+
if (-not $QUOTA_INFO) {
61+
Write-Host "⚠️ WARNING: Failed to retrieve quota for region $REGION. Skipping."
62+
continue
63+
}
64+
65+
$INSUFFICIENT_QUOTA = $false
66+
67+
foreach ($MODEL in $MIN_CAPACITY.Keys) {
68+
69+
$MODEL_INFO = $QUOTA_INFO | Where-Object { $_.Name -eq $MODEL }
70+
71+
if (-not $MODEL_INFO) {
72+
Write-Host "⚠️ WARNING: No quota information found for model: $MODEL in $REGION. Skipping."
73+
continue
74+
}
75+
76+
$CURRENT_VALUE = [int]$MODEL_INFO.CurrentValue
77+
$LIMIT = [int]$MODEL_INFO.Limit
78+
79+
$AVAILABLE = $LIMIT - $CURRENT_VALUE
80+
81+
Write-Host "✅ Model: $MODEL | Used: $CURRENT_VALUE | Limit: $LIMIT | Available: $AVAILABLE"
82+
83+
if ($AVAILABLE -lt $MIN_CAPACITY[$MODEL]) {
84+
Write-Host "❌ ERROR: $MODEL in $REGION has insufficient quota."
85+
$INSUFFICIENT_QUOTA = $true
86+
break
87+
}
88+
}
89+
90+
if ($INSUFFICIENT_QUOTA -eq $false) {
91+
$VALID_REGION = $REGION
92+
break
93+
}
94+
95+
}
96+
97+
if (-not $VALID_REGION) {
98+
Write-Host "❌ No region with sufficient quota found. Blocking deployment."
99+
echo "QUOTA_FAILED=true" >> $env:GITHUB_ENV # Set QUOTA_FAILED for subsequent steps
100+
exit 0
101+
} else {
102+
Write-Host "✅ Suggested Region: $VALID_REGION"
103+
echo "VALID_REGION=$VALID_REGION" >> $env:GITHUB_ENV # Set VALID_REGION for subsequent steps
104+
exit 0
105+
}

0 commit comments

Comments
 (0)