-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathbuild-and-push-multiarch-image.yml
More file actions
186 lines (168 loc) · 6.46 KB
/
build-and-push-multiarch-image.yml
File metadata and controls
186 lines (168 loc) · 6.46 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
name: Build and push multi-arch image
on:
workflow_call:
inputs:
imageName:
required: false
type: string
default: ${{ github.event.repository.name }}
dockerfilePath:
required: false
type: string
default: Dockerfile
context:
required: false
type: string
default: .
buildArgs:
required: false
type: string
gitRef:
required: false
type: string
default: ${{ github.sha }}
arm64_runner_name:
required: false
type: string
default: ubuntu-24.04-arm
amd64_runner_name:
required: false
type: string
default: ubuntu-latest
secrets:
BUILD_SECRETS:
description: "Allows adding secrets to the docker build task - https://docs.docker.com/build/ci/github-actions/secrets"
required: false
outputs:
imageTag:
description: "The image tag for the built image"
value: ${{ jobs.build-and-push-image.outputs.imageTag }}
jobs:
build-and-push-image:
name: Build and push image for ${{ inputs.imageName }}
strategy:
matrix:
arch:
- amd64
- arm64
include:
- arch: amd64
runner: ${{ inputs.amd64_runner_name }}
- arch: arm64
runner: ${{ inputs.arm64_runner_name }}
runs-on: ${{ matrix.runner }}
outputs:
imageTag: ${{ steps.meta.outputs.version }}
localSha: ${{ steps.local-head.outputs.sha }}
permissions:
id-token: write
contents: read
packages: write
steps:
- uses: docker/setup-docker-action@b2189fbf2a6592b51fee7cdd93ee2bfaeba733db # v5.1.0
with:
version: "version=28.5.2"
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
ref: ${{ inputs.gitRef }}
show-progress: false
- name: Login to GitHub Container Registry
uses: docker/login-action@4907a6ddec9925e35a0a9e82d7399ccc52663121 # v4.1.0
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- uses: docker/setup-buildx-action@4d04d5d9486b7bd6fa91e7baf45bbb4f8b9deedd # v4.0.0
- run: echo "sha=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT"
id: local-head
- name: Generate Image Metadata
id: meta
uses: docker/metadata-action@030e881283bb7a6894de51c315a6bfe6a94e05cf # v6.0.0
with:
images: |
ghcr.io/${{ github.repository_owner }}/govuk/${{ inputs.imageName }}
labels: |
org.opencontainers.image.vendor=GDS
tags: |
type=raw,priority=500,value=${{ inputs.gitRef }},enable=${{ startsWith(inputs.gitRef, 'v') }}
type=raw,priority=400,value=${{ steps.local-head.outputs.sha }},enable=${{ !startsWith(inputs.gitRef, 'v') }}
- id: build-image
uses: docker/build-push-action@bcafcacb16a39f128d818304e6c9c0c18556b85f # v7.1.0
with:
file: ${{ inputs.dockerfilepath }}
context: ${{ inputs.context }}
platforms: "linux/${{ matrix.arch }}"
load: true
provenance: false
build-args: ${{ inputs.buildArgs }}
labels: ${{ steps.meta.outputs.labels }}
outputs: type=image,name=ghcr.io/${{ github.repository_owner }}/govuk/${{ inputs.imageName }},push-by-digest=true,name-canonical=true,push=true
secrets: ${{ secrets.BUILD_SECRETS }}
- id: export-digests
env:
DIGEST: "${{ steps.build-image.outputs.digest }}"
run: |
mkdir -p /tmp/digests
touch "/tmp/digests/${DIGEST#sha256:}"
- id: upload-digests
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: digests-${{ inputs.imageName }}-${{ matrix.arch }}
path: /tmp/digests/*
if-no-files-found: error
retention-days: 1
overwrite: true
combine_manifests:
name: Create Docker Manifests for Built Images
needs:
- build-and-push-image
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
packages: write
steps:
- uses: docker/setup-docker-action@b2189fbf2a6592b51fee7cdd93ee2bfaeba733db # v5.1.0
with:
version: "version=28.5.2"
- name: Download Digests
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
with:
path: /tmp/digests
pattern: digests-${{ inputs.imageName }}-*
merge-multiple: true
- uses: docker/setup-buildx-action@4d04d5d9486b7bd6fa91e7baf45bbb4f8b9deedd # v4.0.0
- run: echo "sha=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT"
id: local-head
- name: Login to GitHub Container Registry
uses: docker/login-action@4907a6ddec9925e35a0a9e82d7399ccc52663121 # v4.1.0
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Generate Image Metadata
id: meta
uses: docker/metadata-action@030e881283bb7a6894de51c315a6bfe6a94e05cf # v6.0.0
with:
images: |
ghcr.io/${{ github.repository_owner }}/govuk/${{ inputs.imageName }}
labels: |
org.opencontainers.image.vendor=GDS
tags: |
type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'main') }}
type=raw,priority=500,value=${{ inputs.gitRef }},enable=${{ startsWith(inputs.gitRef, 'v') }}
type=raw,priority=400,value=${{ needs.build-and-push-image.outputs.localSha }},enable=${{ !startsWith(inputs.gitRef, 'v') }}
- name: Create Manifest Lists
env:
IMAGEREF_PREFIX: 'ghcr.io/${{ github.repository_owner }}/govuk/${{ inputs.imageName }}'
working-directory: /tmp/digests
run: |
tag_args=$(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON")
printf -v sources "${IMAGEREF_PREFIX}@sha256:%s " *
# shellcheck disable=SC2086 # Intentional word-splitting on $tag_args and $sources.
docker buildx imagetools create $tag_args $sources
- name: Inspect Images
env:
IMAGEREF: 'ghcr.io/${{ github.repository_owner }}/govuk/${{ inputs.imageName }}:${{ steps.meta.outputs.version }}'
run: |
docker buildx imagetools inspect "$IMAGEREF"