-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathazure_cicd.yaml
More file actions
313 lines (264 loc) · 12.1 KB
/
azure_cicd.yaml
File metadata and controls
313 lines (264 loc) · 12.1 KB
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
trigger:
branches:
include:
- main
- frontend_dev
paths:
include:
- src/ContentProcessorWeb/**
# When multiple commits land quickly on main, only run the latest.
batch: true
pr: none
pool:
vmImage: ubuntu-latest
variables:
azureServiceConnection: 'ado-pipeline-fed'
resourceGroupName: 'rg-cps-v2'
containerAppsEnv: 'cps-apps-env'
containerAppName: 'containerapp-cpsweb'
cpsApiContainerName: 'containerapp-cpsapi'
acrName: 'cpsv2'
acrLoginServer: 'cpsv2.azurecr.io'
imageRepository: 'contentprocessorweb'
appConfigEndpoint: 'https://appconfig-cps.azconfig.io'
appApiBaseUrl: 'https://containerapp-cpsapi.proudbeach-01741a45.westus2.azurecontainerapps.io'
dockerfilePath: 'Dockerfile'
dockerContext: '.'
imageTag: '$(Build.BuildId)'
steps:
- task: AzureCLI@2
inputs:
azureSubscription: '$(azureServiceConnection)' # Replace with your Azure Classic service connection name
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: |
# Log in to Azure Container Registry
az acr login --name $(acrName)
# Retrieve the API URL from the Azure Container App
cpsApiBaseUrl="https://$(az containerapp show --name $(cpsApiContainerName) --resource-group $(resourceGroupName) --query "properties.configuration.ingress.fqdn" -o tsv)"
echo "CPS API Base URL: $cpsApiBaseUrl"
# Replace $cpsApiBaseUrl in .env file with URL from Azure Container App
sed -i "s|\$API_BASE_URL|$cpsApiBaseUrl|g" .env
# DEUBG: Check the .env file to ensure the value has been replaced correctly
cat .env
# Build the Docker image
docker build -t $(acrLoginServer)/$(imageRepository):$(Build.BuildId) .
# Push the Docker image to ACR
docker push $(acrLoginServer)/$(imageRepository):$(Build.BuildId)
displayName: 'Build and push Docker image'
- task: AzureCLI@2
inputs:
azureSubscription: '$(azureServiceConnection)' # Replace with your Azure Classic service connection name
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: |
az acr repository show-tags --name $(acrName) --repository $(imageRepository) --output table
displayName: 'List ACR repository tags'
- task: AzureCLI@2
inputs:
azureSubscription: '$(azureServiceConnection)' # Replace with your Azure Classic service connection name
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: |
# Get the resource ID for the managed identity
managedIdentityResourceId=$(az identity show --name $(managedIdentityName) --resource-group $(resourceGroupName) --query 'id' -o tsv)
cpsApiBaseUrl="https://$(az containerapp show --name $(cpsApiContainerName) --resource-group $(resourceGroupName) --query "properties.configuration.ingress.fqdn" -o tsv)"
echo "CPS API Base URL deploy: $cpsApiBaseUrl"
# Check if the container app exists
if az containerapp show --name $(containerAppName) --resource-group $(resourceGroupName); then
# Update the container app with the new image
az containerapp update \
--name $(containerAppName) \
--resource-group $(resourceGroupName) \
--image $(acrLoginServer)/$(imageRepository):$(Build.BuildId) \
--cpu 4.0 \
--memory 8.0Gi \
--set-env-vars APP_API_BASE_URL=$cpsApiBaseUrl APP_WEB_CLIENT_ID=$(appWebClientId) APP_WEB_AUTHORITY=$(appWebAuthority) APP_WEB_SCOPE=$(appWebScope) APP_API_SCOPE=$(appApiScope) APP_AUTH_ENABLED=false
else
# Create the container app with the new image and registry settings
az containerapp create \
--name $(containerAppName) \
--resource-group $(resourceGroupName) \
--environment $(environmentName) \
--image $(acrLoginServer)/$(imageRepository):$(Build.BuildId) \
--cpu 4.0 \
--memory 8.0Gi \
--registry-server $(acrLoginServer) \
--registry-identity $managedIdentityResourceId \
--ingress external \
--env-vars APP_API_BASE_URL=$cpsApiBaseUrl APP_WEB_CLIENT_ID=$(appWebClientId) APP_WEB_AUTHORITY=$(appWebAuthority) APP_WEB_SCOPE=$(appWebScope) APP_API_SCOPE=$(appApiScope) APP_AUTH_ENABLED=false
fi
displayName: 'Deploy container to Azure Container App'
# - checkout: self
# fetchDepth: 1
# Build + push to ACR (main only). PRs will build only.
# - task: AzureCLI@2
# displayName: Build image (PR + main)
# inputs:
# azureSubscription: '$(azureServiceConnection)'
# scriptType: bash
# scriptLocation: inlineScript
# inlineScript: |
# set -euo pipefail
# IMAGE="$(acrLoginServer)/$(imageRepository):$(imageTag)"
# echo "Building $IMAGE"
# az acr login --name "$(acrName)"
# az acr show --name "$(acrName)"
# docker build -f "$(dockerfilePath)" -t "$IMAGE" "$(dockerContext)"
# - task: AzureCLI@2
# displayName: Push image to ACR (main only)
# condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/main'))
# inputs:
# azureSubscription: '$(azureServiceConnection)'
# scriptType: bash
# scriptLocation: inlineScript
# inlineScript: |
# set -euo pipefail
# IMAGE="$(acrLoginServer)/$(imageRepository):$(imageTag)"
# echo "Pushing $IMAGE"
# az acr login --name "$(acrName)"
# docker push "$IMAGE"
# # Deploy (update container app to the new image) with public ingress.
# - task: AzureCLI@2
# displayName: Deploy to Azure Container Apps (main only, external ingress)
# condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/main'))
# inputs:
# azureSubscription: '$(azureServiceConnection)'
# scriptType: bash
# scriptLocation: inlineScript
# inlineScript: |
# set -euo pipefail
# wait_for_app_ready() {
# local name="$1"
# local rg="$2"
# local max_attempts="${3:-60}"
# local sleep_seconds="${4:-10}"
# for i in $(seq 1 "$max_attempts"); do
# state=$(az containerapp show --name "$name" --resource-group "$rg" --query properties.provisioningState -o tsv 2>/dev/null || true)
# if [ -z "$state" ]; then
# echo "Waiting for container app '$name' to exist... ($i/$max_attempts)"
# elif [ "$state" != "InProgress" ]; then
# echo "Container app provisioningState=$state"
# return 0
# else
# echo "Container app provisioningState=InProgress; waiting... ($i/$max_attempts)"
# fi
# sleep "$sleep_seconds"
# done
# echo "Timed out waiting for container app provisioning to complete."
# return 1
# }
# retry_on_inprogress() {
# local max_attempts="${1:-30}"
# shift
# for i in $(seq 1 "$max_attempts"); do
# set +e
# output=$("$@" 2>&1)
# code=$?
# set -e
# if [ $code -eq 0 ]; then
# return 0
# fi
# echo "$output"
# if echo "$output" | grep -q "ContainerAppOperationInProgress"; then
# echo "Container app busy; retrying... ($i/$max_attempts)"
# sleep 15
# continue
# fi
# return $code
# done
# echo "Timed out retrying operation due to ContainerAppOperationInProgress."
# return 1
# }
# IMAGE="$(acrLoginServer)/$(imageRepository):$(imageTag)"
# echo "Deploying $IMAGE to $(containerAppName)"
# # Ensure extension is available on the hosted agent.
# az extension add --name containerapp --upgrade
# # Create the Container App if it doesn't exist (external ingress => public endpoint).
# if ! az containerapp show --name "$(containerAppName)" --resource-group "$(resourceGroup)" 1>/dev/null 2>&1; then
# az containerapp create \
# --name "$(containerAppName)" \
# --resource-group "$(resourceGroup)" \
# --environment "$(containerAppsEnv)" \
# --image "$IMAGE" \
# --env-vars "APP_CONFIG_ENDPOINT=$(appConfigEndpoint)" "APP_AUTH_ENABLED=false" "APP_API_BASE_URL=$(appApiBaseUrl)" \
# --ingress external \
# --target-port 3000 \
# --transport auto \
# --allow-insecure \
# --only-show-errors 1>/dev/null
# fi
# wait_for_app_ready "$(containerAppName)" "$(resourceGroup)" 60 10
# # Ensure ingress is enabled and points to the container's listening port.
# retry_on_inprogress 30 az containerapp ingress enable \
# --name "$(containerAppName)" \
# --resource-group "$(resourceGroup)" \
# --type external \
# --target-port 3000 \
# --transport auto \
# --allow-insecure \
# --only-show-errors 1>/dev/null
# wait_for_app_ready "$(containerAppName)" "$(resourceGroup)" 60 10
# # Configure managed identity to pull from ACR.
# retry_on_inprogress 30 az containerapp identity assign \
# --name "$(containerAppName)" \
# --resource-group "$(resourceGroup)" \
# --system-assigned \
# --only-show-errors 1>/dev/null
# wait_for_app_ready "$(containerAppName)" "$(resourceGroup)" 60 10
# PRINCIPAL_ID=$(az containerapp show \
# --name "$(containerAppName)" \
# --resource-group "$(resourceGroup)" \
# --query identity.principalId \
# -o tsv)
# ACR_ID=$(az acr show \
# --name "$(acrName)" \
# --query id \
# -o tsv)
# # Validate that the container app identity already has AcrPull on the registry.
# # NOTE: Using --assignee can trigger a Graph API lookup and emit warnings in locked-down tenants.
# # Filtering by principalId avoids Graph entirely.
# if ! az role assignment list \
# --scope "$ACR_ID" \
# --query "[?principalId=='$PRINCIPAL_ID' && roleDefinitionName=='AcrPull'] | length(@)" \
# -o tsv | grep -q '^1$'; then
# echo "ERROR: Container App managed identity does not have 'AcrPull' on ACR scope: $ACR_ID"
# echo "Ask an Azure admin to run:"
# echo " az role assignment create --assignee $PRINCIPAL_ID --role AcrPull --scope $ACR_ID"
# exit 1
# fi
# retry_on_inprogress 30 az containerapp registry set \
# --name "$(containerAppName)" \
# --resource-group "$(resourceGroup)" \
# --server "$(acrLoginServer)" \
# --identity system \
# --only-show-errors 1>/dev/null
# wait_for_app_ready "$(containerAppName)" "$(resourceGroup)" 60 10
# # Roll out the new image.
# retry_on_inprogress 40 az containerapp update \
# --name "$(containerAppName)" \
# --resource-group "$(resourceGroup)" \
# --image "$IMAGE" \
# --set-env-vars "APP_CONFIG_ENDPOINT=$(appConfigEndpoint)" "APP_AUTH_ENABLED=false" "APP_API_BASE_URL=$(appApiBaseUrl)"\
# --only-show-errors
# wait_for_app_ready "$(containerAppName)" "$(resourceGroup)" 80 10
# echo "Container image configured on app:"
# az containerapp show \
# --name "$(containerAppName)" \
# --resource-group "$(resourceGroup)" \
# --query "properties.template.containers[0].{name:name,image:image}" \
# -o table
# READY_REV=$(az containerapp show \
# --name "$(containerAppName)" \
# --resource-group "$(resourceGroup)" \
# --query properties.latestReadyRevisionName \
# -o tsv)
# if [ -n "$READY_REV" ]; then
# echo "Latest ready revision: $READY_REV"
# az containerapp revision show \
# --name "$(containerAppName)" \
# --resource-group "$(resourceGroup)" \
# --revision "$READY_REV" \
# --query "properties.template.containers[0].image" \
# -o tsv
# fi