Skip to content

Commit ba754f6

Browse files
Merge pull request #39 from Roopan-Microsoft/PSL-PURGE-9915
Added code to purge the deployment resources for research assistant
2 parents 8bc9739 + b686b06 commit ba754f6

File tree

1 file changed

+151
-2
lines changed

1 file changed

+151
-2
lines changed

.github/workflows/RAdeploy.yml

Lines changed: 151 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,40 @@ jobs:
7070
--resource-group ${{ env.RESOURCE_GROUP_NAME }} \
7171
--template-file ResearchAssistant/Deployment/bicep/main.bicep \
7272
--parameters solutionPrefix=${{ env.SOLUTION_PREFIX }}
73-
73+
74+
- name: List KeyVaults and Store in Array
75+
id: list_keyvaults
76+
run: |
77+
78+
set -e
79+
echo "Listing all KeyVaults in the resource group ${RESOURCE_GROUP_NAME}..."
80+
81+
# Get the list of KeyVaults in the specified resource group
82+
keyvaults=$(az resource list --resource-group ${{ env.RESOURCE_GROUP_NAME }} --query "[?type=='Microsoft.KeyVault/vaults'].name" -o tsv)
83+
84+
if [ -z "$keyvaults" ]; then
85+
echo "No KeyVaults found in resource group ${RESOURCE_GROUP_NAME}."
86+
echo "KEYVAULTS=[]" >> $GITHUB_ENV # If no KeyVaults found, set an empty array
87+
else
88+
echo "KeyVaults found: $keyvaults"
89+
90+
# Format the list into an array with proper formatting (no trailing comma)
91+
keyvault_array="["
92+
first=true
93+
for kv in $keyvaults; do
94+
if [ "$first" = true ]; then
95+
keyvault_array="$keyvault_array\"$kv\""
96+
first=false
97+
else
98+
keyvault_array="$keyvault_array,\"$kv\""
99+
fi
100+
done
101+
keyvault_array="$keyvault_array]"
102+
103+
# Output the formatted array and save it to the environment variable
104+
echo "KEYVAULTS=$keyvault_array" >> $GITHUB_ENV
105+
fi
106+
74107
- name: Delete Bicep Deployment
75108
if: success()
76109
run: |
@@ -88,9 +121,125 @@ jobs:
88121
echo "Resource group does not exists."
89122
fi
90123
124+
- name: Wait for resource deletion to complete
125+
run: |
126+
127+
# List of keyvaults
128+
KEYVAULTS="${{ env.KEYVAULTS }}"
129+
130+
# Remove the surrounding square brackets, if they exist
131+
stripped_keyvaults=$(echo "$KEYVAULTS" | sed 's/\[\|\]//g')
132+
133+
# Convert the comma-separated string into an array
134+
IFS=',' read -r -a resources_to_check <<< "$stripped_keyvaults"
135+
136+
# Append new resources to the array
137+
resources_to_check+=("${{ env.SOLUTION_PREFIX }}-openai" "${{ env.SOLUTION_PREFIX }}-cogser")
138+
139+
echo "List of resources to check: ${resources_to_check[@]}"
140+
141+
# Get the list of resources in YAML format
142+
resource_list=$(az resource list --resource-group myResourceGroup4 --output yaml)
143+
144+
# Maximum number of retries
145+
max_retries=3
146+
147+
# Retry intervals in seconds (30, 60, 120)
148+
retry_intervals=(30 60 120)
149+
150+
# Retry mechanism to check resources
151+
retries=0
152+
while true; do
153+
resource_found=false
154+
155+
# Iterate through the resources to check
156+
for resource in "${resources_to_check[@]}"; do
157+
echo "Checking resource: $resource"
158+
if echo "$resource_list" | grep -q "name: $resource"; then
159+
echo "Resource '$resource' exists in the resource group."
160+
resource_found=true
161+
else
162+
echo "Resource '$resource' does not exist in the resource group."
163+
fi
164+
done
165+
166+
# If any resource exists, retry
167+
if [ "$resource_found" = true ]; then
168+
retries=$((retries + 1))
169+
if [ "$retries" -ge "$max_retries" ]; then
170+
echo "Maximum retry attempts reached. Exiting."
171+
break
172+
else
173+
# Wait for the appropriate interval for the current retry
174+
echo "Waiting for ${retry_intervals[$retries-1]} seconds before retrying..."
175+
sleep ${retry_intervals[$retries-1]}
176+
fi
177+
else
178+
echo "No resources found. Exiting."
179+
break
180+
fi
181+
done
182+
183+
- name: Purging the Resources
184+
if: success()
185+
run: |
186+
187+
set -e
188+
# Define variables
189+
OPENAI_COMMON_PART="-openai"
190+
openai_name="${{ env.SOLUTION_PREFIX }}${OPENAI_COMMON_PART}"
191+
echo "Azure OpenAI: $openai_name"
192+
193+
MULTISERVICE_COMMON_PART="-cogser"
194+
multiservice_account_name="${{ env.SOLUTION_PREFIX }}${MULTISERVICE_COMMON_PART}"
195+
echo "Azure MultiService Account: $multiservice_account_name"
196+
197+
# Purge OpenAI Resource
198+
echo "Purging the OpenAI Resource..."
199+
if ! az resource delete --ids /subscriptions/${{ secrets.AZURE_SUBSCRIPTION_ID }}/providers/Microsoft.CognitiveServices/locations/eastus2/resourceGroups/${{ env.RESOURCE_GROUP_NAME }}/deletedAccounts/$openai_name --verbose; then
200+
echo "Failed to purge openai resource: $openai_name"
201+
else
202+
echo "Purged the openai resource: $openai_name"
203+
fi
204+
205+
# Purge MultiService Account Resource
206+
echo "Purging the MultiService Account Resource..."
207+
if ! az resource delete --ids /subscriptions/${{ secrets.AZURE_SUBSCRIPTION_ID }}/providers/Microsoft.CognitiveServices/locations/eastus2/resourceGroups/${{ env.RESOURCE_GROUP_NAME }}/deletedAccounts/$multiservice_account_name --verbose; then
208+
echo "Failed to purge multiService account resource: $multiservice_account_name"
209+
else
210+
echo "Purged the multiService account resource: $multiservice_account_name"
211+
fi
212+
213+
# Ensure KEYVAULTS is properly formatted as a comma-separated string
214+
KEYVAULTS="${{ env.KEYVAULTS }}"
215+
216+
# Remove the surrounding square brackets, if they exist
217+
stripped_keyvaults=$(echo "$KEYVAULTS" | sed 's/\[\|\]//g')
218+
219+
# Convert the comma-separated string into an array
220+
IFS=',' read -r -a keyvault_array <<< "$stripped_keyvaults"
221+
222+
echo "Using KeyVaults Array..."
223+
for keyvault_name in "${keyvault_array[@]}"; do
224+
echo "Processing KeyVault: $keyvault_name"
225+
# Check if the KeyVault is soft-deleted
226+
deleted_vaults=$(az keyvault list-deleted --query "[?name=='$keyvault_name']" -o json --subscription ${{ secrets.AZURE_SUBSCRIPTION_ID }})
227+
228+
# If the KeyVault is found in the soft-deleted state, purge it
229+
if [ "$(echo "$deleted_vaults" | jq length)" -gt 0 ]; then
230+
echo "KeyVault '$keyvault_name' is soft-deleted. Proceeding to purge..."
231+
az keyvault purge --name "$keyvault_name" --no-wait
232+
else
233+
echo "KeyVault '$keyvault_name' is not soft-deleted. No action taken."
234+
fi
235+
done
236+
237+
echo "Resource purging completed successfully"
238+
91239
- name: Send Notification on Failure
92240
if: failure()
93241
run: |
242+
94243
RUN_URL="https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
95244
96245
# Construct the email body
@@ -104,4 +253,4 @@ jobs:
104253
# Send the notification
105254
curl -X POST "${{ secrets.LOGIC_APP_URL }}" \
106255
-H "Content-Type: application/json" \
107-
-d "$EMAIL_BODY" || echo "Failed to send notification"
256+
-d "$EMAIL_BODY" || echo "Failed to send notification"

0 commit comments

Comments
 (0)