7373 --template-file ClientAdvisor/Deployment/bicep/main.bicep \
7474 --parameters solutionPrefix=${{ env.SOLUTION_PREFIX }} cosmosLocation=eastus2
7575
76+ - name : List KeyVaults and Store in Array
77+ id : list_keyvaults
78+ run : |
79+
80+ set -e
81+ echo "Listing all KeyVaults in the resource group ${RESOURCE_GROUP_NAME}..."
82+
83+ # Get the list of KeyVaults in the specified resource group
84+ keyvaults=$(az resource list --resource-group ${{ env.RESOURCE_GROUP_NAME }} --query "[?type=='Microsoft.KeyVault/vaults'].name" -o tsv)
85+
86+ if [ -z "$keyvaults" ]; then
87+ echo "No KeyVaults found in resource group ${RESOURCE_GROUP_NAME}."
88+ echo "KEYVAULTS=[]" >> $GITHUB_ENV # If no KeyVaults found, set an empty array
89+ else
90+ echo "KeyVaults found: $keyvaults"
91+
92+ # Format the list into an array with proper formatting (no trailing comma)
93+ keyvault_array="["
94+ first=true
95+ for kv in $keyvaults; do
96+ if [ "$first" = true ]; then
97+ keyvault_array="$keyvault_array\"$kv\""
98+ first=false
99+ else
100+ keyvault_array="$keyvault_array,\"$kv\""
101+ fi
102+ done
103+ keyvault_array="$keyvault_array]"
104+
105+ # Output the formatted array and save it to the environment variable
106+ echo "KEYVAULTS=$keyvault_array" >> $GITHUB_ENV
107+ fi
108+
76109 - name : Update PowerBI URL
77110 if : success()
78111 run : |
112+
79113 set -e
80114
81115 COMMON_PART="-app-service"
@@ -109,10 +143,126 @@ jobs:
109143 else
110144 echo "Resource group does not exists."
111145 fi
146+
147+ - name : Wait for resource deletion to complete
148+ run : |
149+
150+ # List of keyvaults
151+ KEYVAULTS="${{ env.KEYVAULTS }}"
152+
153+ # Remove the surrounding square brackets, if they exist
154+ stripped_keyvaults=$(echo "$KEYVAULTS" | sed 's/\[\|\]//g')
155+
156+ # Convert the comma-separated string into an array
157+ IFS=',' read -r -a resources_to_check <<< "$stripped_keyvaults"
158+
159+ # Append new resources to the array
160+ resources_to_check+=("${{ env.SOLUTION_PREFIX }}-openai" "${{ env.SOLUTION_PREFIX }}-cogser")
161+
162+ echo "List of resources to check: ${resources_to_check[@]}"
163+
164+ # Get the list of resources in YAML format
165+ resource_list=$(az resource list --resource-group myResourceGroup4 --output yaml)
166+
167+ # Maximum number of retries
168+ max_retries=3
169+
170+ # Retry intervals in seconds (30, 60, 120)
171+ retry_intervals=(30 60 120)
172+
173+ # Retry mechanism to check resources
174+ retries=0
175+ while true; do
176+ resource_found=false
177+
178+ # Iterate through the resources to check
179+ for resource in "${resources_to_check[@]}"; do
180+ echo "Checking resource: $resource"
181+ if echo "$resource_list" | grep -q "name: $resource"; then
182+ echo "Resource '$resource' exists in the resource group."
183+ resource_found=true
184+ else
185+ echo "Resource '$resource' does not exist in the resource group."
186+ fi
187+ done
188+
189+ # If any resource exists, retry
190+ if [ "$resource_found" = true ]; then
191+ retries=$((retries + 1))
192+ if [ "$retries" -ge "$max_retries" ]; then
193+ echo "Maximum retry attempts reached. Exiting."
194+ break
195+ else
196+ # Wait for the appropriate interval for the current retry
197+ echo "Waiting for ${retry_intervals[$retries-1]} seconds before retrying..."
198+ sleep ${retry_intervals[$retries-1]}
199+ fi
200+ else
201+ echo "No resources found. Exiting."
202+ break
203+ fi
204+ done
205+
206+ - name : Purging the Resources
207+ if : success()
208+ run : |
209+
210+ set -e
211+ # Define variables
212+ OPENAI_COMMON_PART="-openai"
213+ openai_name="${{ env.SOLUTION_PREFIX }}${OPENAI_COMMON_PART}"
214+ echo "Azure OpenAI: $openai_name"
215+
216+ MULTISERVICE_COMMON_PART="-cogser"
217+ multiservice_account_name="${{ env.SOLUTION_PREFIX }}${MULTISERVICE_COMMON_PART}"
218+ echo "Azure MultiService Account: $multiservice_account_name"
219+
220+ # Purge OpenAI Resource
221+ echo "Purging the OpenAI Resource..."
222+ if ! az resource delete --ids /subscriptions/${{ secrets.AZURE_SUBSCRIPTION_ID }}/providers/Microsoft.CognitiveServices/locations/uksouth/resourceGroups/${{ env.RESOURCE_GROUP_NAME }}/deletedAccounts/$openai_name --verbose; then
223+ echo "Failed to purge openai resource: $openai_name"
224+ else
225+ echo "Purged the openai resource: $openai_name"
226+ fi
227+
228+ # Purge MultiService Account Resource
229+ echo "Purging the MultiService Account Resource..."
230+ if ! az resource delete --ids /subscriptions/${{ secrets.AZURE_SUBSCRIPTION_ID }}/providers/Microsoft.CognitiveServices/locations/uksouth/resourceGroups/${{ env.RESOURCE_GROUP_NAME }}/deletedAccounts/$multiservice_account_name --verbose; then
231+ echo "Failed to purge multiService account resource : $multiservice_account_name"
232+ else
233+ echo "Purged the multiService account resource : $multiservice_account_name"
234+ fi
235+
236+ # Ensure KEYVAULTS is properly formatted as a comma-separated string
237+ KEYVAULTS="${{ env.KEYVAULTS }}"
238+
239+ # Remove the surrounding square brackets, if they exist
240+ stripped_keyvaults=$(echo "$KEYVAULTS" | sed 's/\[\|\]//g')
241+
242+ # Convert the comma-separated string into an array
243+ IFS=',' read -r -a keyvault_array <<< "$stripped_keyvaults"
244+
245+ echo "Using KeyVaults Array..."
246+ for keyvault_name in "${keyvault_array[@]}"; do
247+ echo "Processing KeyVault : $keyvault_name"
248+ # Check if the KeyVault is soft-deleted
249+ deleted_vaults=$(az keyvault list-deleted --query "[?name=='$keyvault_name']" -o json --subscription ${{ secrets.AZURE_SUBSCRIPTION_ID }})
250+
251+ # If the KeyVault is found in the soft-deleted state, purge it
252+ if [ "$(echo "$deleted_vaults" | jq length)" -gt 0 ]; then
253+ echo "KeyVault '$keyvault_name' is soft-deleted. Proceeding to purge..."
254+ az keyvault purge --name "$keyvault_name" --no-wait
255+ else
256+ echo "KeyVault '$keyvault_name' is not soft-deleted. No action taken."
257+ fi
258+ done
259+
260+ echo "Resource purging completed successfully"
112261
113262 - name : Send Notification on Failure
114263 if : failure()
115264 run : |
265+
116266 RUN_URL="https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
117267
118268 # Construct the email body
@@ -127,4 +277,3 @@ jobs:
127277 curl -X POST "${{ secrets.LOGIC_APP_URL }}" \
128278 -H "Content-Type : application/json" \
129279 -d "$EMAIL_BODY" || echo "Failed to send notification"
130-
0 commit comments