Skip to content
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

ci: implement quota check in CI Pipeline to ensure resource availability in supported region for RA #376

Merged
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
46 changes: 45 additions & 1 deletion .github/workflows/RAdeploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,45 @@ jobs:
- name: Checkout Code
uses: actions/checkout@v3

- 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="30"
export TEXT_EMBEDDING_MIN_CAPACITY="45"

chmod +x ResearchAssistant/Deployment/scripts/checkquota.sh
if ! ResearchAssistant/Deployment/scripts/checkquota.sh; then
# If quota check fails due to insufficient quota, set the flag
if grep -q "No region with sufficient quota found" ResearchAssistant/Deployment/scripts/checkquota.sh; then
Comment on lines +30 to +32
Copy link
Preview

Copilot AI Feb 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using 'grep' on the script file to detect a quota check failure may not work as intended. Consider capturing and checking the output of the script execution instead.

Suggested change
if ! ResearchAssistant/Deployment/scripts/checkquota.sh; then
# If quota check fails due to insufficient quota, set the flag
if grep -q "No region with sufficient quota found" ResearchAssistant/Deployment/scripts/checkquota.sh; then
QUOTA_OUTPUT=$(ResearchAssistant/Deployment/scripts/checkquota.sh)
if [ $? -ne 0 ]; then
# If quota check fails due to insufficient quota, set the flag
if echo "$QUOTA_OUTPUT" | grep -q "No region with sufficient quota found"; then

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

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

- name: Setup Azure CLI
run: |
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
Expand All @@ -28,6 +67,11 @@ jobs:
- 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: |
Expand All @@ -46,7 +90,7 @@ jobs:
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 eastus2 || { echo "Error creating resource group"; exit 1; }
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
Expand Down
13 changes: 12 additions & 1 deletion .github/workflows/build-docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,18 @@ jobs:

- name: Determine Tag Name Based on Branch
id: determine_tag
run: echo "tagname=${{ github.ref_name == 'main' && 'latest' || github.ref_name == 'dev' && 'dev' || github.ref_name == 'demo' && 'demo' || github.head_ref || 'default' }}" >> $GITHUB_OUTPUT
run: |
if [[ "${{ github.ref_name }}" == "main" ]]; then
echo "tagname=latest" >> $GITHUB_OUTPUT
elif [[ "${{ github.ref_name }}" == "dev" ]]; then
echo "tagname=dev" >> $GITHUB_OUTPUT
elif [[ "${{ github.ref_name }}" == "demo" ]]; then
echo "tagname=demo" >> $GITHUB_OUTPUT
else
echo "tagname=default" >> $GITHUB_OUTPUT

fi


- name: Build Docker Image and optionally push
uses: docker/build-push-action@v6
Expand Down
97 changes: 97 additions & 0 deletions ResearchAssistant/Deployment/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=("eastus2" "westus" "northcentralus" "uksouth" "francecentral")

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-35-turbo-16k"]=$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 "✅ Suggested Region: $VALID_REGION"
echo "VALID_REGION=$VALID_REGION" >> "$GITHUB_ENV"
exit 0
fi