forked from Lambda3/docker-github-runner-windows
-
Notifications
You must be signed in to change notification settings - Fork 0
343 lines (310 loc) · 14.5 KB
/
build.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json
name: Publish Docker image
on:
workflow_dispatch:
inputs:
push:
description: "Push to docker hub"
required: false
default: false
type: boolean
simulate_schedule:
description: "Simulate schedule"
required: false
default: false
type: boolean
schedule:
- cron: "5 3 * * *"
push:
branches:
- "**"
tags:
- "*"
paths-ignore:
- "**.md"
- "**.txt"
- ".gitattribute"
- ".gitignore"
pull_request:
branches:
- "**"
paths-ignore:
- "**.md"
- "**.txt"
- ".gitattribute"
- ".gitignore"
env:
REPO: lambda3/github-runner-windows
jobs:
set_vars:
name: Set variables
runs-on: ubuntu-latest
outputs:
is_schedule: ${{ steps.set_outputs.outputs.is_schedule }}
is_workflow_dispatch: ${{ steps.set_outputs.outputs.is_workflow_dispatch }}
steps:
- name: Set outputs
id: set_outputs
run: |
is_schedule=${{ github.event_name == 'schedule' || !!inputs.simulate_schedule }}
is_workflow_dispatch=${{ github.event_name == 'workflow_dispatch' && !inputs.simulate_schedule }}
echo "::set-output name=is_schedule::$is_schedule"
echo "::set-output name=is_workflow_dispatch::$is_workflow_dispatch"
push_to_registry:
name: Build and push image to Docker Hub
needs: set_vars
strategy:
fail-fast: false
matrix:
year: ["2019", "2022"]
runs-on: windows-${{ matrix.year }}
# do not run for pushes of tags and latest branch
if: ${{ success() && (!(github.event_name == 'push' && (github.ref == 'refs/heads/latest' || startsWith(github.ref, 'refs/tags/')))) }}
outputs:
push_2019: ${{ steps.push_image.outputs[format('push_{0}', matrix.year)] == 'true' }}
push_2022: ${{ steps.push_image.outputs[format('push_{0}', matrix.year)] == 'true' }}
steps:
- name: Check out the repo
uses: actions/checkout@v2
if: ${{ needs.set_vars.outputs.is_schedule != 'true' }}
- name: Check out the latest branch
uses: actions/checkout@v2
with:
ref: latest
if: ${{ needs.set_vars.outputs.is_schedule == 'true' }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ env.REPO }}
flavor: |
latest=false
tags: |
type=sha,prefix=,suffix=-windowsservercore-ltsc${{ matrix.year }}
- name: Check if update available
id: check
uses: giggio/docker-image-update-checker@v2
with:
base-image: mcr.microsoft.com/windows/servercore:ltsc${{ matrix.year }}
image: ${{ steps.meta.outputs.tags }}
os: windows
verbose: true
if: ${{ needs.set_vars.outputs.is_schedule == 'true' }}
- name: Build image
working-directory: ./runner/
run: |
$ErrorActionPreference = "Stop"
Invoke-Expression "docker build --isolation process --build-arg LTSC_YEAR=${{ matrix.year }} --pull $($env:TAGS.Split("`n") | % { '-t "' + $_ + '"' }) $($env:LABELS.Split("`n") | % { '--label "' + $_ + '"' }) ."
if ($LASTEXITCODE -ne 0) { throw "Docker build failed." }
env:
TAGS: ${{ steps.meta.outputs.tags }}
LABELS: ${{ steps.meta.outputs.labels }}
if: ${{ success() && (contains(fromJson('["push", "pull_request"]'), github.event_name) || (steps.check.outputs.needs-updating == 'true' && needs.set_vars.outputs.is_schedule == 'true') || needs.set_vars.outputs.is_workflow_dispatch == 'true') }}
- name: Docker login
id: docker_login
run: |
$ErrorActionPreference = "Stop"
docker login -u lambdatres -p $env:DOCKER_PASSWORD
if ($LASTEXITCODE -ne 0) { throw "Docker login failed." }
env:
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
if: ${{ success() && ((github.event_name == 'push' && github.ref == 'refs/heads/main') || (steps.check.outputs.needs-updating == 'true' && needs.set_vars.outputs.is_schedule == 'true') || (inputs.push && needs.set_vars.outputs.is_workflow_dispatch == 'true')) }}
- name: Push image
id: push_image
run: |
$ErrorActionPreference = "Stop"
$($env:TAGS.Split("`n") | % {
docker push $_
if ($LASTEXITCODE -ne 0) { throw "Docker push failed." }
})
Write-Output "::set-output name=push_${{ matrix.year }}::true"
env:
TAGS: ${{ steps.meta.outputs.tags }}
if: ${{ success() && ((github.event_name == 'push' && github.ref == 'refs/heads/main') || (steps.check.outputs.needs-updating == 'true' && needs.set_vars.outputs.is_schedule == 'true') || (inputs.push && needs.set_vars.outputs.is_workflow_dispatch == 'true')) }}
- name: Docker logout
run: docker logout
if: ${{ always() && steps.docker_login.outcome != 'skipped' }}
# scheduled: an update to year tag because of base image update, will have ran 'push_to_registry' job:
push_manifest_scheduled_with_year:
name: Push Docker manifest to Docker Hub (with year, scheduled and with base image update)
runs-on: windows-latest
needs: ["push_to_registry", "set_vars"]
strategy:
fail-fast: false
matrix:
year: ["2019", "2022"]
if: ${{ success() && needs.set_vars.outputs.is_schedule == 'true' }} # can't check matrix on job.if, so we check on step.if bellow
env:
DOCKER_CLI_EXPERIMENTAL: enabled
steps:
- name: Check if should continue
id: should
run: |
$shouldContinue = "${{ needs.push_to_registry.outputs[format('push_{0}', matrix.year)] == 'true' }}"
Write-Output "::set-output name=continue::$shouldContinue"
- name: Check out the latest branch
uses: actions/checkout@v2
with:
ref: latest
if: ${{ success() && steps.should.outputs.continue == 'true' }}
- name: Docker login
id: docker_login
run: |
$ErrorActionPreference = "Stop"
docker login -u zaklaus -p $env:DOCKER_PASSWORD
if ($LASTEXITCODE -ne 0) { throw "Docker login failed." }
env:
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
if: ${{ success() && steps.should.outputs.continue == 'true' }}
# example of manifests created:
# lambda3/github-runner-windows:windowsservercore-ltsc2019
# lambda3/github-runner-windows:windowsservercore-ltsc2022
- name: Push manifest (${{ matrix.year }})
run: |
$sha = $(git rev-parse --short HEAD)
docker manifest create ${env:REPO}:windowsservercore-ltsc${{ matrix.year }} --amend ${env:REPO}:$sha-windowsservercore-ltsc${{ matrix.year }}
docker manifest push ${env:REPO}:windowsservercore-ltsc${{ matrix.year }}
if: ${{ success() && steps.should.outputs.continue == 'true' }}
- name: Docker logout
run: docker logout
if: ${{ success() && steps.should.outputs.continue == 'true' }}
# not scheduled, with year, pushes to main and tags, might run 'push_to_registry' job (push to main) or not (push to latest and tags):
push_manifest_branches_and_tags_with_year:
name: Push Docker manifest to Docker Hub (with year, via branch and tag updates)
runs-on: windows-latest
needs: ["push_to_registry", "set_vars"]
strategy:
fail-fast: false
matrix:
year: ["2019", "2022"]
if: ${{ always() && needs.set_vars.outputs.is_schedule != 'true' }} # can't check matrix on job.if, so we check on step.if bellow
env:
DOCKER_CLI_EXPERIMENTAL: enabled
steps:
- name: Check if should continue
id: should
run: |
Write-Output "needs.push_to_registry.result: ${{ needs.push_to_registry.result }}"
Write-Output "matrix.year: ${{ matrix.year }}"
Write-Output "format('push_{0}', matrix.year): ${{ format('push_{0}', matrix.year) }}"
Write-Output "needs.push_to_registry.outputs[format('push_{0}', matrix.year)]: ${{ needs.push_to_registry.outputs[format('push_{0}', matrix.year)] }}"
Write-Output "needs.push_to_registry.result: ${{ needs.push_to_registry.result }}"
Write-Output "github.event_name: ${{ github.event_name }}"
Write-Output "github.ref: ${{ github.ref }}"
Write-Output "github.ref_type: ${{ github.ref_type }}"
$shouldContinue = "${{ (needs.push_to_registry.result == 'success' && needs.push_to_registry.outputs[format('push_{0}', matrix.year)] == 'true') || (needs.push_to_registry.result == 'skipped' && (github.event_name == 'push' && (github.ref == 'refs/heads/latest' || github.ref_type == 'tag'))) }}"
Write-Output "::set-output name=continue::$shouldContinue"
- name: Check out the repo
uses: actions/checkout@v2
if: ${{ success() && steps.should.outputs.continue == 'true' }}
- name: Docker login
id: docker_login
run: |
$ErrorActionPreference = "Stop"
docker login -u zaklaus -p $env:DOCKER_PASSWORD
if ($LASTEXITCODE -ne 0) { throw "Docker login failed." }
env:
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
if: ${{ success() && steps.should.outputs.continue == 'true' }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ env.REPO }}
flavor: |
latest=false
tags: |
type=ref,event=branch
type=ref,event=tag
if: ${{ success() && steps.should.outputs.continue == 'true' }}
# example of manifests created:
# lambda3/github-runner-windows:main-windowsservercore-ltsc2019 (or 2022)
# lambda3/github-runner-windows:latest-windowsservercore-ltsc2019 (or 2022)
# lambda3/github-runner-windows:1.2.3-windowsservercore-ltsc2019 (or 2022)
- name: Push manifest (${{ matrix.year }})
run: |
$sha = $(git rev-parse --short HEAD)
docker manifest create ${{ steps.meta.outputs.tags }}-windowsservercore-ltsc${{ matrix.year }} --amend ${env:REPO}:$sha-windowsservercore-ltsc${{ matrix.year }}
docker manifest push ${{ steps.meta.outputs.tags }}-windowsservercore-ltsc${{ matrix.year }}
if: ${{ success() && steps.should.outputs.continue == 'true' }}
- name: Docker logout
run: docker logout
if: ${{ always() && steps.docker_login.outcome != 'skipped' }}
# scheduled: an update to latest tag because of base image update, will have ran 'push_to_registry' job:
push_manifest_scheduled_latest:
name: Push Docker manifest to Docker Hub (without year, latest tag, scheduled and with base image update)
runs-on: windows-latest
needs: ["push_to_registry", "set_vars"]
if: ${{ success() && needs.set_vars.outputs.is_schedule == 'true' && (needs.push_to_registry.outputs.push_2019 == 'true' || needs.push_to_registry.outputs.push_2022 == 'true') }}
env:
DOCKER_CLI_EXPERIMENTAL: enabled
steps:
- name: Check out the latest branch
uses: actions/checkout@v2
with:
ref: latest
- name: Docker login
id: docker_login
run: |
$ErrorActionPreference = "Stop"
docker login -u zaklaus -p $env:DOCKER_PASSWORD
if ($LASTEXITCODE -ne 0) { throw "Docker login failed." }
env:
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
# example of manifests created:
# lambda3/github-runner-windows:latest
# lambda3/github-runner-windows:95aeca1 (sha)
- name: Push manifest
run: |
$sha = $(git rev-parse --short HEAD)
docker manifest create ${env:REPO}:latest --amend ${env:REPO}:$sha-windowsservercore-ltsc2019 --amend ${env:REPO}:$sha-windowsservercore-ltsc2022
docker manifest push ${env:REPO}:latest
docker manifest create ${env:REPO}:$sha --amend ${env:REPO}:$sha-windowsservercore-ltsc2019 --amend ${env:REPO}:$sha-windowsservercore-ltsc2022
docker manifest push ${env:REPO}:$sha
- name: Docker logout
run: docker logout
if: ${{ always() && steps.docker_login.outcome != 'skipped' }}
# not scheduled, without year, pushes to main and tags, might run 'push_to_registry' job (push to main) or not (push to latest and tags):
push_manifest_branches_and_tags_without_year:
name: Push Docker manifest to Docker Hub (without year, via branch and tag updates)
runs-on: windows-latest
needs: ["push_to_registry", "set_vars"]
if: ${{ always() && needs.set_vars.outputs.is_schedule != 'true' && ((needs.push_to_registry.result == 'success' && (needs.push_to_registry.outputs.push_2019 == 'true' || needs.push_to_registry.outputs.push_2022 == 'true')) || (needs.push_to_registry.result == 'skipped' && (github.event_name == 'push' && (github.ref == 'refs/heads/latest' || github.ref_type == 'tag')))) }}
env:
DOCKER_CLI_EXPERIMENTAL: enabled
steps:
- name: Check out the repo
uses: actions/checkout@v2
- name: Docker login
id: docker_login
run: |
$ErrorActionPreference = "Stop"
docker login -u zaklaus -p $env:DOCKER_PASSWORD
if ($LASTEXITCODE -ne 0) { throw "Docker login failed." }
env:
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ env.REPO }}
flavor: |
latest=false
tags: |
type=ref,event=branch
type=ref,event=tag
# example of manifests created:
# lambda3/github-runner-windows:main
# lambda3/github-runner-windows:latest
# lambda3/github-runner-windows:1.2.3
# lambda3/github-runner-windows:95aeca1 (sha)
- name: Push manifest
run: |
$sha = $(git rev-parse --short HEAD)
docker manifest create ${{ steps.meta.outputs.tags }} --amend ${env:REPO}:$sha-windowsservercore-ltsc2019 --amend ${env:REPO}:$sha-windowsservercore-ltsc2022
docker manifest push ${{ steps.meta.outputs.tags }}
docker manifest create ${env:REPO}:$sha --amend ${env:REPO}:$sha-windowsservercore-ltsc2019 --amend ${env:REPO}:$sha-windowsservercore-ltsc2022
docker manifest push ${env:REPO}:$sha
- name: Docker logout
run: docker logout
if: ${{ always() && steps.docker_login.outcome != 'skipped' }}