-
Notifications
You must be signed in to change notification settings - Fork 796
Expand file tree
/
Copy pathgui-deploy.yml
More file actions
286 lines (251 loc) · 12.2 KB
/
Copy pathgui-deploy.yml
File metadata and controls
286 lines (251 loc) · 12.2 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
# CI/CD pipeline for CoPyRIT GUI deployment.
#
# Triggers on changes to GUI-relevant paths on the main branch.
# Builds Docker image, pushes to ACR, deploys to test ACA environment.
# Production deployment is opt-in via the deployToProd parameter.
#
# All infrastructure details (IDs, connection strings, resource names) are
# stored in ADO variable groups — nothing sensitive appears in this file.
# Required variable groups:
# - copyrit-gui-common (azureServiceConnection, acrName, acrLoginServer, imageName)
# - copyrit-gui-test (resourceGroup, appName, entraTenantId, entraClientId,
# allowedGroupObjectIds, sqlServerFqdn, sqlDatabaseName,
# keyVaultResourceId, acrResourceId, enablePrivateEndpoint, enableOtel)
# - copyrit-gui-prod (same keys as test, with production values)
trigger:
branches:
include:
- main
paths:
include:
- pyrit/backend/**
- frontend/**
- docker/**
- infra/**
pr: none
parameters:
- name: deployToProd
displayName: 'Deploy to production'
type: boolean
default: false
# Service connection must be a compile-time variable (not from a variable group)
# because azureSubscription is validated during YAML parsing.
variables:
azureServiceConnection: 'copyrit-gui-azure'
stages:
# ──────────────────────────────────────────────
# Stage 0: Validate runtime inputs
# ──────────────────────────────────────────────
- stage: ValidateInputs
displayName: 'Validate Inputs'
pool:
vmImage: 'ubuntu-latest'
jobs:
- job: ValidateProdBranchGate
displayName: 'Validate production branch gate'
steps:
- checkout: none
- task: Bash@3
displayName: 'Fail if prod deploy requested from non-release branch'
inputs:
targetType: 'inline'
script: |
set -euo pipefail
DEPLOY_TO_PROD='${{ parameters.deployToProd }}'
DEPLOY_TO_PROD_LOWER=$(echo "$DEPLOY_TO_PROD" | tr '[:upper:]' '[:lower:]')
echo "Build.SourceBranch=$BUILD_SOURCEBRANCH"
echo "deployToProd=$DEPLOY_TO_PROD"
if [[ "$DEPLOY_TO_PROD_LOWER" == "true" && "$BUILD_SOURCEBRANCH" != refs/heads/releases/* ]]; then
echo "##vso[task.logissue type=error]deployToProd can only be used from refs/heads/releases/* branches. Current branch: $BUILD_SOURCEBRANCH"
exit 1
fi
echo "Validation passed"
# ──────────────────────────────────────────────
# Stage 1: Build Docker image and push to ACR
# ──────────────────────────────────────────────
- stage: Build
displayName: 'Build and Push Image'
dependsOn: ValidateInputs
variables:
- group: copyrit-gui-common
pool:
vmImage: 'ubuntu-latest'
jobs:
- job: BuildAndPush
displayName: 'Build Docker image and push to ACR'
steps:
- checkout: self
fetchDepth: 1
- task: AzureCLI@2
displayName: 'Build and push Docker image'
inputs:
azureSubscription: '$(azureServiceConnection)'
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: |
set -euo pipefail
echo "=== Building devcontainer base image ==="
docker build \
-f .devcontainer/Dockerfile \
-t pyrit-devcontainer \
.devcontainer/
echo "=== Building production image ==="
docker build --no-cache \
-f docker/Dockerfile \
-t $(acrLoginServer)/$(imageName):$(Build.SourceVersion) \
-t $(acrLoginServer)/$(imageName):latest \
--build-arg BASE_IMAGE=pyrit-devcontainer \
--build-arg PYRIT_SOURCE=local \
--build-arg GIT_COMMIT=$(Build.SourceVersion) \
--build-arg GIT_MODIFIED=false \
.
echo "=== Pushing to ACR ==="
az acr login --name $(acrName)
docker push $(acrLoginServer)/$(imageName):$(Build.SourceVersion)
docker push $(acrLoginServer)/$(imageName):latest
echo "✅ Image pushed: $(acrLoginServer)/$(imageName):$(Build.SourceVersion)"
# ──────────────────────────────────────────────
# Stage 2: Deploy to test environment
# ──────────────────────────────────────────────
- stage: DeployTest
displayName: 'Deploy to Test'
dependsOn: Build
variables:
- group: copyrit-gui-common
- group: copyrit-gui-test
pool:
vmImage: 'ubuntu-latest'
jobs:
- deployment: DeployToTest
displayName: 'Deploy to test environment'
environment: 'copyrit-test'
strategy:
runOnce:
deploy:
steps:
- checkout: self
fetchDepth: 1
- task: AzureCLI@2
displayName: 'Deploy Bicep to test'
inputs:
azureSubscription: '$(azureServiceConnection)'
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: |
set -euo pipefail
az deployment group create \
--resource-group $(resourceGroup) \
--template-file $(Build.SourcesDirectory)/infra/main.bicep \
--parameters appName=$(appName) \
--parameters containerImage=$(acrLoginServer)/$(imageName):$(Build.SourceVersion) \
--parameters entraTenantId=$(entraTenantId) \
--parameters entraClientId=$(entraClientId) \
--parameters allowedGroupObjectIds="$(allowedGroupObjectIds)" \
--parameters sqlServerFqdn=$(sqlServerFqdn) \
--parameters sqlDatabaseName=$(sqlDatabaseName) \
--parameters keyVaultResourceId=$(keyVaultResourceId) \
--parameters acrResourceId=$(acrResourceId) \
--parameters enablePrivateEndpoint=$(enablePrivateEndpoint) \
--parameters enableOtel=$(enableOtel) \
--parameters envSecretName=$(envSecretName)
- task: AzureCLI@2
displayName: 'Health check'
inputs:
azureSubscription: '$(azureServiceConnection)'
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: |
set -euo pipefail
IMAGE="$(acrLoginServer)/$(imageName):$(Build.SourceVersion)"
HEALTH=""
for i in {1..5}; do
HEALTH="$(az containerapp revision list \
--resource-group "$(resourceGroup)" \
--name "$(appName)" \
--query "[?properties.template.containers[0].image=='$IMAGE'] | sort_by(@,&properties.createdTime)[-1].properties.healthState" \
-o tsv)"
echo "Attempt $i/5 - Image: $IMAGE - Health: ${HEALTH:-<not-found>}"
if [[ "$HEALTH" == "Healthy" ]]; then
echo "✅ Test environment health check passed"
exit 0
fi
if [[ "$i" -lt 5 ]]; then
echo "Revision is not healthy yet. Waiting 2 minutes before retry..."
sleep 120
fi
done
echo "❌ Test environment health check failed after 5 attempts"
exit 1
# ──────────────────────────────────────────────
# Stage 3: Deploy to production (manual trigger)
# ──────────────────────────────────────────────
- stage: DeployProd
displayName: 'Deploy to Production'
dependsOn: DeployTest
condition: and(succeeded('DeployTest'), eq('${{ parameters.deployToProd }}', 'true'), startsWith(variables['Build.SourceBranch'], 'refs/heads/releases/'))
variables:
- group: copyrit-gui-common
- group: copyrit-gui-prod
pool:
vmImage: 'ubuntu-latest'
jobs:
- deployment: DeployToProd
displayName: 'Deploy to production environment'
environment: 'copyrit-prod'
strategy:
runOnce:
deploy:
steps:
- checkout: self
fetchDepth: 1
- task: AzureCLI@2
displayName: 'Deploy Bicep to prod'
inputs:
azureSubscription: '$(azureServiceConnection)'
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: |
set -euo pipefail
az deployment group create \
--resource-group $(resourceGroup) \
--template-file $(Build.SourcesDirectory)/infra/main.bicep \
--parameters appName=$(appName) \
--parameters containerImage=$(acrLoginServer)/$(imageName):$(Build.SourceVersion) \
--parameters entraTenantId=$(entraTenantId) \
--parameters entraClientId=$(entraClientId) \
--parameters allowedGroupObjectIds="$(allowedGroupObjectIds)" \
--parameters sqlServerFqdn=$(sqlServerFqdn) \
--parameters sqlDatabaseName=$(sqlDatabaseName) \
--parameters keyVaultResourceId=$(keyVaultResourceId) \
--parameters acrResourceId=$(acrResourceId) \
--parameters enablePrivateEndpoint=$(enablePrivateEndpoint) \
--parameters enableOtel=$(enableOtel) \
--parameters envSecretName=$(envSecretName)
- task: AzureCLI@2
displayName: 'Health check'
inputs:
azureSubscription: '$(azureServiceConnection)'
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: |
set -euo pipefail
IMAGE="$(acrLoginServer)/$(imageName):$(Build.SourceVersion)"
HEALTH=""
for i in {1..5}; do
HEALTH="$(az containerapp revision list \
--resource-group "$(resourceGroup)" \
--name "$(appName)" \
--query "[?properties.template.containers[0].image=='$IMAGE'] | sort_by(@,&properties.createdTime)[-1].properties.healthState" \
-o tsv)"
echo "Attempt $i/5 - Image: $IMAGE - Health: ${HEALTH:-<not-found>}"
if [[ "$HEALTH" == "Healthy" ]]; then
echo "✅ Production health check passed"
exit 0
fi
if [[ "$i" -lt 5 ]]; then
echo "Revision is not healthy yet. Waiting 2 minutes before retry..."
sleep 120
fi
done
echo "❌ Production health check failed after 5 attempts"
exit 1