-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathazure_cicd.yml
More file actions
230 lines (191 loc) · 7.74 KB
/
azure_cicd.yml
File metadata and controls
230 lines (191 loc) · 7.74 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
trigger:
branches:
include:
- main
paths:
include:
- src/ContentProcessor/**
# When multiple commits land quickly on main, only run the latest.
batch: true
pr: none
pool:
vmImage: ubuntu-latest
variables:
azureServiceConnection: 'ado-pipeline-fed'
resourceGroup: 'rg-cps-v2'
containerAppsEnv: 'cps-apps-env'
containerAppName: 'containerapp-cps'
acrName: 'cpsv2'
acrLoginServer: 'cpsv2.azurecr.io'
imageRepository: 'contentprocessor'
appConfigEndpoint: 'https://appconfig-cps.azconfig.io'
dockerfilePath: 'Dockerfile'
dockerContext: '.'
imageTag: '$(Build.BuildId)'
steps:
- 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 NO ingress.
- task: AzureCLI@2
displayName: Deploy to Azure Container Apps (main only, no 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 (ingress disabled => no 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)" \
--ingress disabled \
--only-show-errors 1>/dev/null
fi
wait_for_app_ready "$(containerAppName)" "$(resourceGroup)" 60 10
# Hard-disable ingress to guarantee no external port is opened.
retry_on_inprogress 30 az containerapp ingress disable \
--name "$(containerAppName)" \
--resource-group "$(resourceGroup)" \
--only-show-errors 1>/dev/null || true
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)" \
--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