-
Notifications
You must be signed in to change notification settings - Fork 0
170 lines (153 loc) · 5.83 KB
/
build.yml
File metadata and controls
170 lines (153 loc) · 5.83 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
name: Publish Docker image
on:
workflow_dispatch:
inputs:
push:
description: "Push to container registry"
required: false
default: false
type: boolean
push:
branches:
- "**"
tags:
- "*"
paths-ignore:
- "**.yml"
- "**.yaml"
- "**.md"
- "**.txt"
- ".gitattribute"
- ".gitignore"
pull_request:
branches:
- "**"
paths-ignore:
- "**.yml"
- "**.yaml"
- "**.md"
- "**.txt"
- ".gitattribute"
- ".gitignore"
permissions:
contents: read
packages: write
env:
REGISTRY: ghcr.io
REPO: ${{ github.repository_owner }}/github_runner
jobs:
set_vars:
name: Set variables
runs-on: [self-hosted, Linux]
outputs:
is_workflow_dispatch: ${{ steps.set_outputs.outputs.is_workflow_dispatch }}
steps:
- name: Set outputs
id: set_outputs
run: |
is_workflow_dispatch=${{ github.event_name == 'workflow_dispatch' }}
echo "is_workflow_dispatch=$is_workflow_dispatch" >> $GITHUB_OUTPUT
push_to_registry:
name: Build and push image to Container Registry
needs: set_vars
strategy:
fail-fast: false
matrix:
year: ["2019", "2022"]
runs-on: windows-${{ matrix.year }}
# do not run for pushes of tags
if: ${{ success() && (!(github.event_name == 'push' && 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
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ env.REGISTRY }}/${{ env.REPO }}
flavor: |
latest=false
tags: |
type=sha,prefix=,suffix=-windowsservercore-ltsc${{ matrix.year }}
- 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) || needs.set_vars.outputs.is_workflow_dispatch == 'true') }}
- name: Docker login
id: docker_login
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
if: ${{ success() && ((github.event_name == 'push' && github.ref == 'refs/heads/main') || (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') || (inputs.push && needs.set_vars.outputs.is_workflow_dispatch == 'true')) }}
- name: Docker logout
run: docker logout ${{ env.REGISTRY }}
if: ${{ always() && steps.docker_login.outcome != 'skipped' }}
push_manifest:
name: Push Docker manifest to Container Registry
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_type == 'tag'))) }}
env:
DOCKER_CLI_EXPERIMENTAL: enabled
steps:
- name: Check out the repo
uses: actions/checkout@v2
- name: Docker login
id: docker_login
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ env.REGISTRY }}/${{ env.REPO }}
flavor: |
latest=false
tags: |
type=ref,event=branch
type=ref,event=tag
- name: Push manifest
run: |
$ErrorActionPreference = "Stop"
$sha = $(git rev-parse --short HEAD)
$repo = "${{ env.REGISTRY }}/${{ env.REPO }}".ToLower()
$branch = "${{ github.ref_name }}".ToLower()
# Create and push manifest for branch
docker manifest create "${repo}:${branch}" `
--amend "${repo}:${sha}-windowsservercore-ltsc2019" `
--amend "${repo}:${sha}-windowsservercore-ltsc2022"
docker manifest push "${repo}:${branch}"
# Create and push manifest for SHA
docker manifest create "${repo}:${sha}" `
--amend "${repo}:${sha}-windowsservercore-ltsc2019" `
--amend "${repo}:${sha}-windowsservercore-ltsc2022"
docker manifest push "${repo}:${sha}"
- name: Docker logout
run: docker logout ${{ env.REGISTRY }}
if: ${{ always() && steps.docker_login.outcome != 'skipped' }}