-
Notifications
You must be signed in to change notification settings - Fork 82
278 lines (228 loc) · 11 KB
/
CAdeploy.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
name: CI-Validate Deployment-Client Advisor
on:
push:
branches:
- main
paths:
- 'ClientAdvisor/**'
schedule:
- cron: '0 6,18 * * *' # Runs at 6:00 AM and 6:00 PM GMT
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v3
- name: Setup Azure CLI
run: |
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
az --version # Verify installation
- name: Login to Azure
run: |
az login --service-principal -u ${{ secrets.AZURE_CLIENT_ID }} -p ${{ secrets.AZURE_CLIENT_SECRET }} --tenant ${{ secrets.AZURE_TENANT_ID }}
- name: Install Bicep CLI
run: az bicep install
- name: Generate Resource Group Name
id: generate_rg_name
run: |
echo "Generating a unique resource group name..."
TIMESTAMP=$(date +%Y%m%d%H%M%S)
COMMON_PART="pslautomationCli"
UNIQUE_RG_NAME="${COMMON_PART}${TIMESTAMP}"
echo "RESOURCE_GROUP_NAME=${UNIQUE_RG_NAME}" >> $GITHUB_ENV
echo "Generated RESOURCE_GROUP_PREFIX: ${UNIQUE_RG_NAME}"
- name: Check and Create Resource Group
id: check_create_rg
run: |
echo "RESOURCE_GROUP: ${{ env.RESOURCE_GROUP_NAME }}"
set -e
echo "Checking if resource group exists..."
rg_exists=$(az group exists --name ${{ env.RESOURCE_GROUP_NAME }})
if [ "$rg_exists" = "false" ]; then
echo "Resource group does not exist. Creating..."
az group create --name ${{ env.RESOURCE_GROUP_NAME }} --location uksouth || { echo "Error creating resource group"; exit 1; }
else
echo "Resource group already exists."
fi
- name: Generate Unique Solution Prefix
id: generate_solution_prefix
run: |
set -e
COMMON_PART="pslc"
TIMESTAMP=$(date +%s)
UPDATED_TIMESTAMP=$(echo $TIMESTAMP | tail -c 3)
UNIQUE_SOLUTION_PREFIX="${COMMON_PART}${UPDATED_TIMESTAMP}"
echo "SOLUTION_PREFIX=${UNIQUE_SOLUTION_PREFIX}" >> $GITHUB_ENV
echo "Generated SOLUTION_PREFIX: ${UNIQUE_SOLUTION_PREFIX}"
- name: Deploy Bicep Template
id: deploy
run: |
set -e
az deployment group create \
--resource-group ${{ env.RESOURCE_GROUP_NAME }} \
--template-file ClientAdvisor/Deployment/bicep/main.bicep \
--parameters solutionPrefix=${{ env.SOLUTION_PREFIX }} cosmosLocation=eastus2
- name: List KeyVaults and Store in Array
id: list_keyvaults
run: |
set -e
echo "Listing all KeyVaults in the resource group ${RESOURCE_GROUP_NAME}..."
# Get the list of KeyVaults in the specified resource group
keyvaults=$(az resource list --resource-group ${{ env.RESOURCE_GROUP_NAME }} --query "[?type=='Microsoft.KeyVault/vaults'].name" -o tsv)
if [ -z "$keyvaults" ]; then
echo "No KeyVaults found in resource group ${RESOURCE_GROUP_NAME}."
echo "KEYVAULTS=[]" >> $GITHUB_ENV # If no KeyVaults found, set an empty array
else
echo "KeyVaults found: $keyvaults"
# Format the list into an array with proper formatting (no trailing comma)
keyvault_array="["
first=true
for kv in $keyvaults; do
if [ "$first" = true ]; then
keyvault_array="$keyvault_array\"$kv\""
first=false
else
keyvault_array="$keyvault_array,\"$kv\""
fi
done
keyvault_array="$keyvault_array]"
# Output the formatted array and save it to the environment variable
echo "KEYVAULTS=$keyvault_array" >> $GITHUB_ENV
fi
- name: Update PowerBI URL
if: success()
run: |
set -e
COMMON_PART="-app-service"
application_name="${{ env.SOLUTION_PREFIX }}${COMMON_PART}"
echo "Updating application: $application_name"
# Log the Power BI URL being set
echo "Setting Power BI URL: ${{ vars.VITE_POWERBI_EMBED_URL }}"
# Update the application settings
az webapp config appsettings set --name "$application_name" --resource-group "${{ env.RESOURCE_GROUP_NAME }}" --settings VITE_POWERBI_EMBED_URL="${{ vars.VITE_POWERBI_EMBED_URL }}"
# Restart the web app
az webapp restart --resource-group "${{ env.RESOURCE_GROUP_NAME }}" --name "$application_name"
echo "Power BI URL updated successfully for application: $application_name."
- name: Delete Bicep Deployment
if: success()
run: |
set -e
echo "Checking if resource group exists..."
rg_exists=$(az group exists --name ${{ env.RESOURCE_GROUP_NAME }})
if [ "$rg_exists" = "true" ]; then
echo "Resource group exist. Cleaning..."
az group delete \
--name ${{ env.RESOURCE_GROUP_NAME }} \
--yes \
--no-wait
echo "Resource group deleted... ${{ env.RESOURCE_GROUP_NAME }}"
else
echo "Resource group does not exists."
fi
- name: Wait for resource deletion to complete
run: |
# List of keyvaults
KEYVAULTS="${{ env.KEYVAULTS }}"
# Remove the surrounding square brackets, if they exist
stripped_keyvaults=$(echo "$KEYVAULTS" | sed 's/\[\|\]//g')
# Convert the comma-separated string into an array
IFS=',' read -r -a resources_to_check <<< "$stripped_keyvaults"
# Append new resources to the array
resources_to_check+=("${{ env.SOLUTION_PREFIX }}-openai" "${{ env.SOLUTION_PREFIX }}-cogser")
echo "List of resources to check: ${resources_to_check[@]}"
# Get the list of resources in YAML format
resource_list=$(az resource list --resource-group ${{ env.RESOURCE_GROUP_NAME }} --output yaml)
# Maximum number of retries
max_retries=3
# Retry intervals in seconds (30, 60, 120)
retry_intervals=(30 60 120)
# Retry mechanism to check resources
retries=0
while true; do
resource_found=false
# Iterate through the resources to check
for resource in "${resources_to_check[@]}"; do
echo "Checking resource: $resource"
if echo "$resource_list" | grep -q "name: $resource"; then
echo "Resource '$resource' exists in the resource group."
resource_found=true
else
echo "Resource '$resource' does not exist in the resource group."
fi
done
# If any resource exists, retry
if [ "$resource_found" = true ]; then
retries=$((retries + 1))
if [ "$retries" -ge "$max_retries" ]; then
echo "Maximum retry attempts reached. Exiting."
break
else
# Wait for the appropriate interval for the current retry
echo "Waiting for ${retry_intervals[$retries-1]} seconds before retrying..."
sleep ${retry_intervals[$retries-1]}
fi
else
echo "No resources found. Exiting."
break
fi
done
- name: Purging the Resources
if: success()
run: |
set -e
# Define variables
OPENAI_COMMON_PART="-openai"
openai_name="${{ env.SOLUTION_PREFIX }}${OPENAI_COMMON_PART}"
echo "Azure OpenAI: $openai_name"
MULTISERVICE_COMMON_PART="-cogser"
multiservice_account_name="${{ env.SOLUTION_PREFIX }}${MULTISERVICE_COMMON_PART}"
echo "Azure MultiService Account: $multiservice_account_name"
# Purge OpenAI Resource
echo "Purging the OpenAI Resource..."
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
echo "Failed to purge openai resource: $openai_name"
else
echo "Purged the openai resource: $openai_name"
fi
# Purge MultiService Account Resource
echo "Purging the MultiService Account Resource..."
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
echo "Failed to purge multiService account resource: $multiservice_account_name"
else
echo "Purged the multiService account resource: $multiservice_account_name"
fi
# Ensure KEYVAULTS is properly formatted as a comma-separated string
KEYVAULTS="${{ env.KEYVAULTS }}"
# Remove the surrounding square brackets, if they exist
stripped_keyvaults=$(echo "$KEYVAULTS" | sed 's/\[\|\]//g')
# Convert the comma-separated string into an array
IFS=',' read -r -a keyvault_array <<< "$stripped_keyvaults"
echo "Using KeyVaults Array..."
for keyvault_name in "${keyvault_array[@]}"; do
echo "Processing KeyVault: $keyvault_name"
# Check if the KeyVault is soft-deleted
deleted_vaults=$(az keyvault list-deleted --query "[?name=='$keyvault_name']" -o json --subscription ${{ secrets.AZURE_SUBSCRIPTION_ID }})
# If the KeyVault is found in the soft-deleted state, purge it
if [ "$(echo "$deleted_vaults" | jq length)" -gt 0 ]; then
echo "KeyVault '$keyvault_name' is soft-deleted. Proceeding to purge..."
az keyvault purge --name "$keyvault_name" --no-wait
else
echo "KeyVault '$keyvault_name' is not soft-deleted. No action taken."
fi
done
echo "Resource purging completed successfully"
- name: Send Notification on Failure
if: failure()
run: |
RUN_URL="https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
# Construct the email body
EMAIL_BODY=$(cat <<EOF
{
"body": "<p>Dear Team,</p><p>We would like to inform you that the Client Advisor Automation process has encountered an issue and has failed to complete successfully.</p><p><strong>Build URL:</strong> ${RUN_URL}<br> ${OUTPUT}</p><p>Please investigate the matter at your earliest convenience.</p><p>Best regards,<br>Your Automation Team</p>"
}
EOF
)
# Send the notification
curl -X POST "${{ secrets.LOGIC_APP_URL }}" \
-H "Content-Type: application/json" \
-d "$EMAIL_BODY" || echo "Failed to send notification"