3232 echo "Generating a unique resource group name..."
3333 TIMESTAMP=$(date +%Y%m%d%H%M%S)
3434 COMMON_PART="ci-mycsa"
35- UNIQUE_RG_NAME="${COMMON_PART}${TIMESTAMP}"
35+ # UNIQUE_RG_NAME="${COMMON_PART}${TIMESTAMP}"
36+ UNIQUE_RG_NAME="ci-mycsa20250512102836"
3637 echo "RESOURCE_GROUP_NAME=${UNIQUE_RG_NAME}" >> $GITHUB_ENV
3738 echo "Generated Resource_GROUP_PREFIX: ${UNIQUE_RG_NAME}"
3839
@@ -51,14 +52,14 @@ jobs:
5152 fi
5253
5354
54- - name : Deploy Bicep Template
55- id : deploy
56- run : |
57- set -e
58- az deployment group create \
59- --resource-group ${{ env.RESOURCE_GROUP_NAME }} \
60- --template-file infra/main.bicep \
61- --parameters AzureAiServiceLocation=northcentralus Prefix=codegen
55+ # - name: Deploy Bicep Template
56+ # id: deploy
57+ # run: |
58+ # set -e
59+ # az deployment group create \
60+ # --resource-group ${{ env.RESOURCE_GROUP_NAME }} \
61+ # --template-file infra/main.bicep \
62+ # --parameters AzureAiServiceLocation=northcentralus Prefix=codegen
6263
6364
6465 # - name: Send Notification on Failure
@@ -118,163 +119,175 @@ jobs:
118119 fi
119120
120121
121- # - name: List KeyVaults and Store in Array
122- # id: list_keyvaults
123- # run: |
122+ - name : List KeyVaults and Store in Array
123+ id : list_keyvaults
124+ run : |
124125
125- # set -e
126- # echo "Listing all KeyVaults in the resource group ${RESOURCE_GROUP_NAME}..."
126+ set -e
127+ echo "Listing all KeyVaults in the resource group ${RESOURCE_GROUP_NAME}..."
127128
128- # # Get the list of KeyVaults in the specified resource group
129- # keyvaults=$(az resource list --resource-group ${{ env.RESOURCE_GROUP_NAME }} --query "[?type=='Microsoft.KeyVault/vaults'].name" -o tsv)
130-
131- # if [ -z "$keyvaults" ]; then
132- # echo "No KeyVaults found in resource group ${RESOURCE_GROUP_NAME}."
133- # echo "KEYVAULTS=[]" >> $GITHUB_ENV # If no KeyVaults found, set an empty array
134- # else
135- # echo "KeyVaults found: $keyvaults"
136-
137- # # Format the list into an array with proper formatting (no trailing comma)
138- # keyvault_array="["
139- # first=true
140- # for kv in $keyvaults; do
141- # if [ "$first" = true ]; then
142- # keyvault_array="$keyvault_array\"$kv\""
143- # first=false
144- # else
145- # keyvault_array="$keyvault_array,\"$kv\""
146- # fi
147- # done
148- # keyvault_array="$keyvault_array]"
149-
150- # # Output the formatted array and save it to the environment variable
151- # echo "KEYVAULTS=$keyvault_array" >> $GITHUB_ENV
152- # fi
153-
154- # - name: Purge log analytics workspace
155- # id: log_analytics_workspace
156- # run: |
129+ # Get the list of KeyVaults in the specified resource group
130+ keyvaults=$(az resource list --resource-group ${{ env.RESOURCE_GROUP_NAME }} --query "[?type=='Microsoft.KeyVault/vaults'].name" -o tsv)
157131
158- # set -e
159- # # Purge Log Analytics Workspace
160- # echo "Purging the Log Analytics Workspace..."
161- # if ! az monitor log-analytics workspace delete --force --resource-group ${{ env.RESOURCE_GROUP_NAME }} --workspace-name ${{ env.LOG_ANALYTICS_WORKSPACE_NAME }} --yes --verbose; then
162- # echo "Failed to purge Log Analytics workspace: ${{ env.LOG_ANALYTICS_WORKSPACE_NAME }}"
163- # else
164- # echo "Purged the Log Analytics workspace: ${{ env.LOG_ANALYTICS_WORKSPACE_NAME }}"
165- # fi
166-
167- # echo "Log analytics workspace resource purging completed successfully"
132+ if [ -z "$keyvaults" ]; then
133+ echo "No KeyVaults found in resource group ${RESOURCE_GROUP_NAME}."
134+ echo "KEYVAULTS=[]" >> $GITHUB_ENV # If no KeyVaults found, set an empty array
135+ else
136+ echo "KeyVaults found: $keyvaults"
137+
138+ # Format the list into an array with proper formatting (no trailing comma)
139+ keyvault_array="["
140+ first=true
141+ for kv in $keyvaults; do
142+ if [ "$first" = true ]; then
143+ keyvault_array="$keyvault_array\"$kv\""
144+ first=false
145+ else
146+ keyvault_array="$keyvault_array,\"$kv\""
147+ fi
148+ done
149+ keyvault_array="$keyvault_array]"
150+
151+ # Output the formatted array and save it to the environment variable
152+ echo "KEYVAULTS=$keyvault_array" >> $GITHUB_ENV
153+ fi
154+
155+ - name : Purge log analytics workspace
156+ id : log_analytics_workspace
157+ run : |
158+
159+ set -e
160+ # Purge Log Analytics Workspace
161+ echo "Purging the Log Analytics Workspace..."
162+ if ! az monitor log-analytics workspace delete --force --resource-group ${{ env.RESOURCE_GROUP_NAME }} --workspace-name ${{ env.LOG_ANALYTICS_WORKSPACE_NAME }} --yes --verbose; then
163+ echo "Failed to purge Log Analytics workspace: ${{ env.LOG_ANALYTICS_WORKSPACE_NAME }}"
164+ else
165+ echo "Purged the Log Analytics workspace: ${{ env.LOG_ANALYTICS_WORKSPACE_NAME }}"
166+ fi
167+
168+ echo "Log analytics workspace resource purging completed successfully"
168169
169170
170- # - name: Delete Bicep Deployment
171- # if: success()
172- # run: |
173- # set -e
174- # echo "Checking if resource group exists..."
175- # rg_exists=$(az group exists --name ${{ env.RESOURCE_GROUP_NAME }})
176- # if [ "$rg_exists" = "true" ]; then
177- # echo "Resource group exist. Cleaning..."
178- # az group delete \
179- # --name ${{ env.RESOURCE_GROUP_NAME }} \
180- # --yes \
181- # --no-wait
182- # echo "Resource group deleted... ${{ env.RESOURCE_GROUP_NAME }}"
183- # else
184- # echo "Resource group does not exists."
185- # fi
186-
187-
188- # - name: Wait for resource deletion to complete
189- # run: |
171+ - name : Delete Bicep Deployment
172+ if : success()
173+ run : |
174+ set -e
175+ echo "Checking if resource group exists..."
176+ rg_exists=$(az group exists --name ${{ env.RESOURCE_GROUP_NAME }})
177+ if [ "$rg_exists" = "true" ]; then
178+ echo "Resource group exist. Cleaning..."
179+ az group delete \
180+ --name ${{ env.RESOURCE_GROUP_NAME }} \
181+ --yes \
182+ --no-wait
183+ echo "Resource group deleted... ${{ env.RESOURCE_GROUP_NAME }}"
184+ else
185+ echo "Resource group does not exists."
186+ fi
190187
191- # # List of keyvaults
192- # KEYVAULTS="${{ env.KEYVAULTS }}"
193188
194- # # Remove the surrounding square brackets, if they exist
195- # stripped_keyvaults=$(echo "$KEYVAULTS" | sed 's/\[\|\]//g')
189+ - name : Wait for resource deletion to complete
190+ run : |
191+
192+ # List of keyvaults
193+ KEYVAULTS="${{ env.KEYVAULTS }}"
194+
195+ # Remove the surrounding square brackets, if they exist
196+ stripped_keyvaults=$(echo "$KEYVAULTS" | sed 's/\[\|\]//g')
196197
197- # # Convert the comma-separated string into an array
198- # IFS=',' read -r -a resources_to_check <<< "$stripped_keyvaults"
199-
200- # # Append new resources to the array
201- # resources_to_check+=("${{ env.LOG_ANALYTICS_WORKSPACE_NAME }}")
202-
203- # echo "List of resources to check: ${resources_to_check[@]}"
204-
205- # # Maximum number of retries
206- # max_retries=3
207-
208- # # Retry intervals in seconds (30, 60, 120)
209- # retry_intervals=(30 60 120)
210-
211- # # Retry mechanism to check resources
212- # retries=0
213- # while true; do
214- # resource_found=false
215-
216- # # Get the list of resources in YAML format again on each retry
217- # resource_list=$(az resource list --resource-group ${{ env.RESOURCE_GROUP_NAME }} --output yaml)
218-
219- # # Iterate through the resources to check
220- # for resource in "${resources_to_check[@]}"; do
221- # echo "Checking resource: $resource"
222- # if echo "$resource_list" | grep -q "name: $resource"; then
223- # echo "Resource '$resource' exists in the resource group."
224- # resource_found=true
225- # else
226- # echo "Resource '$resource' does not exist in the resource group."
227- # fi
228- # done
229-
230- # # If any resource exists, retry
231- # if [ "$resource_found" = true ]; then
232- # retries=$((retries + 1))
233- # if [ "$retries" -gt "$max_retries" ]; then
234- # echo "Maximum retry attempts reached. Exiting."
235- # break
236- # else
237- # # Wait for the appropriate interval for the current retry
238- # echo "Waiting for ${retry_intervals[$retries-1]} seconds before retrying..."
239- # sleep ${retry_intervals[$retries-1]}
240- # fi
241- # else
242- # echo "No resources found. Exiting."
243- # break
244- # fi
245- # done
198+ # Convert the comma-separated string into an array
199+ IFS=',' read -r -a resources_to_check <<< "$stripped_keyvaults"
200+
201+ # Append new resources to the array
202+ resources_to_check+=("${{ env.LOG_ANALYTICS_WORKSPACE_NAME }}")
203+ resources_to_check+=("${{ env.OPENAI_RESOURCE_NAME }}")
204+
205+ echo "List of resources to check: ${resources_to_check[@]}"
206+
207+ # Maximum number of retries
208+ max_retries=3
209+
210+ # Retry intervals in seconds (30, 60, 120)
211+ retry_intervals=(30 60 120)
212+
213+ # Retry mechanism to check resources
214+ retries=0
215+ while true; do
216+ resource_found=false
217+
218+ # Get the list of resources in YAML format again on each retry
219+ resource_list=$(az resource list --resource-group ${{ env.RESOURCE_GROUP_NAME }} --output yaml)
220+
221+ # Iterate through the resources to check
222+ for resource in "${resources_to_check[@]}"; do
223+ echo "Checking resource: $resource"
224+ if echo "$resource_list" | grep -q "name: $resource"; then
225+ echo "Resource '$resource' exists in the resource group."
226+ resource_found=true
227+ else
228+ echo "Resource '$resource' does not exist in the resource group."
229+ fi
230+ done
231+
232+ # If any resource exists, retry
233+ if [ "$resource_found" = true ]; then
234+ retries=$((retries + 1))
235+ if [ "$retries" -gt "$max_retries" ]; then
236+ echo "Maximum retry attempts reached. Exiting."
237+ break
238+ else
239+ # Wait for the appropriate interval for the current retry
240+ echo "Waiting for ${retry_intervals[$retries-1]} seconds before retrying..."
241+ sleep ${retry_intervals[$retries-1]}
242+ fi
243+ else
244+ echo "No resources found. Exiting."
245+ break
246+ fi
247+ done
246248
247249
248- # - name: Purging the Resources
249- # if: success()
250- # run: |
250+ - name : Purging the Resources
251+ if : success()
252+ run : |
253+
254+ set -e
255+
256+ echo "Azure OpenAI: ${{ env.OPENAI_RESOURCE_NAME }}"
257+
258+ # Purge OpenAI Resource
259+ echo "Purging the OpenAI Resource..."
260+ if ! az resource delete --ids /subscriptions/${{ secrets.AZURE_SUBSCRIPTION_ID }}/providers/Microsoft.CognitiveServices/locations/eastus/resourceGroups/${{ env.RESOURCE_GROUP_NAME }}/deletedAccounts/${{ env.OPENAI_RESOURCE_NAME }} --verbose; then
261+ echo "Failed to purge openai resource: ${{ env.OPENAI_RESOURCE_NAME }}"
262+ else
263+ echo "Purged the openai resource: ${{ env.OPENAI_RESOURCE_NAME }}"
264+ fi
251265
252- # set -e
253- # # List of keyvaults
254- # KEYVAULTS="${{ env.KEYVAULTS }}"
266+ # List of keyvaults
267+ KEYVAULTS="${{ env.KEYVAULTS }}"
255268
256- # # Remove the surrounding square brackets, if they exist
257- # stripped_keyvaults=$(echo "$KEYVAULTS" | sed 's/\[\|\]//g')
269+ # Remove the surrounding square brackets, if they exist
270+ stripped_keyvaults=$(echo "$KEYVAULTS" | sed 's/\[\|\]//g')
258271
259- # # Convert the comma-separated string into an array
260- # IFS=',' read -r -a keyvault_array <<< "$stripped_keyvaults"
261-
262- # echo "Using KeyVaults Array..."
263- # for keyvault_name in "${keyvault_array[@]}"; do
264- # echo "Processing KeyVault: $keyvault_name"
265- # # Check if the KeyVault is soft-deleted
266- # deleted_vaults=$(az keyvault list-deleted --query "[?name=='$keyvault_name']" -o json --subscription ${{ secrets.AZURE_SUBSCRIPTION_ID }})
267-
268- # # If the KeyVault is found in the soft-deleted state, purge it
269- # if [ "$(echo "$deleted_vaults" | jq length)" -gt 0 ]; then
270- # echo "KeyVault '$keyvault_name' is soft-deleted. Proceeding to purge..."
271- # # Purge the KeyVault
272- # if az keyvault purge --name "$keyvault_name" --no-wait; then
273- # echo "Successfully purged KeyVault '$keyvault_name'."
274- # else
275- # echo "Failed to purge KeyVault '$keyvault_name'."
276- # fi
277- # else
278- # echo "KeyVault '$keyvault_name' is not soft-deleted. No action taken."
279- # fi
280- # done
272+ # Convert the comma-separated string into an array
273+ IFS=',' read -r -a keyvault_array <<< "$stripped_keyvaults"
274+
275+ echo "Using KeyVaults Array..."
276+ for keyvault_name in "${keyvault_array[@]}"; do
277+ echo "Processing KeyVault: $keyvault_name"
278+ # Check if the KeyVault is soft-deleted
279+ deleted_vaults=$(az keyvault list-deleted --query "[?name=='$keyvault_name']" -o json --subscription ${{ secrets.AZURE_SUBSCRIPTION_ID }})
280+
281+ # If the KeyVault is found in the soft-deleted state, purge it
282+ if [ "$(echo "$deleted_vaults" | jq length)" -gt 0 ]; then
283+ echo "KeyVault '$keyvault_name' is soft-deleted. Proceeding to purge..."
284+ # Purge the KeyVault
285+ if az keyvault purge --name "$keyvault_name" --no-wait; then
286+ echo "Successfully purged KeyVault '$keyvault_name'."
287+ else
288+ echo "Failed to purge KeyVault '$keyvault_name'."
289+ fi
290+ else
291+ echo "KeyVault '$keyvault_name' is not soft-deleted. No action taken."
292+ fi
293+ done
0 commit comments