Skip to content

ci: implement quota check in CI Pipeline to ensure resource availability in supported region #1680

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,52 @@ jobs:
- name: Checkout code
uses: actions/checkout@v4

# Run Quota Check Script
- name: Run Quota Check
id: quota-check
run: |
export AZURE_CLIENT_ID=${{ secrets.AZURE_CLIENT_ID }}
export AZURE_TENANT_ID=${{ secrets.AZURE_TENANT_ID }}
export AZURE_CLIENT_SECRET=${{ secrets.AZURE_CLIENT_SECRET }}
export AZURE_SUBSCRIPTION_ID="${{ secrets.AZURE_SUBSCRIPTION_ID }}"
export GPT_MIN_CAPACITY="130"
export TEXT_EMBEDDING_MIN_CAPACITY="30"

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 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>"
}
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

# The pipeline stops here if quota check fails!

- name: Set Deployment Region
run: |
echo "Selected Region: $VALID_REGION"
echo "AZURE_LOCATION=$VALID_REGION" >> $GITHUB_ENV

- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
Expand All @@ -51,7 +97,7 @@ jobs:
uses: devcontainers/ci@v0.3
env:
AZURE_ENV_NAME: ${{ github.run_id }}
AZURE_LOCATION: ${{ vars.AZURE_LOCATION }}
AZURE_LOCATION: ${{ env.AZURE_LOCATION }}
with:
imageName: ghcr.io/azure-samples/chat-with-your-data-solution-accelerator
cacheFrom: ghcr.io/azure-samples/chat-with-your-data-solution-accelerator
Expand Down
97 changes: 97 additions & 0 deletions scripts/checkquota.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#!/bin/bash

# List of Azure regions to check for quota (update as needed)
REGIONS=("eastus" "westus" "northcentralus" "uksouth" "swedencentral")

SUBSCRIPTION_ID="${AZURE_SUBSCRIPTION_ID}"
GPT_MIN_CAPACITY="${GPT_MIN_CAPACITY}"
TEXT_EMBEDDING_MIN_CAPACITY="${TEXT_EMBEDDING_MIN_CAPACITY}"
AZURE_CLIENT_ID="${AZURE_CLIENT_ID}"
AZURE_TENANT_ID="${AZURE_TENANT_ID}"
AZURE_CLIENT_SECRET="${AZURE_CLIENT_SECRET}"

# Authenticate using Managed Identity
echo "Authentication using Managed Identity..."
if ! az login --service-principal -u "$AZURE_CLIENT_ID" -p "$AZURE_CLIENT_SECRET" --tenant "$AZURE_TENANT_ID"; then
echo "❌ Error: Failed to login using Managed Identity."
exit 1
fi

echo "🔄 Validating required environment variables..."
if [[ -z "$SUBSCRIPTION_ID" || -z "$GPT_MIN_CAPACITY" || -z "$TEXT_EMBEDDING_MIN_CAPACITY" ]]; then
echo "❌ ERROR: Missing required environment variables."
exit 1
fi

echo "🔄 Setting Azure subscription..."
if ! az account set --subscription "$SUBSCRIPTION_ID"; then
echo "❌ ERROR: Invalid subscription ID or insufficient permissions."
exit 1
fi
echo "✅ Azure subscription set successfully."

# Define models and their minimum required capacities
declare -A MIN_CAPACITY=(
["OpenAI.Standard.gpt-4o"]=$GPT_MIN_CAPACITY
["OpenAI.Standard.text-embedding-ada-002"]=$TEXT_EMBEDDING_MIN_CAPACITY
)

VALID_REGION=""
for REGION in "${REGIONS[@]}"; do
echo "----------------------------------------"
echo "🔍 Checking region: $REGION"

QUOTA_INFO=$(az cognitiveservices usage list --location "$REGION" --output json)
if [ -z "$QUOTA_INFO" ]; then
echo "⚠️ WARNING: Failed to retrieve quota for region $REGION. Skipping."
continue
fi

INSUFFICIENT_QUOTA=false
for MODEL in "${!MIN_CAPACITY[@]}"; do
MODEL_INFO=$(echo "$QUOTA_INFO" | awk -v model="\"value\": \"$MODEL\"" '
BEGIN { RS="},"; FS="," }
$0 ~ model { print $0 }
')

if [ -z "$MODEL_INFO" ]; then
echo "⚠️ WARNING: No quota information found for model: $MODEL in $REGION. Skipping."
continue
fi

CURRENT_VALUE=$(echo "$MODEL_INFO" | awk -F': ' '/"currentValue"/ {print $2}' | tr -d ',' | tr -d ' ')
LIMIT=$(echo "$MODEL_INFO" | awk -F': ' '/"limit"/ {print $2}' | tr -d ',' | tr -d ' ')

CURRENT_VALUE=${CURRENT_VALUE:-0}
LIMIT=${LIMIT:-0}

CURRENT_VALUE=$(echo "$CURRENT_VALUE" | cut -d'.' -f1)
LIMIT=$(echo "$LIMIT" | cut -d'.' -f1)

AVAILABLE=$((LIMIT - CURRENT_VALUE))

echo "✅ Model: $MODEL | Used: $CURRENT_VALUE | Limit: $LIMIT | Available: $AVAILABLE"

if [ "$AVAILABLE" -lt "${MIN_CAPACITY[$MODEL]}" ]; then
echo "❌ ERROR: $MODEL in $REGION has insufficient quota."
INSUFFICIENT_QUOTA=true
break
fi
done

if [ "$INSUFFICIENT_QUOTA" = false ]; then
VALID_REGION="$REGION"
break
fi

done

if [ -z "$VALID_REGION" ]; then
echo "❌ No region with sufficient quota found. Blocking deployment."
echo "QUOTA_FAILED=true" >> "$GITHUB_ENV"
exit 0
else
echo "✅ Final Region: $VALID_REGION"
echo "VALID_REGION=$VALID_REGION" >> "$GITHUB_ENV"
exit 0
fi