This repository has been archived by the owner on Oct 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
azure-pipelines.yml
243 lines (218 loc) · 10 KB
/
azure-pipelines.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
trigger:
batch: true
branches:
include:
- main
pr:
- main
pool:
vmImage: ubuntu-latest
variables:
- name: 'BuildId'
value: '$(Build.SourceVersion)'
- name: 'BuildTag'
value: '$(Build.BuildNumber)'
- name: 'InfraDirectory'
value: '$(System.DefaultWorkingDirectory)/infra'
- name: 'DistDirectory'
value: '$(System.DefaultWorkingDirectory)/dist'
# This variable group is used to define "default" variable values that can be overridden by "environment" variables
- group: na-js-env-vars
# The "preview" environment is the default environment
- group: na-js-env-vars-preview
# The "production" environment is targeted when the source branch is `main`
- ${{ if eq(variables['Build.SourceBranch'], 'refs/heads/main') }}:
- group: na-js-env-vars-prod
# This is the ARM deployment name
- name: 'DeploymentName'
value: $[format('{0}-{1}', variables['EnvironmentName'], variables['Build.BuildNumber'])]
# The `DeployEnv` is used in deployment jobs - `AzureEnvironment` is the name of the Environment in Azure DevOps, which could be different to the `EnvironmentName`
- name: 'DeployEnv'
value: $[coalesce(variables['AzureEnvironment'], variables['EnvironmentName'])]
# If no "shared" resource group is defined, then set it to the "environment" resource group
- name: 'SharedResourceGroupName'
value: $[coalesce(variables['AzureSharedResourceGroup'], variables['AzureResourceGroup'])]
stages:
- stage: 'BuildInfrastructure'
jobs:
- job: 'BuildArmTemplate'
steps:
# The Bicep build command fails if the output directory doesn't exist
- pwsh: md "$(InfraDirectory)"
displayName: 'Create infra directory'
- task: AzureCLI@2
displayName: 'Build ARM template from bicep file'
inputs:
azureSubscription: '$(AzureServiceConnection)'
scriptType: pscore
scriptLocation: inlineScript
workingDirectory: '$(System.DefaultWorkingDirectory)/.azure/infra'
inlineScript: |
az bicep build -f main.bicep --outdir "$(InfraDirectory)"
# Replace tokens in the ARM parameters template file with relevant values for this build
- pwsh: |
. ./Set-Tokens.ps1
Set-Tokens `
-InputFile main.parameters.json.template `
-OutputFile "$(InfraDirectory)/main.parameters.json" `
-Tokens @{ `
resourcePrefix="$(AzureResourcePrefix)"; `
environment="$(EnvironmentName)"; `
sharedResourceGroupName="$(SharedResourceGroupName)"; `
buildId="$(BuildId)"; `
buildTag="$(BuildTag)"; `
webAppSkuName="$(WebAppSkuName)"; `
webAppSkuCapacity=$(WebAppSkuCapacity); `
webAppSlotName="$(WebAppSlotName)"; `
webAppDomainName="$(WebAppDomainName)"; `
webAppCertName="$(WebAppCertName)" `
}
workingDirectory: '$(System.DefaultWorkingDirectory)/.azure/infra'
displayName: 'Create ARM parameters file'
# Publish the ARM template and parameters as build artifacts
- publish: '$(InfraDirectory)'
displayName: 'Publish artifacts'
artifact: infra
# Run a What-If deployment to aid in reviewing the deployment during approval checks
- task: AzureCLI@2
displayName: 'What-If ARM template'
inputs:
azureSubscription: '$(AzureServiceConnection)'
scriptType: pscore
scriptLocation: inlineScript
workingDirectory: '$(InfraDirectory)'
inlineScript: |
az account show
az deployment group create -w -n "$(DeploymentName)" -g $(AzureResourceGroup) -f main.json -p `@main.parameters.json
- stage: 'DeployInfrastructure'
jobs:
- deployment: 'DeployArmTemplate'
environment: '$(DeployEnv)'
strategy:
runOnce:
deploy:
steps:
- task: CopyFiles@2
displayName: 'Copy infra artifacts to workspace'
inputs:
SourceFolder: '$(Pipeline.Workspace)/infra'
TargetFolder: '$(InfraDirectory)'
Contents: '**'
- task: AzureCLI@2
displayName: 'Run ARM template'
inputs:
azureSubscription: '$(AzureServiceConnection)'
scriptType: pscore
scriptLocation: inlineScript
workingDirectory: '$(InfraDirectory)'
inlineScript: |
az account show
az deployment group create -n "$(DeploymentName)" -g $(AzureResourceGroup) -f main.json -p `@main.parameters.json
- stage: 'BuildNextApp'
jobs:
- job: 'BuildNextApp'
steps:
# Use the app settings output from the ARM deployment to set environment variables that are required for our Next app build
- task: AzureCLI@2
displayName: 'Get ARM outputs'
inputs:
azureSubscription: '$(AzureServiceConnection)'
scriptType: pscore
scriptLocation: inlineScript
inlineScript: |
$webAppSettings = (az deployment group show -n "$(DeploymentName)" -g $(AzureResourceGroup) --query properties.outputs.webAppSettings.value | ConvertFrom-Json -AsHashtable)
foreach ($setting in $webAppSettings.GetEnumerator()) { `
$key=$setting.Key; $value=$setting.Value; `
Write-Output("##vso[task.setvariable variable=${key};]${value}") `
}
# This creates an env file by setting values on each key in the template that has a matching key in the pipeline environment
# This is so we don't have to pass each setting to the Docker build as a build arg
- task: EnvTransform@0
displayName: 'Create env file for build'
inputs:
inputType: 'file'
inputFile: '$(System.DefaultWorkingDirectory)/.env.template'
outputFile: '$(System.DefaultWorkingDirectory)/.env.local'
- task: DockerCompose@0
displayName: 'Build Next app container images'
inputs:
action: Build services
azureSubscriptionEndpoint: '$(AzureServiceConnection)'
azureContainerRegistry: '$(DOCKER_REGISTRY_SERVER)'
projectName: '$(DOCKER_IMAGE_NAME)'
dockerComposeFile: docker-compose.yml
qualifyImageNames: true
additionalImageTags: '$(DOCKER_IMAGE_TAG)'
includeLatestTag: true
- task: DockerCompose@0
displayName: 'Push Next app container image to container registry'
inputs:
action: Push services
azureSubscriptionEndpoint: '$(AzureServiceConnection)'
azureContainerRegistry: '$(DOCKER_REGISTRY_SERVER)'
projectName: '$(DOCKER_IMAGE_NAME)'
dockerComposeFile: docker-compose.yml
qualifyImageNames: true
additionalImageTags: '$(DOCKER_IMAGE_TAG)'
includeLatestTag: true
# The next task fails if the output directory doesn't exist
- pwsh: md "$(DistDirectory)"
displayName: 'Create dist directory'
# Replace tokens in the app service docker compose file with relevant values for this build
# Locally docker compose CLI would do this for us, but app services does not expand these tokens
- pwsh: |
. ../infra/Set-Tokens.ps1
Set-Tokens `
-InputFile docker-compose.appservice.yml `
-OutputFile "$(DistDirectory)/docker-compose.appservice.yml" `
-StartTokenPattern "\$\{" `
-EndTokenPattern "\}" `
-Tokens @{ `
DOCKER_REGISTRY_SERVER="$(DOCKER_REGISTRY_SERVER)"; `
DOCKER_IMAGE_NAME="$(DOCKER_IMAGE_NAME)"; `
DOCKER_IMAGE_TAG="$(DOCKER_IMAGE_TAG)" `
}
workingDirectory: '$(System.DefaultWorkingDirectory)/.azure/web-app'
displayName: 'Create App Service Docker Compose file'
# Publish the dist outputs as artifacts
- publish: '$(DistDirectory)'
displayName: 'Publish artifacts'
artifact: dist
- stage: 'DeployNextApp'
jobs:
- deployment: 'DeployNextApp'
environment: '$(DeployEnv)'
strategy:
runOnce:
deploy:
steps:
# Get the name of the app service we are deploying to from the ARM deployment outputs
- task: AzureCLI@2
displayName: 'Get ARM outputs'
inputs:
azureSubscription: '$(AzureServiceConnection)'
scriptType: pscore
scriptLocation: inlineScript
inlineScript: |
$outputs = (az deployment group show -n "$(DeploymentName)" -g $(AzureResourceGroup) --query properties.outputs | ConvertFrom-Json)
$webAppName = $outputs.webAppName.value
Write-Output("##vso[task.setvariable variable=WebAppName;]$webAppName")
# Update app service container settings to pull new images from the registry
# Using AzureCLI task instead of AzureWebAppContainer task because AzureWebAppContainer incorrectly sets `windowsFxVersion` instead of `linuxFxVersion`
# See https://github.com/microsoft/azure-pipelines-tasks/issues/14805
- task: AzureCLI@2
displayName: 'Update web app container settings'
inputs:
azureSubscription: '$(AzureServiceConnection)'
scriptType: pscore
scriptLocation: inlineScript
inlineScript: |
$slotName = "$(WebAppSlotName)"
if ($slotName) {
# Deploy to slot
az webapp config container set --name "$(WebAppName)" --resource-group "$(SharedResourceGroupName)" --multicontainer-config-file "$(Pipeline.Workspace)/dist/docker-compose.appservice.yml" --multicontainer-config-type COMPOSE --slot $slotName
}
else {
# Deploy to production
az webapp config container set --name "$(WebAppName)" --resource-group "$(SharedResourceGroupName)" --multicontainer-config-file "$(Pipeline.Workspace)/dist/docker-compose.appservice.yml" --multicontainer-config-type COMPOSE
}