@@ -97,165 +97,184 @@ jobs:
9797 echo "LOG_ANALYTICS_WORKSPACE_NAME=${log_analytics_workspace_name}" >> $GITHUB_ENV
9898 echo "Log Analytics workspace name : ${log_analytics_workspace_name}"
9999 fi
100-
101-
102- - name : List KeyVaults and Store in Array
103- id : list_keyvaults
100+
101+ - name : Get OpenAI, App Service and Container Registry Resource from Resource Group
102+ id : get_openai_resource
104103 run : |
105104
105+
106106 set -e
107- echo "Listing all KeyVaults in the resource group ${RESOURCE_GROUP_NAME}..."
107+ echo "Fetching OpenAI resource from resource group ${{ env. RESOURCE_GROUP_NAME } }..."
108108
109- # Get the list of KeyVaults in the specified resource group
110- keyvaults =$(az resource list --resource-group ${{ env.RESOURCE_GROUP_NAME }} --query "[?type==' Microsoft.KeyVault/vaults' ].name" -o tsv)
109+ # Run the az resource list command to get the OpenAI resource name
110+ openai_resource_name =$(az resource list --resource-group ${{ env.RESOURCE_GROUP_NAME }} --resource-type " Microsoft.CognitiveServices/accounts" --query "[0 ].name" -o tsv)
111111
112- if [ -z "$keyvaults " ]; then
113- echo "No KeyVaults found in resource group ${RESOURCE_GROUP_NAME}."
114- echo "KEYVAULTS=[]" >> $GITHUB_ENV # If no KeyVaults found, set an empty array
112+ if [ -z "$openai_resource_name " ]; then
113+ echo "No OpenAI resource found in resource group ${{ env. RESOURCE_GROUP_NAME } }."
114+ exit 1
115115 else
116- echo "KeyVaults found: $keyvaults"
117-
118- # Format the list into an array with proper formatting (no trailing comma)
119- keyvault_array="["
120- first=true
121- for kv in $keyvaults; do
122- if [ "$first" = true ]; then
123- keyvault_array="$keyvault_array\"$kv\""
124- first=false
125- else
126- keyvault_array="$keyvault_array,\"$kv\""
127- fi
128- done
129- keyvault_array="$keyvault_array]"
130-
131- # Output the formatted array and save it to the environment variable
132- echo "KEYVAULTS=$keyvault_array" >> $GITHUB_ENV
116+ echo "OPENAI_RESOURCE_NAME=${openai_resource_name}" >> $GITHUB_ENV
117+ echo "OpenAI resource name: ${openai_resource_name}"
133118 fi
119+
134120
135- - name : Purge log analytics workspace
136- id : log_analytics_workspace
137- run : |
138-
139- set -e
140- # Purge Log Analytics Workspace
141- echo "Purging the Log Analytics Workspace..."
142- if ! az monitor log-analytics workspace delete --force --resource-group ${{ env.RESOURCE_GROUP_NAME }} --workspace-name ${{ env.LOG_ANALYTICS_WORKSPACE_NAME }} --yes --verbose; then
143- echo "Failed to purge Log Analytics workspace: ${{ env.LOG_ANALYTICS_WORKSPACE_NAME }}"
144- else
145- echo "Purged the Log Analytics workspace: ${{ env.LOG_ANALYTICS_WORKSPACE_NAME }}"
146- fi
121+ # - name: List KeyVaults and Store in Array
122+ # id: list_keyvaults
123+ # run: |
147124
148- echo "Log analytics workspace resource purging completed successfully"
125+ # set -e
126+ # echo "Listing all KeyVaults in the resource group ${RESOURCE_GROUP_NAME}..."
127+
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: |
157+
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"
149168
150169
151- - name : Delete Bicep Deployment
152- if : success()
153- run : |
154- set -e
155- echo "Checking if resource group exists..."
156- rg_exists=$(az group exists --name ${{ env.RESOURCE_GROUP_NAME }})
157- if [ "$rg_exists" = "true" ]; then
158- echo "Resource group exist. Cleaning..."
159- az group delete \
160- --name ${{ env.RESOURCE_GROUP_NAME }} \
161- --yes \
162- --no-wait
163- echo "Resource group deleted... ${{ env.RESOURCE_GROUP_NAME }}"
164- else
165- echo "Resource group does not exists."
166- fi
167-
168-
169- - name : Wait for resource deletion to complete
170- run : |
171-
172- # List of keyvaults
173- KEYVAULTS="${{ env.KEYVAULTS }}"
174-
175- # Remove the surrounding square brackets, if they exist
176- stripped_keyvaults=$(echo "$KEYVAULTS" | sed 's/\[\|\]//g')
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: |
190+
191+ # # List of keyvaults
192+ # KEYVAULTS="${{ env.KEYVAULTS }}"
193+
194+ # # Remove the surrounding square brackets, if they exist
195+ # stripped_keyvaults=$(echo "$KEYVAULTS" | sed 's/\[\|\]//g')
177196
178- # Convert the comma-separated string into an array
179- IFS=',' read -r -a resources_to_check <<< "$stripped_keyvaults"
180-
181- # Append new resources to the array
182- resources_to_check+=("${{ env.LOG_ANALYTICS_WORKSPACE_NAME }}")
183-
184- echo "List of resources to check: ${resources_to_check[@]}"
185-
186- # Maximum number of retries
187- max_retries=3
188-
189- # Retry intervals in seconds (30, 60, 120)
190- retry_intervals=(30 60 120)
191-
192- # Retry mechanism to check resources
193- retries=0
194- while true; do
195- resource_found=false
196-
197- # Get the list of resources in YAML format again on each retry
198- resource_list=$(az resource list --resource-group ${{ env.RESOURCE_GROUP_NAME }} --output yaml)
199-
200- # Iterate through the resources to check
201- for resource in "${resources_to_check[@]}"; do
202- echo "Checking resource: $resource"
203- if echo "$resource_list" | grep -q "name: $resource"; then
204- echo "Resource '$resource' exists in the resource group."
205- resource_found=true
206- else
207- echo "Resource '$resource' does not exist in the resource group."
208- fi
209- done
210-
211- # If any resource exists, retry
212- if [ "$resource_found" = true ]; then
213- retries=$((retries + 1))
214- if [ "$retries" -gt "$max_retries" ]; then
215- echo "Maximum retry attempts reached. Exiting."
216- break
217- else
218- # Wait for the appropriate interval for the current retry
219- echo "Waiting for ${retry_intervals[$retries-1]} seconds before retrying..."
220- sleep ${retry_intervals[$retries-1]}
221- fi
222- else
223- echo "No resources found. Exiting."
224- break
225- fi
226- done
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
227246
228247
229- - name : Purging the Resources
230- if : success()
231- run : |
248+ # - name: Purging the Resources
249+ # if: success()
250+ # run: |
232251
233- set -e
234- # List of keyvaults
235- KEYVAULTS="${{ env.KEYVAULTS }}"
252+ # set -e
253+ # # List of keyvaults
254+ # KEYVAULTS="${{ env.KEYVAULTS }}"
236255
237- # Remove the surrounding square brackets, if they exist
238- stripped_keyvaults=$(echo "$KEYVAULTS" | sed 's/\[\|\]//g')
256+ # # Remove the surrounding square brackets, if they exist
257+ # stripped_keyvaults=$(echo "$KEYVAULTS" | sed 's/\[\|\]//g')
239258
240- # Convert the comma-separated string into an array
241- IFS=',' read -r -a keyvault_array <<< "$stripped_keyvaults"
242-
243- echo "Using KeyVaults Array..."
244- for keyvault_name in "${keyvault_array[@]}"; do
245- echo "Processing KeyVault: $keyvault_name"
246- # Check if the KeyVault is soft-deleted
247- deleted_vaults=$(az keyvault list-deleted --query "[?name=='$keyvault_name']" -o json --subscription ${{ secrets.AZURE_SUBSCRIPTION_ID }})
248-
249- # If the KeyVault is found in the soft-deleted state, purge it
250- if [ "$(echo "$deleted_vaults" | jq length)" -gt 0 ]; then
251- echo "KeyVault '$keyvault_name' is soft-deleted. Proceeding to purge..."
252- # Purge the KeyVault
253- if az keyvault purge --name "$keyvault_name" --no-wait; then
254- echo "Successfully purged KeyVault '$keyvault_name'."
255- else
256- echo "Failed to purge KeyVault '$keyvault_name'."
257- fi
258- else
259- echo "KeyVault '$keyvault_name' is not soft-deleted. No action taken."
260- fi
261- done
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
0 commit comments