Validate Deployment #235
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Validate Deployment | |
on: | |
push: | |
branches: | |
- main | |
schedule: | |
- cron: '0 9,21 * * *' # Runs at 9:00 AM and 9:00 PM GMT | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout Code | |
uses: actions/checkout@v3 | |
- name: Setup Azure CLI | |
run: | | |
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash | |
az --version # Verify installation | |
- name: Login to Azure | |
run: | | |
az login --service-principal -u ${{ secrets.AZURE_CLIENT_ID }} -p ${{ secrets.AZURE_CLIENT_SECRET }} --tenant ${{ secrets.AZURE_TENANT_ID }} | |
- 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="30" | |
export AZURE_REGIONS="${{ vars.AZURE_REGIONS }}" | |
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 | |
- 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: | | |
echo "Generating a unique resource group name..." | |
ACCL_NAME="docgen" # 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 | |
- name: Generate Unique Solution Prefix | |
id: generate_solution_prefix | |
run: | | |
set -e | |
COMMON_PART="pslr" | |
TIMESTAMP=$(date +%s) | |
UPDATED_TIMESTAMP=$(echo $TIMESTAMP | tail -c 3) | |
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 | |
az deployment group create \ | |
--resource-group ${{ env.RESOURCE_GROUP_NAME }} \ | |
--template-file infra/main.json \ | |
--parameters \ | |
environmentName="${{ env.SOLUTION_PREFIX }}" \ | |
secondaryLocation="eastus2" \ | |
deploymentType="GlobalStandard" \ | |
gptModelName="gpt-4o" \ | |
azureOpenaiAPIVersion="2024-05-01-preview" \ | |
gptDeploymentCapacity="10" \ | |
embeddingModel="text-embedding-ada-002" \ | |
embeddingDeploymentCapacity="30" \ | |
imageTag="latest" | |
- name: Delete Bicep Deployment | |
if: success() | |
run: | | |
set -e | |
echo "Checking if resource group exists..." | |
rg_exists=$(az group exists --name ${{ env.RESOURCE_GROUP_NAME }}) | |
if [ "$rg_exists" = "true" ]; then | |
echo "Resource group exists. Cleaning..." | |
az group delete \ | |
--name ${{ env.RESOURCE_GROUP_NAME }} \ | |
--yes \ | |
--no-wait | |
echo "Resource group deleted... ${{ env.RESOURCE_GROUP_NAME }}" | |
else | |
echo "Resource group does not exist." | |
fi | |
- name: Wait for Resource Deletion to Complete | |
run: | | |
# List of resources to check based on SOLUTION_PREFIX | |
resources_to_check=( | |
"aoai-${{ env.SOLUTION_PREFIX }}" | |
"appins-${{ env.SOLUTION_PREFIX }}" | |
"db-cosmos-${{ env.SOLUTION_PREFIX }}" | |
"${{ env.SOLUTION_PREFIX }}-plan" | |
"search-${{ env.SOLUTION_PREFIX }}" | |
"webapp-${{ env.SOLUTION_PREFIX }}" | |
"worksp-${{ env.SOLUTION_PREFIX }}" | |
) | |
# Get the list of resources in YAML format | |
resource_list=$(az resource list --subscription ${{ secrets.AZURE_SUBSCRIPTION_ID }} --output yaml) | |
# Maximum number of retries and retry intervals | |
max_retries=3 | |
retry_intervals=(30 60 120) | |
retries=0 | |
while true; do | |
resource_found=false | |
# Iterate through the resources to check | |
for resource in "${resources_to_check[@]}"; do | |
echo "Checking resource: $resource" | |
if echo "$resource_list" | grep -q "name: $resource"; then | |
echo "Resource '$resource' exists in the subscription." | |
resource_found=true | |
else | |
echo "Resource '$resource' does not exist in the subscription." | |
fi | |
done | |
# If any resource exists, retry | |
if [ "$resource_found" = true ]; then | |
retries=$((retries + 1)) | |
if [ "$retries" -ge "$max_retries" ]; then | |
echo "Maximum retry attempts reached. Exiting." | |
break | |
else | |
echo "Waiting for ${retry_intervals[$retries-1]} seconds before retrying..." | |
sleep ${retry_intervals[$retries-1]} | |
fi | |
else | |
echo "No resources found. Exiting." | |
break | |
fi | |
done | |
- name: Purging the Resources | |
if: success() | |
run: | | |
set -e | |
# Purging resources based on solution prefix | |
echo "Purging resources..." | |
# List of resources to purge | |
resources_to_purge=( | |
"aoai-${{ env.SOLUTION_PREFIX }}" | |
"appins-${{ env.SOLUTION_PREFIX }}" | |
"db-cosmos-${{ env.SOLUTION_PREFIX }}" | |
"${{ env.SOLUTION_PREFIX }}-plan" | |
"search-${{ env.SOLUTION_PREFIX }}" | |
"webapp-${{ env.SOLUTION_PREFIX }}" | |
"worksp-${{ env.SOLUTION_PREFIX }}" | |
) | |
for resource in "${resources_to_purge[@]}"; do | |
echo "Purging resource: $resource" | |
if ! az resource delete --ids /subscriptions/${{ secrets.AZURE_SUBSCRIPTION_ID }}/providers/Microsoft.CognitiveServices/locations/uksouth/deletedAccounts/$resource --verbose; then | |
echo "Failed to purge resource: $resource" | |
else | |
echo "Purged the resource: $resource" | |
fi | |
done | |
echo "Resource purging completed successfully" | |
- name: Send Notification on Failure | |
if: failure() | |
run: | | |
RUN_URL="https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}" | |
EMAIL_BODY=$(cat <<EOF | |
{ | |
"body": "<p>Dear Team,</p><p>We would like to inform you that the Research Assistant Automation process has encountered an issue and has failed to complete successfully.</p><p><strong>Build URL:</strong> ${RUN_URL}<br> ${OUTPUT}</p><p>Please investigate the matter at your earliest convenience.</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" |