CI-Validate Deployment-Multi-Agent-Custom-Automation-Engine-Solution-Accelerator #9
This file contains 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: CI-Validate Deployment-Multi-Agent-Custom-Automation-Engine-Solution-Accelerator | |
on: | |
push: | |
branches: | |
- main | |
schedule: | |
- cron: '0 6,18 * * *' # Runs at 6:00 AM and 6: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: Install Bicep CLI | |
run: az bicep install | |
- name: Generate Resource Group Name | |
id: generate_rg_name | |
run: | | |
TIMESTAMP=$(date +%Y%m%d%H%M%S) | |
COMMON_PART="pslautomationRes" | |
UNIQUE_RG_NAME="${COMMON_PART}${TIMESTAMP}" | |
echo "RESOURCE_GROUP_NAME=${UNIQUE_RG_NAME}" >> $GITHUB_ENV | |
- name: Check and Create Resource Group | |
id: check_create_rg | |
run: | | |
rg_exists=$(az group exists --name ${{ env.RESOURCE_GROUP_NAME }}) | |
if [ "$rg_exists" = "false" ]; then | |
az group create --name ${{ env.RESOURCE_GROUP_NAME }} --location eastus2 | |
fi | |
- name: Generate Unique Solution Prefix | |
id: generate_solution_prefix | |
run: | | |
COMMON_PART="pslr" | |
TIMESTAMP=$(date +%s) | |
UNIQUE_SOLUTION_PREFIX="${COMMON_PART}$(echo $TIMESTAMP | tail -c 3)" | |
echo "SOLUTION_PREFIX=${UNIQUE_SOLUTION_PREFIX}" >> $GITHUB_ENV | |
- name: Deploy Bicep Template | |
id: deploy | |
run: | | |
az deployment group create \ | |
--resource-group ${{ env.RESOURCE_GROUP_NAME }} \ | |
--template-file deploy/macae.bicep \ | |
--parameters prefix=${{ env.SOLUTION_PREFIX }} \ | |
--parameters azureOpenAILocation=westus \ | |
--parameters cosmosLocation=westus | |
- name: Retrieve Deployed OpenAI Resource Name | |
id: get_openai_name | |
run: | | |
PREFIX="${{ env.SOLUTION_PREFIX }}-openai" | |
RESOURCE_GROUP="${{ env.RESOURCE_GROUP_NAME }}" | |
OPENAI_RESOURCE_NAME=$(az resource list --resource-group $RESOURCE_GROUP --query "[?contains(name, '$PREFIX')].name | [0]" -o tsv) | |
if [ -z "$OPENAI_RESOURCE_NAME" ]; then | |
echo "Failed to find the OpenAI resource in the resource group." | |
exit 1 | |
fi | |
echo "OPENAI_RESOURCE_NAME=${OPENAI_RESOURCE_NAME}" >> $GITHUB_ENV | |
- name: Use OpenAI Resource Name | |
run: | | |
echo "Deployed OpenAI Resource Name: ${{ env.OPENAI_RESOURCE_NAME }}" | |
- name: Delete Bicep Deployment | |
if: success() | |
run: | | |
az group delete --name ${{ env.RESOURCE_GROUP_NAME }} --yes --no-wait | |
- name: Wait for resource deletion to complete | |
run: | | |
resources_to_check+=("${{ env.OPENAI_RESOURCE_NAME }}") | |
echo "List of resources to check: ${resources_to_check[@]}" | |
resource_list=$(az resource list --resource-group ${{ env.RESOURCE_GROUP_NAME }} --output yaml) | |
max_retries=3 | |
retry_intervals=(60 120 140) | |
retries=0 | |
while true; do | |
resource_found=false | |
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 resource group." | |
resource_found=true | |
else | |
echo "Resource '$resource' does not exist in the resource group." | |
fi | |
done | |
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: Purge OpenAI Resource | |
if: success() | |
run: | | |
echo "Purging the OpenAI Resource..." | |
if ! az resource delete --ids /subscriptions/${{ secrets.AZURE_SUBSCRIPTION_ID }}/providers/Microsoft.CognitiveServices/locations/westus/resourceGroups/${{ env.RESOURCE_GROUP_NAME }}/deletedAccounts/${{ env.OPENAI_RESOURCE_NAME }} --verbose; then | |
echo "Failed to purge OpenAI resource: ${{ env.OPENAI_RESOURCE_NAME }}" | |
else | |
echo "Purged the OpenAI resource: ${{ env.OPENAI_RESOURCE_NAME }}" | |
fi | |
- name: Send Notification on Failure | |
if: failure() | |
run: | | |
RUN_URL="https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}" | |
# Construct the email body | |
EMAIL_BODY=$(cat <<EOF | |
{ | |
"body": "<p>Dear Team,</p><p>We would like to inform you that the Multi-Agent-Custom-Automation-Engine-Solution-Accelerator 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 | |
) | |
# Send the notification | |
curl -X POST "${{ secrets.LOGIC_APP_URL }}" \ | |
-H "Content-Type: application/json" \ | |
-d "$EMAIL_BODY" || echo "Failed to send notification" |