Skip to content

Commit ea52fca

Browse files
added code to purge the deployment resources for client advisor
1 parent 7f84184 commit ea52fca

File tree

1 file changed

+92
-1
lines changed

1 file changed

+92
-1
lines changed

.github/workflows/CAdeploy.yml

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,38 @@ jobs:
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+
set -e
80+
echo "Listing all KeyVaults in the resource group ${RESOURCE_GROUP_NAME}..."
81+
82+
# Get the list of KeyVaults in the specified resource group
83+
keyvaults=$(az resource list --resource-group ${{ env.RESOURCE_GROUP_NAME }} --query "[?type=='Microsoft.KeyVault/vaults'].name" -o tsv)
84+
85+
if [ -z "$keyvaults" ]; then
86+
echo "No KeyVaults found in resource group ${RESOURCE_GROUP_NAME}."
87+
echo "KEYVAULTS=[]" >> $GITHUB_ENV # If no KeyVaults found, set an empty array
88+
else
89+
echo "KeyVaults found: $keyvaults"
90+
91+
# Format the list into an array with proper formatting (no trailing comma)
92+
keyvault_array="["
93+
first=true
94+
for kv in $keyvaults; do
95+
if [ "$first" = true ]; then
96+
keyvault_array="$keyvault_array\"$kv\""
97+
first=false
98+
else
99+
keyvault_array="$keyvault_array,\"$kv\""
100+
fi
101+
done
102+
keyvault_array="$keyvault_array]"
103+
104+
# Output the formatted array and save it to the environment variable
105+
echo "KEYVAULTS=$keyvault_array" >> $GITHUB_ENV
106+
fi
107+
76108
- name: Update PowerBI URL
77109
if: success()
78110
run: |
@@ -109,6 +141,66 @@ jobs:
109141
else
110142
echo "Resource group does not exists."
111143
fi
144+
145+
- name: Wait for resource deletion to complete
146+
run: |
147+
echo "Waiting for resource group deletion to complete..."
148+
sleep 120 # Wait for 2 minutes (120 seconds) to allow deletion to complete
149+
150+
- name: Purging the Resources
151+
if: success()
152+
run: |
153+
set -e
154+
# Define variables
155+
OPENAI_COMMON_PART="-openai"
156+
openai_name="${{ env.SOLUTION_PREFIX }}${OPENAI_COMMON_PART}"
157+
echo "Azure OpenAI: $openai_name"
158+
159+
MULTISERVICE_COMMON_PART="-cogser"
160+
multiservice_account_name="${{ env.SOLUTION_PREFIX }}${MULTISERVICE_COMMON_PART}"
161+
echo "Azure MultiService Account: $multiservice_account_name"
162+
163+
# Purge OpenAI Resource
164+
echo "Purging the OpenAI Resource..."
165+
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
166+
echo "Failed to purge openai resource: $openai_name"
167+
else
168+
echo "Purged the openai resource: $openai_name"
169+
fi
170+
171+
# Purge MultiService Account Resource
172+
echo "Purging the MultiService Account Resource..."
173+
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
174+
echo "Failed to purge multiService account resource: $multiservice_account_name"
175+
else
176+
echo "Purged the multiService account resource: $multiservice_account_name"
177+
fi
178+
179+
# Ensure KEYVAULTS is properly formatted as a comma-separated string
180+
KEYVAULTS="${{ env.KEYVAULTS }}"
181+
182+
# Remove the surrounding square brackets, if they exist
183+
stripped_keyvaults=$(echo "$KEYVAULTS" | sed 's/\[\|\]//g')
184+
185+
# Convert the comma-separated string into an array
186+
IFS=',' read -r -a keyvault_array <<< "$stripped_keyvaults"
187+
188+
echo "Using KeyVaults Array..."
189+
for keyvault_name in "${keyvault_array[@]}"; do
190+
echo "Processing KeyVault: $keyvault_name"
191+
# Check if the KeyVault is soft-deleted
192+
deleted_vaults=$(az keyvault list-deleted --query "[?name=='$keyvault_name']" -o json --subscription ${{ secrets.AZURE_SUBSCRIPTION_ID }})
193+
194+
# If the KeyVault is found in the soft-deleted state, purge it
195+
if [ "$(echo "$deleted_vaults" | jq length)" -gt 0 ]; then
196+
echo "KeyVault '$keyvault_name' is soft-deleted. Proceeding to purge..."
197+
az keyvault purge --name "$keyvault_name" --no-wait
198+
else
199+
echo "KeyVault '$keyvault_name' is not soft-deleted. No action taken."
200+
fi
201+
done
202+
203+
echo "Resource purging completed successfully"
112204

113205
- name: Send Notification on Failure
114206
if: failure()
@@ -127,4 +219,3 @@ jobs:
127219
curl -X POST "${{ secrets.LOGIC_APP_URL }}" \
128220
-H "Content-Type: application/json" \
129221
-d "$EMAIL_BODY" || echo "Failed to send notification"
130-

0 commit comments

Comments
 (0)