This repository has been archived by the owner on May 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathazure-pipelines.yml
290 lines (268 loc) · 14.8 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
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
# Jekyll site
# Package your Jekyll site using the jekyll/builder Docker container image.
# Add steps that build, test, save build artifacts, deploy, and more:
# https://aka.ms/yaml
variables:
azureSubscription: 'Azure Resource Manager'
jobs:
- job: Build
pool:
vmImage: ubuntu-latest
steps:
- task: Bash@3
inputs:
targetType: 'inline'
script: |
chmod 777 "$BINARIES_DIRECTORY"
env:
BINARIES_DIRECTORY: $(Build.BinariesDirectory)
- task: DockerCompose@0
displayName: 'Run Jekyll & Test Links'
inputs:
action: Run services
containerRegistryType: Container Registry
dockerComposeFile: docker-compose-ci.yml
dockerComposeFileArgs: |
SOURCES_DIRECTORY=$(Build.SourcesDirectory)
BINARIES_DIRECTORY=$(Build.BinariesDirectory)
projectName: $(Build.Repository.Name)
abortOnContainerExit: true
detached: false
- publish: $(Build.BinariesDirectory)
artifact: www
displayName: Publish Site
condition: and(succeeded(), or(eq(variables['Build.SourceBranch'], 'refs/heads/master'), eq(variables['Build.Reason'], 'PullRequest')))
- deployment: UploadArtifact
environment: www-dev
pool:
vmImage: windows-latest
dependsOn: Build
condition: and(succeeded(), or(eq(variables['Build.SourceBranch'], 'refs/heads/master'), eq(variables['Build.Reason'], 'PullRequest')))
strategy:
runOnce:
deploy:
steps:
- download: current
artifact: www
- checkout: none
- task: AzureFileCopy@3
inputs:
sourcePath: $(Pipeline.Workspace)/www
azureSubscription: $(azureSubscription)
destination: azureBlob
storage: missionbitwww
containerName: '$web'
blobPrefix: $(Build.SourceVersion)
- deployment: DeployPR
environment: www-dev
pool:
# windows-latest not currently compatible with AzureCLI@2
vmImage: vs2017-win2016
dependsOn: Build
condition: and(succeeded(), eq(variables['Build.Reason'], 'PullRequest'))
variables:
prHost: pr$(system.pullRequest.pullRequestNumber).dev.missionbit.org
prEnvironment: pr$(system.pullRequest.pullRequestNumber)-dev-missionbit-org
strategy:
runOnce:
deploy:
steps:
- download: none
- checkout: none
- task: AzureCLI@2
displayName: Azure CLI
inputs:
azureSubscription: $(azureSubscription)
scriptType: ps
scriptLocation: inlineScript
inlineScript: |
$name = $env:prEnvironment
az extension add --name front-door
$frontends = $(az network front-door frontend-endpoint list -f missionbit-dev -g www-dev | ConvertFrom-Json)
$frontend = $frontends.where({ $_.name -eq $name })[0]
if (!$frontend) {
echo "Creating new frontend for $env:prHost to /$env:Build_SourceVersion/"
$frontend = $(az network front-door frontend-endpoint create -f missionbit-dev -g www-dev --host-name $env:prHost --name $name | ConvertFrom-Json)
echo "Enabling https"
az network front-door frontend-endpoint enable-https -f missionbit-dev -g www-dev -n $name
$frontends = $(az network front-door frontend-endpoint list -f missionbit-dev -g www-dev | ConvertFrom-Json)
echo "Updating redirect-to-https rule with all frontends"
az network front-door routing-rule create -f missionbit-dev -g www-dev --accepted-protocols Http -n redirect-to-https --frontend-endpoints $($frontends.id) --route-type Redirect --redirect-type PermanentRedirect --redirect-protocol HttpsOnly
}
echo "Updating routing-rule $name to /$env:Build_SourceVersion/"
az network front-door routing-rule create --front-door-name missionbit-dev --resource-group www-dev --accepted-protocols Https --forwarding-protocol HttpsOnly --name $name --backend-pool blob --frontend-endpoints $name --route-type Forward --custom-forwarding-path "/$env:Build_SourceVersion/"
- deployment: GitHubPR
environment: www-dev
pool:
# windows-latest not currently compatible with AzureCLI@2
vmImage: vs2017-win2016
dependsOn:
- UploadArtifact
- DeployPR
condition: and(succeeded(), eq(variables['Build.Reason'], 'PullRequest'))
variables:
prHost: pr$(system.pullRequest.pullRequestNumber).dev.missionbit.org
prEnvironment: pr$(system.pullRequest.pullRequestNumber)-dev-missionbit-org
strategy:
runOnce:
deploy:
steps:
- download: none
- checkout: none
- task: GitHubDeployment@0
inputs:
gitHubConnection: github.com_etrepum
repositoryName: $(Build.Repository.Name)
ref: $(system.pullRequest.sourceBranch)
task: "deploy"
autoMerge: true
environment: $(prEnvironment)
description: "$(Build.SourceVersionMessage)"
transientEnvironment: true
state: 'success'
logUrl: https://$(prHost)/
environmentUrl: https://$(prHost)/
autoInactive: true
- deployment: DeployMasterFrontDoor
environment: www
pool:
# windows-latest not currently compatible with AzureCLI@2
vmImage: vs2017-win2016
dependsOn: UploadArtifact
condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
strategy:
runOnce:
deploy:
steps:
- download: none
- checkout: none
- task: AzureCLI@2
name: FrontDoor
displayName: Azure CLI
inputs:
azureSubscription: $(azureSubscription)
scriptType: ps
scriptLocation: inlineScript
inlineScript: |
az extension add --name front-door
echo "Updating 404 document"
az storage blob service-properties update --account-name missionbitwww --static-website --404-document "$(Build.SourceVersion)/404.html"
echo "Updating missionbit-dev routing-rule serve-content to /$env:Build_SourceVersion/"
az network front-door routing-rule create -f missionbit-dev -g www-dev --accepted-protocols Https --forwarding-protocol HttpsOnly -n serve-content --backend-pool blob --frontend-endpoints missionbit-dev-azurefd-net --route-type Forward --custom-forwarding-path "/$env:Build_SourceVersion/"
echo "Fetching old missionbit-www routing-rule serve-content prefix"
$oldPrefix = (az network front-door routing-rule show --front-door-name missionbit-www --resource-group www --name serve-content | ConvertFrom-Json).routeConfiguration.customForwardingPath.TrimStart("/")
echo "Old prefix $oldPrefix"
echo "Updating missionbit-www routing-rule serve-content to /$env:Build_SourceVersion/"
az network front-door routing-rule create --front-door-name missionbit-www --resource-group www --accepted-protocols Https --forwarding-protocol HttpsOnly --name serve-content --backend-pool blob --frontend-endpoints missionbit-www-azurefd-net www-missionbit-org --route-type Forward --custom-forwarding-path "/$env:Build_SourceVersion/" --dynamic-compression Enabled --caching Enabled --query-parameter-strip-directive StripNone
echo "Purging cache for missionbit-www"
az network front-door purge-endpoint -g www -n missionbit-www --content-paths '/*'
echo "vso[task.setvariable variable=oldPrefix;isSecret=false;isOutput=true;]$oldPrefix"
echo "##vso[task.setvariable variable=oldPrefix;isSecret=false;isOutput=true;]$oldPrefix"
- deployment: DeployMasterCleanupDev
environment: www-dev
pool:
# windows-latest not currently compatible with AzureCLI@2
vmImage: vs2017-win2016
dependsOn:
- UploadArtifact
- DeployMasterFrontDoor
variables:
oldPrefix: $[ dependencies.DeployMasterFrontDoor.outputs['DeployMasterFrontDoor.FrontDoor.oldPrefix'] ]
condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
strategy:
runOnce:
deploy:
steps:
- download: none
- checkout: none
- task: AzureCLI@2
displayName: Azure CLI
inputs:
azureSubscription: $(azureSubscription)
scriptType: ps
scriptLocation: inlineScript
inlineScript: |
az extension add --name front-door
$frontends = $(az network front-door frontend-endpoint list -f missionbit-dev -g www-dev | ConvertFrom-Json)
$envs = @{ "missionbit-dev-azurefd-net" = 0; }
$frontendMap = @{}
$frontends.ForEach({ $frontendMap[$_.name] = $true })
$headers = @{ "Accept" = "application/vnd.github.shadow-cat-preview+json"; }
echo "Getting number of latest PR"
$url = "https://api.github.com/repos/missionbit/missionbit.github.io/pulls?state=all"
$allPrs = (Invoke-WebRequest -Uri $url -Headers $Headers -Method Get -ErrorAction Stop | ConvertFrom-Json)
$latestPr = $allPrs[0].number
echo "Latest PR is ${latestPr}"
$prealloc = (1..3).ForEach({ "pr$($_ + $latestPr)-dev-missionbit-org" }).where({
$envs[$_] = 0;
-Not $frontEndMap.ContainsKey($_)
})
$prealloc.ForEach({
$name = $_
echo "Pre-allocating frontend for $name"
$frontend = $(az network front-door frontend-endpoint create -f missionbit-dev -g www-dev --host-name $name.Replace("-", ".") --name $name | ConvertFrom-Json)
echo "Preallocating routing rule $name"
az network front-door routing-rule create --front-door-name missionbit-dev --resource-group www-dev --accepted-protocols Https --forwarding-protocol HttpsOnly --name $name --backend-pool blob --frontend-endpoints $name --route-type Forward --custom-forwarding-path "/prealloc/"
echo "Enabling https"
az network front-door frontend-endpoint enable-https -f missionbit-dev -g www-dev -n $name
})
if ($prealloc) {
$frontends = $(az network front-door frontend-endpoint list -f missionbit-dev -g www-dev | ConvertFrom-Json)
$frontends.ForEach({ $frontendMap[$_.name] = $true })
}
$allRoutes = @{ "missionbit-dev-azurefd-net" = $true }
$routes = (az network front-door routing-rule list -f missionbit-dev -g www-dev | ConvertFrom-Json)
$routes.ForEach({ $allRoutes[$_.name] = $true })
$frontends.where({ -Not $allRoutes.ContainsKey($_.name) }).ForEach({
$name = $_.name
echo "Preallocating missing routing rule $name"
az network front-door routing-rule create --front-door-name missionbit-dev --resource-group www-dev --accepted-protocols Https --forwarding-protocol HttpsOnly --name $name --backend-pool blob --frontend-endpoints $name --route-type Forward --custom-forwarding-path "/prealloc/"
})
echo "Getting list of open PRs"
$url = "https://api.github.com/repos/missionbit/missionbit.github.io/pulls"
$prs = (Invoke-WebRequest -Uri $url -Headers $Headers -Method Get -ErrorAction Stop | ConvertFrom-Json)
$prs.number.ForEach({ $envs["pr$_-dev-missionbit-org"] = $_ })
$closedFrontends = $frontends.where({ -Not $envs.ContainsKey($_.name) })
if ($closedFrontends -Or $prealloc) {
if ($closedFrontends) {
echo "Detected closed environments: $($closedFrontends.name)"
}
if ($prealloc) {
echo "New preallocated environments: $prealloc"
}
$openFrontends = $frontends.where({ $envs.ContainsKey($_.name) })
echo "Updating redirect-to-https rule with all open frontends"
az network front-door routing-rule create -f missionbit-dev -g www-dev --accepted-protocols Http -n redirect-to-https --frontend-endpoints $($openFrontends.id) --route-type Redirect --redirect-type PermanentRedirect --redirect-protocol HttpsOnly
}
$closedFrontends.ForEach({
echo "Removing routing rule $($_.name)"
az network front-door routing-rule delete -g www-dev -f missionbit-dev -n $_.name
echo "Removing frontend endpoint $($_.hostName)"
az network front-door frontend-endpoint delete -g www-dev -f missionbit-dev -n $_.name
})
echo "Cleaning up blob storage"
$routes = (az network front-door routing-rule list -f missionbit-dev -g www-dev | ConvertFrom-Json)
$validPrefix = @{ "$env:Build_SourceVersion/" = $true }
$validPrefix["$(oldPrefix)"] = $true
$routes.routeConfiguration.customForwardingPath.where({ $_ }).ForEach({
$validPrefix[$_.TrimStart("/")] = $true
})
echo "$($validPrefix.Count) valid blob prefixes"
echo "Old prefix: $(oldPrefix)"
echo "New prefix: $env:Build_SourceVersion/"
$earlier = (Get-Date).AddHours(-6)
$prefixes = (az storage blob list -c '$web' --account-name=missionbitwww --delimiter / | ConvertFrom-Json)
echo "All prefix count: $($prefixes.count)"
echo "Filtered prefix count: $($prefixes.name.where({ -Not $validPrefix.ContainsKey($_) }).count)"
foreach ($prefix in $prefixes.name.where({ -Not $validPrefix.ContainsKey($_) })) {
Write-Host "Deleting unused blob prefix $prefix before $earlier"
$blobs = (az storage blob list -c '$web' --account-name=missionbitwww --prefix $prefix --num-results 10 | ConvertFrom-Json)
$lastModified = [datetime]($blobs.properties.lastModified | measure -Maximum).Maximum
if ($lastModified -ge $earlier) {
Write-Host " $prefix Last-Modified $lastModified newer than $earlier, skipping delete"
} else {
Write-Host " $prefix Last-Modified $lastModified older than $earlier, deleting"
az storage remove -c '$web' --account-name=missionbitwww -n $prefix --recursive
}
$prefix
}