Skip to content

Commit ba01bc5

Browse files
Update deployment workflow to include environment name and adjust cleanup steps
1 parent 1f329d7 commit ba01bc5

File tree

1 file changed

+42
-198
lines changed

1 file changed

+42
-198
lines changed

.github/workflows/deploy.yml

Lines changed: 42 additions & 198 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ jobs:
4242
KEYVAULTS: ${{ steps.list_keyvaults.outputs.KEYVAULTS }}
4343
AZURE_LOCATION: ${{ steps.set_region.outputs.AZURE_LOCATION }}
4444
SOLUTION_PREFIX: ${{ steps.generate_solution_prefix.outputs.SOLUTION_PREFIX }}
45+
ENV_NAME: ${{ steps.generate_env_name.outputs.ENV_NAME }}
4546

4647
steps:
4748
- name: Checkout
@@ -163,7 +164,7 @@ jobs:
163164
- name: Generate Unique Environment Name
164165
id: generate_env_name
165166
run: |
166-
COMMON_PART="macae"
167+
COMMON_PART="pslc"
167168
TIMESTAMP=$(date +%s)
168169
UPDATED_TIMESTAMP=$(echo $TIMESTAMP | tail -c 6)
169170
UNIQUE_ENV_NAME="${COMMON_PART}${UPDATED_TIMESTAMP}"
@@ -355,229 +356,72 @@ jobs:
355356
CA_WEB_URL: ${{ needs.deploy.outputs.WEBAPP_URL }}
356357
secrets: inherit
357358

358-
cleanup:
359-
if: always()
359+
cleanup-deployment:
360+
if: always() && needs.deploy.outputs.RESOURCE_GROUP_NAME != ''
360361
needs: [deploy, e2e-test]
361362
runs-on: ubuntu-latest
362363
env:
363364
RESOURCE_GROUP_NAME: ${{ needs.deploy.outputs.RESOURCE_GROUP_NAME }}
364-
AI_SERVICES_NAME: ${{ needs.deploy.outputs.AI_SERVICES_NAME }}
365-
KEYVAULTS: ${{ needs.deploy.outputs.KEYVAULTS }}
366365
AZURE_LOCATION: ${{ needs.deploy.outputs.AZURE_LOCATION }}
367-
SOLUTION_PREFIX: ${{ needs.deploy.outputs.SOLUTION_PREFIX }}
366+
ENV_NAME: ${{ needs.deploy.outputs.ENV_NAME }}
368367
steps:
369-
- name: Setup Azure CLI
370-
run: curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
371-
372-
- name: Login to Azure
373-
run: |
374-
az login --service-principal -u ${{ secrets.AZURE_CLIENT_ID }} -p ${{ secrets.AZURE_CLIENT_SECRET }} --tenant ${{ secrets.AZURE_TENANT_ID }}
375-
az account set --subscription ${{ secrets.AZURE_SUBSCRIPTION_ID }}
376-
377-
# Set environment variables for azd authentication
378-
echo "AZURE_CLIENT_ID=${{ secrets.AZURE_CLIENT_ID }}" >> $GITHUB_ENV
379-
echo "AZURE_CLIENT_SECRET=${{ secrets.AZURE_CLIENT_SECRET }}" >> $GITHUB_ENV
380-
echo "AZURE_TENANT_ID=${{ secrets.AZURE_TENANT_ID }}" >> $GITHUB_ENV
381-
echo "AZURE_SUBSCRIPTION_ID=${{ secrets.AZURE_SUBSCRIPTION_ID }}" >> $GITHUB_ENV
368+
- name: Checkout Code
369+
uses: actions/checkout@v4
382370

