-
Notifications
You must be signed in to change notification settings - Fork 83
174 lines (140 loc) · 4.9 KB
/
docker-image.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
# This workflow builds multiplatform Nosey Parker Docker images for both the Debian and
# Alpine varieties.
#
# For each variety, it uses a native-hardware runner to build the individual
# Docker images, and then merges them into a multiplatform image.
#
# This approach is taken instead of using QEMU, since a QEMU build of Nosey
# Parker takes several hours. Using native runners instead takes a few minutes.
#
# This was adapted from the Docker documentation site:
# https://docs.docker.com/build/ci/github-actions/multi-platform/#distribute-build-across-multiple-runners
#
name: Docker
on:
push:
branches: [ "main" ]
# Run when release tags are created
tags: [ "v*.*.*" ]
# Do not run on pull requests; the Docker registry permissions are not set up
# to work except for with the canonical `noseyparker` repo. Running from pull
# requests from forks will fail.
#
# pull_request:
# branches: [ "main" ]
jobs:
# build single-platform docker images on native runners
build:
strategy:
matrix:
include:
- base: debian
platform: linux/amd64
platform-pair: linux-amd64
dockerfile: Dockerfile
image: ghcr.io/${{ github.repository }}
runs-on: ubuntu-22.04
- base: debian
platform: linux/arm64
platform-pair: linux-arm64
dockerfile: Dockerfile
image: ghcr.io/${{ github.repository }}
runs-on: ubuntu-22.04-arm64-8-core
- base: alpine
platform: linux/amd64
platform-pair: linux-amd64
dockerfile: Dockerfile.alpine
image: ghcr.io/${{ github.repository }}-alpine
runs-on: ubuntu-22.04
- base: alpine
platform: linux/arm64
platform-pair: linux-arm64
dockerfile: Dockerfile.alpine
image: ghcr.io/${{ github.repository }}-alpine
runs-on: ubuntu-22.04-arm64-8-core
name: Docker (${{ matrix.platform }} ${{ matrix.base }})
runs-on: ${{ matrix.runs-on }}
steps:
- uses: actions/checkout@v4
with:
persist-credentials: false
- uses: docker/setup-buildx-action@v3
- name: Authenticate with Docker registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Collect Docker metadata
id: metadata
uses: docker/metadata-action@v5
with:
images: ${{ matrix.image }}
- name: Build and push by digest
uses: docker/build-push-action@v6
id: build
with:
context: .
file: ${{ matrix.dockerfile }}
platforms: ${{ matrix.platform }}
# don't specify 'tags' here (error "get can't push tagged ref by digest")
labels: ${{ steps.metadata.outputs.labels }}
outputs: type=image,name=${{ matrix.image }},push-by-digest=true,name-canonical=true,push=true
- name: Inspect Docker image
run: |
docker buildx imagetools inspect "${{ matrix.image }}@${{ steps.build.outputs.digest }}"
- name: Test Docker image
run: docker run --rm "${{ matrix.image }}@${{ steps.build.outputs.digest }}" --version
- name: Export digest
run: |
mkdir -p /tmp/digests
digest="${{ steps.build.outputs.digest }}"
touch "/tmp/digests/${digest#sha256:}"
- name: Upload digest
uses: actions/upload-artifact@v4
with:
name: digests-${{ matrix.base }}-${{ matrix.platform-pair }}
path: /tmp/digests/*
if-no-files-found: error
retention-days: 1
# merge single-platform images into multiplatform images
merge:
strategy:
matrix:
include:
- base: debian
image: ghcr.io/${{ github.repository }}
- base: alpine
image: ghcr.io/${{ github.repository }}-alpine
runs-on: ubuntu-latest
needs:
- build
name: Docker (${{ matrix.base }})
steps:
- name: Download digests
uses: actions/download-artifact@v4
with:
path: /tmp/digests
pattern: digests-${{ matrix.base }}-*
merge-multiple: true
- uses: docker/setup-buildx-action@v3
- name: Collect Docker metadata
id: metadata
uses: docker/metadata-action@v5
with:
images: ${{ matrix.image }}
- name: Authenticate with Docker registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Create and push merged image
working-directory: /tmp/digests
run: |
docker buildx imagetools create \
$(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
$(printf '${{ matrix.image }}@sha256:%s ' *)
- name: Inspect Docker image
run: |
docker buildx imagetools inspect "${{ matrix.image }}:${{ steps.metadata.outputs.version }}"