@@ -123,6 +123,27 @@ jobs:
123123 embeddingDeploymentCapacity="30" \
124124 imageTag="latest"
125125
126+ - name : Extract AI Services and Key Vault Names
127+ if : always()
128+ run : |
129+ echo "Fetching AI Services and Key Vault names before deletion..."
130+
131+ # Get Key Vault name
132+ KEYVAULT_NAME=$(az resource list --resource-group "${{ env.RESOURCE_GROUP_NAME }}" --resource-type "Microsoft.KeyVault/vaults" --query "[].name" -o tsv)
133+ echo "Detected Key Vault: $KEYVAULT_NAME"
134+ echo "KEYVAULT_NAME=$KEYVAULT_NAME" >> $GITHUB_ENV
135+
136+
137+ # Extract AI Services names
138+ echo "Fetching AI Services..."
139+ AI_SERVICES=$(az resource list --resource-group '${{ env.RESOURCE_GROUP_NAME }}' --resource-type "Microsoft.CognitiveServices/accounts" --query "[].name" -o tsv)
140+
141+ # Flatten newline-separated values to space-separated
142+ AI_SERVICES=$(echo "$AI_SERVICES" | paste -sd ' ' -)
143+
144+ echo "Detected AI Services : $AI_SERVICES"
145+ echo "AI_SERVICES=$AI_SERVICES" >> $GITHUB_ENV
146+
126147 - name : Delete Bicep Deployment
127148 if : success()
128149 run : |
@@ -142,82 +163,124 @@ jobs:
142163
143164 - name : Wait for Resource Deletion to Complete
144165 run : |
145- # List of resources to check based on SOLUTION_PREFIX
146- resources_to_check=(
147- "aoai-${{ env.SOLUTION_PREFIX }}"
148- "appins-${{ env.SOLUTION_PREFIX }}"
149- "db-cosmos-${{ env.SOLUTION_PREFIX }}"
150- "${{ env.SOLUTION_PREFIX }}-plan"
151- "search-${{ env.SOLUTION_PREFIX }}"
152- "webapp-${{ env.SOLUTION_PREFIX }}"
153- "worksp-${{ env.SOLUTION_PREFIX }}"
154- )
155-
156- # Get the list of resources in YAML format
157- resource_list=$(az resource list --subscription ${{ secrets.AZURE_SUBSCRIPTION_ID }} --output yaml)
158-
159- # Maximum number of retries and retry intervals
166+ echo "Waiting for all deployed resources (including AI Services) to be deleted..."
167+
168+ # Convert AI_SERVICES space-separated string into an array
169+ IFS=' ' read -r -a resources_to_check <<< "${{ env.AI_SERVICES }}"
170+
171+ echo "Resources to check for deletion:"
172+ printf '%s\n' "${resources_to_check[@]}"
173+
174+ # Get the current resource list in YAML
175+ resource_list=$(az resource list --subscription "${{ secrets.AZURE_SUBSCRIPTION_ID }}" --output yaml)
176+
177+ # Set up retry logic
160178 max_retries=3
161179 retry_intervals=(30 60 120)
162180 retries=0
163-
181+
164182 while true; do
165183 resource_found=false
166- # Iterate through the resources to check
167184 for resource in "${resources_to_check[@]}"; do
168- echo "Checking resource: $resource"
185+ echo "Checking if resource ' $resource' still exists... "
169186 if echo "$resource_list" | grep -q "name : $resource"; then
170- echo "Resource '$resource' exists in the subscription ."
187+ echo "Resource '$resource' still exists ."
171188 resource_found=true
172189 else
173- echo "Resource '$resource' does not exist in the subscription ."
190+ echo "Resource '$resource' has been deleted ."
174191 fi
175192 done
176-
177- # If any resource exists, retry
193+
178194 if [ "$resource_found" = true ]; then
179195 retries=$((retries + 1))
180196 if [ "$retries" -ge "$max_retries" ]; then
181- echo "Maximum retry attempts reached . Exiting."
197+ echo "Reached max retry attempts. Exiting wait loop ."
182198 break
183199 else
184- echo "Waiting for ${retry_intervals[$retries-1]} seconds before retrying..."
185- sleep ${retry_intervals[$retries-1]}
200+ echo "Some resources still exist. Waiting for ${retry_intervals[$((retries-1))]} seconds..."
201+ sleep "${retry_intervals[$((retries-1))]}"
202+ resource_list=$(az resource list --subscription "${{ secrets.AZURE_SUBSCRIPTION_ID }}" --output yaml)
186203 fi
187204 else
188- echo "No resources found. Exiting ."
205+ echo "All resources have been deleted ."
189206 break
190207 fi
191208 done
209+
192210
193- - name : Purging the Resources
194- if : success ()
211+ - name : Wait for Soft Deletion of Key Vault and AI Services
212+ if : always ()
195213 run : |
196- set -e
197- # Purging resources based on solution prefix
198- echo "Purging resources..."
199-
200- # List of resources to purge
201- resources_to_purge=(
202- "aoai-${{ env.SOLUTION_PREFIX }}"
203- "appins-${{ env.SOLUTION_PREFIX }}"
204- "db-cosmos-${{ env.SOLUTION_PREFIX }}"
205- "${{ env.SOLUTION_PREFIX }}-plan"
206- "search-${{ env.SOLUTION_PREFIX }}"
207- "webapp-${{ env.SOLUTION_PREFIX }}"
208- "worksp-${{ env.SOLUTION_PREFIX }}"
209- )
214+ echo "Waiting for resources to be soft deleted..."
215+
216+ # Wait for Key Vault to be soft deleted
217+ if [ -n "${{ env.KEYVAULT_NAME }}" ]; then
218+ while true; do
219+ DELETED_VAULT=$(az keyvault show-deleted --name ${{ env.KEYVAULT_NAME }} --query "id" -o tsv 2>/dev/null || echo "")
220+ if [ -n "$DELETED_VAULT" ]; then
221+ echo "Key Vault soft deleted!"
222+ break
223+ fi
224+ echo "Key Vault not yet soft deleted. Retrying in 15s..."
225+ sleep 15
226+ done
227+ fi
228+
229+
230+ # Wait for AI Services to be soft deleted
231+ for AI_SERVICE in ${{ env.AI_SERVICES }}; do
232+ while true; do
233+ DELETED_AI_SERVICE=$(az cognitiveservices account list-deleted --query "[?name=='$AI_SERVICE'].id" -o tsv 2>/dev/null || echo "")
234+ if [ -n "$DELETED_AI_SERVICE" ]; then
235+ echo "AI Service $AI_SERVICE is soft deleted!"
236+ break
237+ fi
238+ echo "AI Service $AI_SERVICE not yet soft deleted. Retrying in 15s..."
239+ sleep 15
240+ done
241+ done
242+
243+
244+ - name : Purge Key Vault and AI Services
245+ if : always()
246+ run : |
247+ echo "Purging soft deleted resources..."
210248
211- for resource in "${resources_to_purge[@]}"; do
212- echo "Purging resource: $resource"
213- if ! az resource delete --ids /subscriptions/${{ secrets.AZURE_SUBSCRIPTION_ID }}/providers/Microsoft.CognitiveServices/locations/uksouth/deletedAccounts/$resource --verbose; then
214- echo "Failed to purge resource: $resource"
249+ # Ensure AI_SERVICES is properly split into individual services
250+ IFS=' ' read -r -a SERVICES <<< "${{ env.AI_SERVICES }}"
251+
252+ for AI_SERVICE in "${SERVICES[@]}"; do
253+ echo "Checking location for AI Service: $AI_SERVICE"
254+
255+ # Fetch AI Service location
256+ SERVICE_LOCATION=$(az cognitiveservices account list-deleted --query "[?name=='$AI_SERVICE'].location" -o tsv 2>/dev/null || echo "")
257+
258+ if [ -n "$SERVICE_LOCATION" ]; then
259+ echo "Purging AI Service $AI_SERVICE in $SERVICE_LOCATION"
260+ az cognitiveservices account purge --location "$SERVICE_LOCATION" --resource-group "${{ env.RESOURCE_GROUP_NAME }}" --name "$AI_SERVICE"
261+ else
262+ echo "Could not determine location for AI Service: $AI_SERVICE. Skipping purge."
263+ fi
264+ done
265+
266+ # --- Purge Key Vaults ---
267+ echo "Starting purge for Key Vaults..."
268+ IFS=' ' read -r -a VAULTS <<< "${{ env.KEYVAULT_NAME }}"
269+
270+ for VAULT in "${VAULTS[@]}"; do
271+ echo "Checking location for Key Vault: $VAULT"
272+
273+ # Fetch Key Vault location
274+ VAULT_LOCATION=$(az keyvault list-deleted --query "[?name=='$VAULT'].properties.location" -o tsv 2>/dev/null || echo "")
275+
276+ if [ -n "$VAULT_LOCATION" ]; then
277+ echo "Purging Key Vault $VAULT in $VAULT_LOCATION"
278+ az keyvault purge --name "$VAULT" --location "$VAULT_LOCATION"
215279 else
216- echo "Purged the resource : $resource "
280+ echo "Could not determine location for Key Vault : $VAULT. Skipping purge. "
217281 fi
218282 done
219283
220- echo "Resource purging completed successfully"
221284
222285 - name : Send Notification on Failure
223286 if : failure()
0 commit comments