383-
- name: Cleanup using azd down
384-
if: always()
371+
- name: Setup Azure Developer CLI
385372
run: |
386-
set -e
387-
echo "Cleaning up resources using azd down..."
388-
389-
# Install azd (Azure Developer CLI)
390-
curl -fsSL https://aka.ms/install-azd.sh | bash
391-
392-
# Check if resource group exists before attempting cleanup
393-
echo "Resource group name: ${{ env.RESOURCE_GROUP_NAME }}"
394-
if [ -z "${{ env.RESOURCE_GROUP_NAME }}" ]; then
395-
echo "Resource group name is empty. Skipping cleanup."
396-
exit 0
397-
fi
398-
399-
rg_exists=$(az group exists --name "${{ env.RESOURCE_GROUP_NAME }}")
400-
if [ "$rg_exists" = "true" ]; then
401-
echo "Resource group exists. Cleaning up with azd down..."
402-
403-
# Extract environment name from resource group name (remove rg- prefix)
404-
ENV_NAME=$(echo "${{ env.RESOURCE_GROUP_NAME }}" | sed 's/^rg-//')
405-
406-
# azd authentication environment variables are already set in the previous step
407-
408-
# Try to clean up using azd down (if environment exists)
409-
if azd env list | grep -q "$ENV_NAME"; then
410-
echo "Found azd environment: $ENV_NAME"
411-
azd env select "$ENV_NAME"
412-
azd down --force --purge --no-prompt
413-
else
414-
echo "No azd environment found for $ENV_NAME, falling back to resource group deletion"
415-
az group delete \
416-
--name "${{ env.RESOURCE_GROUP_NAME }}" \
417-
--yes \
418-
--no-wait
419-
fi
420-
echo "Resource cleanup initiated for: ${{ env.RESOURCE_GROUP_NAME }}"
421-
else
422-
echo "Resource group does not exist."
423-
fi
373+
curl -fsSL https://aka.ms/install-azd.sh | sudo bash
374+
azd version
424375
425-
- name: Wait for resource deletion to complete
426-
if: always()
376+
- name: Login to Azure
427377
run: |
428-
# Check if resource group name is available
429-
if [ -z "${{ env.RESOURCE_GROUP_NAME }}" ]; then
430-
echo "Resource group name is empty. Skipping resource check."
431-
exit 0
432-
fi
433-
434-
# List of keyvaults
435-
KEYVAULTS="${{ env.KEYVAULTS }}"
436-
437-
# Remove the surrounding square brackets and quotes, if they exist
438-
stripped_keyvaults=$(echo "$KEYVAULTS" | sed 's/\[\|\]//g' | sed 's/"//g')
439-
# Convert the comma-separated string into an array
440-
IFS=',' read -r -a resources_to_check <<< "$stripped_keyvaults"
441-
echo "List of resources to check: ${resources_to_check[@]}"
442-
# Check if resource group still exists before listing resources
443-
rg_exists=$(az group exists --name "${{ env.RESOURCE_GROUP_NAME }}")
444-
if [ "$rg_exists" = "false" ]; then
445-
echo "Resource group no longer exists. Skipping resource check."
446-
exit 0
447-
fi
448-
449-
# Get the list of resources in YAML format
450-
resource_list=$(az resource list --resource-group "${{ env.RESOURCE_GROUP_NAME }}" --output yaml || echo "")
451-
452-
# Maximum number of retries
453-
max_retries=3
454-
455-
# Retry intervals in seconds (30, 60, 120)
456-
retry_intervals=(30 60 120)
457-
458-
# Retry mechanism to check resources
459-
retries=0
460-
while true; do
461-
resource_found=false
462-
463-
# Check if resource group still exists
464-
rg_exists=$(az group exists --name "${{ env.RESOURCE_GROUP_NAME }}")
465-
if [ "$rg_exists" = "false" ]; then
466-
echo "Resource group no longer exists. Exiting resource check."
467-
break
468-
fi
469-
470-
# Iterate through the resources to check
471-
for resource in "${resources_to_check[@]}"; do
472-
# Skip empty resource names
473-
if [ -z "$resource" ]; then
474-
continue
475-
fi
476-
477-
echo "Checking resource: $resource"
478-
if echo "$resource_list" | grep -q "name: $resource"; then
479-
echo "Resource '$resource' exists in the resource group."
480-
resource_found=true
481-
else
482-
echo "Resource '$resource' does not exist in the resource group."
483-
fi
484-
done
485-
486-
# If any resource exists, retry
487-
if [ "$resource_found" = true ]; then
488-
retries=$((retries + 1))
489-
if [ "$retries" -ge "$max_retries" ]; then
490-
echo "Maximum retry attempts reached. Exiting."
491-
break
492-
else
493-
# Wait for the appropriate interval for the current retry
494-
echo "Waiting for ${retry_intervals[$retries-1]} seconds before retrying..."
495-
sleep ${retry_intervals[$retries-1]}
496-
# Refresh resource list
497-
resource_list=$(az resource list --resource-group "${{ env.RESOURCE_GROUP_NAME }}" --output yaml || echo "")
498-
fi
499-
else
500-
echo "No resources found. Exiting."
501-
break
502-
fi
503-
done
378+
azd auth login --client-id ${{ secrets.AZURE_CLIENT_ID }} --client-secret ${{ secrets.AZURE_CLIENT_SECRET }} --tenant-id ${{ secrets.AZURE_TENANT_ID }}
379+
azd config set defaults.subscription ${{ secrets.AZURE_SUBSCRIPTION_ID }}
504380
505-
- name: Purging the Resources
506-
if: always()
381+
- name: Select Environment
507382
run: |
508-
set -e
509-
510-
# Check if resource group name is available
511-
if [ -z "${{ env.RESOURCE_GROUP_NAME }}" ]; then
512-
echo "Resource group name is empty. Skipping resource purging."
513-
exit 0
514-
fi
515-
516-
# Purge AI Services
517-
if [ -z "${{ env.AI_SERVICES_NAME }}" ]; then
518-
echo "AI_SERVICES_NAME is not set. Skipping AI Services purge."
383+
# Try to select the environment if it exists, otherwise create a minimal environment for cleanup
384+
azd env list
385+
if azd env list | grep -q "${{ env.ENV_NAME }}"; then
386+
echo "Environment ${{ env.ENV_NAME }} found, selecting it..."
387+
azd env select ${{ env.ENV_NAME }}
519388
else
520-
echo "Purging AI Services..."
521-
if [ -n "$(az cognitiveservices account list-deleted --query "[?name=='${{ env.AI_SERVICES_NAME }}']" -o tsv)" ]; then
522-
echo "AI Services '${{ env.AI_SERVICES_NAME }}' is soft-deleted. Proceeding to purge..."
523-
az cognitiveservices account purge --location "${{ env.AZURE_LOCATION }}" --resource-group "${{ env.RESOURCE_GROUP_NAME }}" --name "${{ env.AI_SERVICES_NAME }}"
524-
else
525-
echo "AI Services '${{ env.AI_SERVICES_NAME }}' is not soft-deleted. No action taken."
526-
fi
527-
fi
389+
echo "Environment ${{ env.ENV_NAME }} not found, creating minimal environment for cleanup..."
390+
azd env new ${{ env.ENV_NAME }} --no-prompt
391+
azd env set AZURE_RESOURCE_GROUP "${{ env.RESOURCE_GROUP_NAME }}"
392+
azd env set AZURE_SUBSCRIPTION_ID "${{ secrets.AZURE_SUBSCRIPTION_ID }}"
393+
azd env set AZURE_ENV_OPENAI_LOCATION="${{ env.AZURE_LOCATION }}"
394+
azd env set AZURE_ENV_USE_WAF_ALIGNED_ARCHITECTURE=false
528395
529-
# Ensure KEYVAULTS is properly formatted as a comma-separated string
530-
KEYVAULTS="${{ env.KEYVAULTS }}"
531-
532-
# Check if KEYVAULTS is empty or null
533-
if [ -z "$KEYVAULTS" ] || [ "$KEYVAULTS" = "[]" ]; then
534-
echo "No KeyVaults to purge."
535-
exit 0
536396
fi
537397
538-
# Remove the surrounding square brackets and quotes, if they exist
539-
stripped_keyvaults=$(echo "$KEYVAULTS" | sed 's/\[\|\]//g' | sed 's/"//g')
540-
# Convert the comma-separated string into an array
541-
IFS=',' read -r -a keyvault_array <<< "$stripped_keyvaults"
542-
543-
echo "Using KeyVaults Array..."
544-
for keyvault_name in "${keyvault_array[@]}"; do
545-
# Skip empty keyvault names
546-
if [ -z "$keyvault_name" ]; then
547-
continue
548-
fi
549-
550-
echo "Processing KeyVault: $keyvault_name"
551-
# Check if the KeyVault is soft-deleted
552-
deleted_vaults=$(az keyvault list-deleted --query "[?name=='$keyvault_name']" -o json --subscription ${{ env.AZURE_SUBSCRIPTION_ID }})
553-
554-
# If the KeyVault is found in the soft-deleted state, purge it
555-
if [ "$(echo "$deleted_vaults" | jq length)" -gt 0 ]; then
556-
echo "KeyVault '$keyvault_name' is soft-deleted. Proceeding to purge..."
557-
az keyvault purge --name "$keyvault_name" --no-wait
558-
else
559-
echo "KeyVault '$keyvault_name' is not soft-deleted. No action taken."
560-
fi
561-
done
562-
563-
echo "Resource purging completed successfully"
564-
565-
- name: Logout
566-
if: always()
567-
run: az logout
398+
- name: Delete deployment using azd
399+
run: |
400+
set -e
401+
echo "Deleting deployment..."
402+
azd down --purge --force --no-prompt
403+
echo "Deployment deleted successfully."
568404
569-
- name: Notify on Failure
570-
if: failure() || needs.deploy.result == 'failure' || needs.e2e-test.result == 'failure'
405+
- name: Send Notification on Failure
406+
if: always() && (failure() || needs.deploy.result == 'failure')
571407
run: |
572408
RUN_URL="https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
409+
573410
# Construct the email body
574411
EMAIL_BODY=$(cat <<EOF
575412
{
576-
"body": "<p>Dear Team,</p><p>We would like to inform you that the Build-your-own-copilot-Solution-Accelerator(Client Advisior) 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>"
413+
"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>"
577414
}
578415
EOF
579416
)
417+
580418
# Send the notification
581419
curl -X POST "${{ secrets.LOGIC_APP_URL }}" \
582420
-H "Content-Type: application/json" \
583421
-d "$EMAIL_BODY" || echo "Failed to send notification"
422+
423+
- name: Logout from Azure
424+
if: always()
425+
run: |
426+
azd auth logout
427+
echo "Logged out from Azure."

0 commit comments

Comments
 (0)