-
-
Notifications
You must be signed in to change notification settings - Fork 0
305 lines (278 loc) · 11.3 KB
/
publish.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
name: publish
on:
push:
branches: ["main"]
# Publish semver tags as releases.
tags: ["v*.*.*"]
pull_request:
branches: ["main"]
env:
# Use docker.io for Docker Hub if empty
REGISTRY: ghcr.io
# github.repository as <account>/<repo>
SITE_IMAGE_NAME: ${{ github.repository }}
EDITOR_IMAGE_NAME: ${{ github.repository }}editor
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
id-token: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
# Workaround: https://github.com/docker/build-push-action/issues/461
- name: Setup Docker buildx
uses: docker/setup-buildx-action@6524bf65af31da8d45b59e8c27de4bd072b392f5
- name: Set commit hash
id: set-commit-hash
shell: bash
run: |
export COMMIT_HASH=${GITHUB_SHA:0:7}
echo "COMMIT_HASH=$COMMIT_HASH" >> $GITHUB_OUTPUT
# Login against a Docker registry except on PR
# https://github.com/docker/login-action
- name: Log into registry ${{ env.REGISTRY }}
if: github.event_name != 'pull_request'
uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
# Extract metadata (tags, labels) for Docker
# https://github.com/docker/metadata-action
- name: Extract Docker metadata (site)
id: meta-site
uses: docker/metadata-action@8e5442c4ef9f78752691e2d8f8d19755c6f78e81
with:
images: ${{ env.REGISTRY }}/${{ env.SITE_IMAGE_NAME }}
tags: |
type=sha,format=long,prefix=,priority=100
type=sha,prefix=,priority=80
type=ref,event=branch,priority=60
type=ref,event=pr,priority=40
# Build and push Docker image with Buildx (don't push on PR)
# https://github.com/docker/build-push-action
- name: Build and push Docker image (site)
id: build-and-push-site
uses: docker/build-push-action@5cd11c3a4ced054e52742c5fd54dca954e0edd85
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta-site.outputs.tags }}
file: "src/GordonBeemingCom/Dockerfile"
build-args: |
A_COMMIT_HASH=${{ steps.set-commit-hash.outputs.COMMIT_HASH }}
A_BRANCH_NAME=${{ github.ref_name }}
# Extract metadata (tags, labels) for Docker
# https://github.com/docker/metadata-action
- name: Extract Docker metadata (editor)
id: meta-editor
uses: docker/metadata-action@8e5442c4ef9f78752691e2d8f8d19755c6f78e81
with:
images: ${{ env.REGISTRY }}/${{ env.EDITOR_IMAGE_NAME }}
tags: |
type=sha,format=long,prefix=,priority=100
type=sha,prefix=,priority=80
type=ref,event=branch,priority=60
type=ref,event=pr,priority=40
# Build and push Docker image with Buildx (don't push on PR)
# https://github.com/docker/build-push-action
- name: Build and push Docker image (editor)
id: build-and-push-editor
uses: docker/build-push-action@5cd11c3a4ced054e52742c5fd54dca954e0edd85
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta-editor.outputs.tags }}
file: "src/GordonBeemingCom.Editor/Dockerfile"
build-args: |
A_COMMIT_HASH=${{ steps.set-commit-hash.outputs.COMMIT_HASH }}
A_BRANCH_NAME=${{ github.ref_name }}
deployPreview:
name: "Deploy to Preview"
needs: build
if: github.ref == 'refs/heads/main'
concurrency:
group: preview
cancel-in-progress: false
environment:
name: "preview"
url: "https://preview.gordonbeeming.com/"
runs-on: ubuntu-latest
permissions:
id-token: write
env:
API_URL: "https://preview.gordonbeeming.com"
steps:
- name: Azure Login
uses: azure/login@v2
with:
client-id: ${{ secrets.ARM_CLIENT_ID }}
tenant-id: ${{ secrets.ARM_TENANT_ID }}
subscription-id: ${{ secrets.ARM_SUBSCRIPTION_ID }}
- name: Set commit hash
id: set-commit-hash
shell: bash
run: |
export COMMIT_HASH=${GITHUB_SHA:0:7}
echo "COMMIT_HASH=$COMMIT_HASH" >> $GITHUB_OUTPUT
- name: Deploy Web App to Preview (editor)
uses: Azure/cli@v2
with:
inlineScript: |
TAG=$(echo 'https://${{ env.REGISTRY }}/${{ env.EDITOR_IMAGE_NAME }}:${{ github.sha }}' | tr '[:upper:]' '[:lower:]')
az webapp config container set --resource-group ${{ secrets.AZURE_WEBAPP_RG }} --name ${{ secrets.EDITOR_AZURE_WEBAPP_NAME }} --docker-custom-image-name $TAG
- name: Deploy Web App to Preview (site)
uses: Azure/cli@v2
with:
inlineScript: |
TAG=$(echo 'https://${{ env.REGISTRY }}/${{ env.SITE_IMAGE_NAME }}:${{ github.sha }}' | tr '[:upper:]' '[:lower:]')
az webapp config container set --resource-group ${{ secrets.AZURE_WEBAPP_RG }} --name ${{ secrets.SITE_AZURE_WEBAPP_NAME }} --docker-custom-image-name $TAG
- name: Check deployed version endpoint
shell: pwsh
run: |
$tries = 0
$maxTries = 24
$apiDeployed = $false
$returnedVersion = ""
$expectedCommitHash = $env:COMMIT_HASH
$baseUrl = $env:API_URL
$VersionApi = "$($baseUrl)/api/Deploy/Version"
Write-Host "Waiting for API to deploy at $VersionApi"
Write-Host "Expected Version: $expectedCommitHash"
while ($tries -lt $maxTries -and $apiDeployed -eq $false) {
try
{
$version = Invoke-WebRequest -Uri $VersionApi -Method POST -Body "" -ContentType "application/json"
$returnedVersion = $version.Content.Trim('"')
if ($version.StatusCode -eq 200 -and $returnedVersion -eq $expectedCommitHash) {
$apiDeployed = $true
} else {
Write-Host "Waiting for API to deploy... (try $($tries + 1) of $maxTries)"
Start-Sleep -Seconds 5
$tries++
}
}
catch
{
Write-Host "🔥 Error trying to get version... $($_.Exception)"
Write-Host "Waiting for API to deploy... (try $($tries + 1) of $maxTries)"
Start-Sleep -Seconds 5
$tries++
}
}
if ($apiDeployed -eq $false) {
Write-Host "🔥 API did not deploy after $($maxTries * 5) seconds"
Write-Host "🔥 Current Version: $($returnedVersion), Expected Version: $($expectedCommitHash)"
exit(1)
}
# Write-Host "Health Check at $($baseUrl)/api/HealthCheck"
# $response = Invoke-WebRequest -Uri $env:API_URL/api/HealthCheck -SslProtocol Tls12 -Method POST -Body "" -ContentType "application/json"
# if ($response.Content -eq "OK") {
# Write-Host "Health check succeeded"
# } else {
# Write-Error "Health check failed: $($response.StatusCode)"
# exit 1
# }
env:
COMMIT_HASH: ${{ steps.set-commit-hash.outputs.COMMIT_HASH }}
- name: Azure Logout
uses: azure/CLI@v2
with:
inlineScript: |
az logout
deployLive:
name: "Deploy to live"
needs: deployPreview
if: github.ref == 'refs/heads/main'
concurrency:
group: live
cancel-in-progress: false
environment:
name: "live"
url: "https://gordonbeeming.com/"
runs-on: ubuntu-latest
permissions:
id-token: write
env:
API_URL: "https://gordonbeeming.com"
steps:
- name: Azure Login
uses: azure/login@v2
with:
client-id: ${{ secrets.ARM_CLIENT_ID }}
tenant-id: ${{ secrets.ARM_TENANT_ID }}
subscription-id: ${{ secrets.ARM_SUBSCRIPTION_ID }}
- name: Set commit hash
id: set-commit-hash
shell: bash
run: |
export COMMIT_HASH=${GITHUB_SHA:0:7}
echo "COMMIT_HASH=$COMMIT_HASH" >> $GITHUB_OUTPUT
- name: Deploy Web App to Live (editor)
uses: Azure/cli@v2
with:
inlineScript: |
TAG=$(echo 'https://${{ env.REGISTRY }}/${{ env.EDITOR_IMAGE_NAME }}:${{ github.sha }}' | tr '[:upper:]' '[:lower:]')
az webapp config container set --resource-group ${{ secrets.AZURE_WEBAPP_RG }} --name ${{ secrets.EDITOR_AZURE_WEBAPP_NAME }} --docker-custom-image-name $TAG
- name: Deploy Web App to Live (site)
uses: Azure/cli@v2
with:
inlineScript: |
TAG=$(echo 'https://${{ env.REGISTRY }}/${{ env.SITE_IMAGE_NAME }}:${{ github.sha }}' | tr '[:upper:]' '[:lower:]')
az webapp config container set --resource-group ${{ secrets.AZURE_WEBAPP_RG }} --name ${{ secrets.SITE_AZURE_WEBAPP_NAME }} --docker-custom-image-name $TAG
- name: Check deployed version endpoint
shell: pwsh
run: |
$tries = 0
$maxTries = 24
$apiDeployed = $false
$returnedVersion = ""
$expectedCommitHash = $env:COMMIT_HASH
$baseUrl = $env:API_URL
$VersionApi = "$($baseUrl)/api/Deploy/Version"
Write-Host "Waiting for API to deploy at $VersionApi"
Write-Host "Expected Version: $expectedCommitHash"
while ($tries -lt $maxTries -and $apiDeployed -eq $false) {
try
{
$version = Invoke-WebRequest -Uri $VersionApi -Method POST -Body "" -ContentType "application/json"
$returnedVersion = $version.Content.Trim('"')
if ($version.StatusCode -eq 200 -and $returnedVersion -eq $expectedCommitHash) {
$apiDeployed = $true
} else {
Write-Host "Waiting for API to deploy... (try $($tries + 1) of $maxTries)"
Start-Sleep -Seconds 5
$tries++
}
}
catch
{
Write-Host "🔥 Error trying to get version... $($_.Exception)"
Write-Host "Waiting for API to deploy... (try $($tries + 1) of $maxTries)"
Start-Sleep -Seconds 5
$tries++
}
}
if ($apiDeployed -eq $false) {
Write-Host "🔥 API did not deploy after $($maxTries * 5) seconds"
Write-Host "🔥 Current Version: $($returnedVersion), Expected Version: $($expectedCommitHash)"
exit(1)
}
# Write-Host "Health Check at $($baseUrl)/api/HealthCheck"
# $response = Invoke-WebRequest -Uri $env:API_URL/api/HealthCheck -SslProtocol Tls12 -Method POST -Body "" -ContentType "application/json"
# if ($response.Content -eq "OK") {
# Write-Host "Health check succeeded"
# } else {
# Write-Error "Health check failed: $($response.StatusCode)"
# exit 1
# }
env:
COMMIT_HASH: ${{ steps.set-commit-hash.outputs.COMMIT_HASH }}
- name: Azure Logout
uses: azure/CLI@v2
with:
inlineScript: |
az logout