-
Notifications
You must be signed in to change notification settings - Fork 0
277 lines (255 loc) · 12.4 KB
/
build-multiarch.yaml
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
name: Build and push multi-arch images
on:
workflow_dispatch:
inputs:
gitRef:
description: Commit, tag or branch name to deploy
required: true
type: string
default: main
pushToRegistry:
description: Push to image registry. Set to false to build without pushing.
required: true
type: boolean
default: true
push:
branches:
- main
schedule:
- cron: '34 3 * * *'
env:
REGISTRY_BASE: ghcr.io/alphagov
jobs:
configure_builds:
name: Read configuration from build-matrix.json
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
matrix_versions: ${{ steps.set-matrix.outputs.matrix_versions }}
runs_on: ${{ steps.set-matrix.outputs.runs_on }}
steps:
- uses: actions/checkout@v4
with:
show-progress: false
- id: set-matrix
run: |
{
echo "matrix=$(jq -c . < build-matrix.json)"
echo "matrix_versions=$(jq -c .version < build-matrix.json)"
echo "runs_on=$(jq -c .runs_on < build-matrix.json)"
} >> "$GITHUB_OUTPUT"
build_and_push_image:
name: Build ruby_${{ join(matrix.version.rubyver, '.') }} for ${{ matrix.runs_on.arch }} and push to GHCR
needs: configure_builds
strategy:
matrix: ${{ fromJson(needs.configure_builds.outputs.matrix) }}
runs-on: ${{ matrix.runs_on.runner_type }}
permissions:
packages: write
steps:
- name: Login to GHCR
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- uses: actions/checkout@v4
with:
ref: ${{ inputs.gitRef || github.ref }}
show-progress: false
- uses: docker/setup-buildx-action@v3
- name: Calculate Image Tags
id: calculate-image-tags
run: |
CREATED_DATE="$(date -u +'%Y-%m-%dT%H:%M:%SZ')"
echo "createdDate=${CREATED_DATE}" >> "$GITHUB_OUTPUT"
- name: Generate Base Image Metadata
uses: docker/metadata-action@v5
id: base-image-metadata
with:
flavor: |
latest=false
images: |
${{ env.REGISTRY_BASE }}/govuk-ruby-base
tags: |
type=raw,value=${{ matrix.version.rubyver[0] }}.${{ matrix.version.rubyver[1] }}
type=raw,value=${{ join(matrix.version.rubyver, '.') }}
type=raw,value=latest,enable=${{ matrix.version.extra == 'latest' }}
type=sha,enable=true,prefix=${{ matrix.version.rubyver[0] }}.${{ matrix.version.rubyver[1] }}-,format=short
type=sha,enable=true,prefix=${{ join(matrix.version.rubyver, '.') }}-,format=short
type=sha,enable=true,priority=100,format=long,prefix=${{ matrix.version.rubyver[0] }}.${{ matrix.version.rubyver[1] }}-
type=sha,enable=true,priority=200,format=long,prefix=${{ join(matrix.version.rubyver, '.') }}-
labels: |
org.opencontainers.image.title=govuk-ruby-base
org.opencontainers.image.authors="GOV.UK Platform Engineering"
org.opencontainers.image.description="Base image for GOV.UK Ruby apps"
org.opencontainers.image.source="https://github.com/alphagov/govuk-ruby-images"
org.opencontainers.image.version=${{ join(matrix.version.rubyver, '.') }}
org.opencontainers.image.created=${{ steps.calculate-image-tags.outputs.createdDate }}
org.opencontainers.image.vendor=GDS
- name: Generate Builder Image Metadata
uses: docker/metadata-action@v5
id: builder-image-metadata
with:
flavor: |
latest=false
images: |
${{ env.REGISTRY_BASE }}/govuk-ruby-builder
tags: |
type=raw,value=${{ matrix.version.rubyver[0] }}.${{ matrix.version.rubyver[1] }}
type=raw,value=${{ join(matrix.version.rubyver, '.') }}
type=raw,value=latest,enable=${{ matrix.version.extra == 'latest' }}
type=sha,enable=true,prefix=${{ matrix.version.rubyver[0] }}.${{ matrix.version.rubyver[1] }}-,format=short
type=sha,enable=true,prefix=${{ join(matrix.version.rubyver, '.') }}-,format=short
type=sha,enable=true,priority=100,format=long,prefix=${{ matrix.version.rubyver[0] }}.${{ matrix.version.rubyver[1] }}-
type=sha,enable=true,priority=200,format=long,prefix=${{ join(matrix.version.rubyver, '.') }}-
labels: |
org.opencontainers.image.title=govuk-ruby-builder
org.opencontainers.image.authors="GOV.UK Platform Engineering"
org.opencontainers.image.description="Builder Image for GOV.UK Ruby-based Apps"
org.opencontainers.image.source="https://github.com/alphagov/govuk-ruby-images"
org.opencontainers.image.version=${{ join(matrix.version.rubyver, '.') }}
org.opencontainers.image.created=${{ steps.calculate-image-tags.outputs.createdDate }}
org.opencontainers.image.vendor=GDS
- id: build-base-image
uses: docker/build-push-action@v5
with:
file: base.Dockerfile
context: .
platforms: "linux/${{ matrix.runs_on.arch }}"
load: true
provenance: false
build-args: |
RUBY_MAJOR=${{ matrix.version.rubyver[0] }}.${{ matrix.version.rubyver[1] }}
RUBY_VERSION=${{ join(matrix.version.rubyver, '.') }}
RUBY_CHECKSUM=${{ matrix.version.checksum }}
labels: ${{ steps.base-image-metadata.outputs.labels }}
outputs: type=image,name=${{ env.REGISTRY_BASE }}/govuk-ruby-base,push-by-digest=true,name-canonical=true,push=true
cache-from: type=gha,scope=build-base-${{ matrix.version.rubyver }}-${{ matrix.runs_on.arch }}
cache-to: type=gha,scope=build-base-${{ matrix.version.rubyver }}-${{ matrix.runs_on.arch }},mode=max
- id: build-builder-image
uses: docker/build-push-action@v5
with:
file: builder.Dockerfile
context: .
push: false
platforms: "linux/${{ matrix.runs_on.arch }}"
load: true
provenance: false
build-args: |
BASE_IMAGE_DIGEST=${{steps.build-base-image.outputs.digest }}
OWNER=${{ github.repository_owner }}
labels: ${{ steps.builder-image-metadata.outputs.labels }}
outputs: type=image,name=${{ env.REGISTRY_BASE }}/govuk-ruby-builder,push-by-digest=true,name-canonical=true,push=true
cache-from: type=gha,scope=build-builder-${{ matrix.version.rubyver }}-${{ matrix.runs_on.arch }}
cache-to: type=gha,scope=build-builder-${{ matrix.version.rubyver }}-${{ matrix.runs_on.arch }},mode=max
- id: export-digests
run: |
mkdir -p /tmp/digests/base /tmp/digests/builder
baseDigest="${{steps.build-base-image.outputs.digest }}"
builderDigest="${{steps.build-builder-image.outputs.digest }}"
touch "/tmp/digests/base/${baseDigest#sha256:}"
touch "/tmp/digests/builder/${builderDigest#sha256:}"
- id: upload-digests
uses: actions/upload-artifact@v4
with:
name: digests-${{ join(matrix.version.rubyver, '.') }}-${{ matrix.runs_on.arch }}
path: /tmp/digests/*
if-no-files-found: error
retention-days: 1
overwrite: true
combine_manifests:
if: ${{ inputs.pushToRegistry || true }}
name: Combine Docker Manifests for Ruby ${{ join(matrix.version.rubyver, '.') }} Images
needs:
- configure_builds
- build_and_push_image
runs-on: ubuntu-latest
strategy:
matrix:
version: ${{ fromJson(needs.configure_builds.outputs.matrix_versions) }}
permissions:
packages: write
steps:
- name: Download Digests
uses: actions/download-artifact@v4
with:
path: /tmp/digests
pattern: digests-${{ join(matrix.version.rubyver, '.') }}-*
merge-multiple: true
- uses: docker/setup-buildx-action@v3
- name: Calculate Image Tags
id: calculate-image-tags
run: |
CREATED_DATE="$(date -u +'%Y-%m-%dT%H:%M:%SZ')"
echo "createdDate=${CREATED_DATE}" >> "$GITHUB_OUTPUT"
- name: Login to GHCR
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Generate Base Image Metadata
uses: docker/metadata-action@v5
id: base-image-metadata
with:
flavor: |
latest=false
images: |
${{ env.REGISTRY_BASE }}/govuk-ruby-base
labels: |
org.opencontainers.image.title=govuk-ruby-base
org.opencontainers.image.authors="GOV.UK Platform Engineering"
org.opencontainers.image.description="Base image for GOV.UK Ruby apps"
org.opencontainers.image.source="https://github.com/alphagov/govuk-ruby-images"
org.opencontainers.image.version=${{ join(matrix.version.rubyver, '.') }}
org.opencontainers.image.created=${{ steps.calculate-image-tags.outputs.createdDate }}
org.opencontainers.image.vendor=GDS
tags: |
type=raw,value=${{ matrix.version.rubyver[0] }}.${{ matrix.version.rubyver[1] }}
type=raw,value=${{ join(matrix.version.rubyver, '.') }}
type=raw,value=latest,enable=${{ matrix.version.extra == 'latest' }}
type=sha,enable=true,prefix=${{ matrix.version.rubyver[0] }}.${{ matrix.version.rubyver[1] }}-,format=short
type=sha,enable=true,prefix=${{ join(matrix.version.rubyver, '.') }}-,format=short
type=sha,enable=true,priority=100,format=long,prefix=${{ matrix.version.rubyver[0] }}.${{ matrix.version.rubyver[1] }}-
type=sha,enable=true,priority=200,format=long,prefix=${{ join(matrix.version.rubyver, '.') }}-
- name: Create Manifest Lists (for Base)
working-directory: /tmp/digests/base
run: |
# shellcheck disable=SC2046
docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
$(printf '${{ env.REGISTRY_BASE }}/govuk-ruby-base@sha256:%s ' *)
- name: Generate Builder Image Metadata
uses: docker/metadata-action@v5
id: builder-image-metadata
with:
flavor: |
latest=false
images: |
${{ env.REGISTRY_BASE }}/govuk-ruby-builder
labels: |
org.opencontainers.image.title=govuk-ruby-builder
org.opencontainers.image.authors="GOV.UK Platform Engineering"
org.opencontainers.image.description="Builder Image for GOV.UK Ruby-based Apps"
org.opencontainers.image.source="https://github.com/alphagov/govuk-ruby-images"
org.opencontainers.image.version=${{ join(matrix.version.rubyver, '.') }}
org.opencontainers.image.created=${{ steps.calculate-image-tags.outputs.createdDate }}
org.opencontainers.image.vendor=GDS
tags: |
type=raw,value=${{ matrix.version.rubyver[0] }}.${{ matrix.version.rubyver[1] }}
type=raw,value=${{ join(matrix.version.rubyver, '.') }}
type=raw,value=latest,enable=${{ matrix.version.extra == 'latest' }}
type=sha,enable=true,prefix=${{ matrix.version.rubyver[0] }}.${{ matrix.version.rubyver[1] }}-,format=short
type=sha,enable=true,prefix=${{ join(matrix.version.rubyver, '.') }}-,format=short
type=sha,enable=true,priority=100,format=long,prefix=${{ matrix.version.rubyver[0] }}.${{ matrix.version.rubyver[1] }}-
type=sha,enable=true,priority=200,format=long,prefix=${{ join(matrix.version.rubyver, '.') }}-
- name: Create Manifest Lists (for Builder)
working-directory: /tmp/digests/builder
run: |
# shellcheck disable=SC2046
docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
$(printf '${{ env.REGISTRY_BASE }}/govuk-ruby-builder@sha256:%s ' *)
- name: Inspect Images
run: |
docker buildx imagetools inspect ${{ env.REGISTRY_BASE }}/govuk-ruby-base:${{ steps.base-image-metadata.outputs.version }}
docker buildx imagetools inspect ${{ env.REGISTRY_BASE }}/govuk-ruby-builder:${{ steps.builder-image-metadata.outputs.version }}