Skip to content

Commit 2a77659

Browse files
committed
Adding Docker Bake to ease multi-stage/multi-platform builds
1 parent 7a94a42 commit 2a77659

File tree

2 files changed

+113
-36
lines changed

2 files changed

+113
-36
lines changed

.github/workflows/docker-build.yml

+77-36
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,70 @@ name: Docker Image
33

44
on:
55
push:
6-
tags: [ '*.*.*' ]
6+
# tags: [ '*.*.*' ]
77

88
env:
99
REGISTRY_IMAGE: ghcr.io/rgrizzell/circuitpython
1010

1111
jobs:
12+
prepare:
13+
runs-on: ubuntu-latest
14+
outputs:
15+
matrix: ${{ steps.platforms.outputs.matrix }}
16+
steps:
17+
- name: Checkout
18+
uses: actions/checkout@v4
19+
20+
- name: Create matrix
21+
id: platforms
22+
run: |
23+
echo "matrix=$(docker buildx bake runtime --print | jq -cr '.target."runtime".platforms')" >>${GITHUB_OUTPUT}
24+
25+
- name: Show matrix
26+
run: |
27+
echo ${{ steps.platforms.outputs.matrix }}
28+
29+
- name: Docker meta
30+
id: meta
31+
uses: docker/metadata-action@v5
32+
with:
33+
images: ${{ env.REGISTRY_IMAGE }}
34+
35+
- name: Rename meta bake definition file
36+
run: |
37+
mv "${{ steps.meta.outputs.bake-file }}" "/tmp/bake-meta.json"
38+
mv "${{ steps.meta.outputs.bake-file-annotations }}" "/tmp/bake-annotations.json"
39+
40+
- name: Upload meta bake definition
41+
uses: actions/upload-artifact@v4
42+
with:
43+
name: bake-meta
44+
path: /tmp/bake-*.json
45+
if-no-files-found: error
46+
retention-days: 1
47+
1248
build:
1349
runs-on: ubuntu-latest
50+
needs:
51+
- prepare
1452
strategy:
1553
fail-fast: false
1654
matrix:
17-
platform:
18-
- linux/amd64
19-
- linux/arm64
55+
platform: ${{ fromJson(needs.prepare.outputs.matrix) }}
2056
steps:
2157
- name: Prepare
2258
run: |
2359
platform=${{ matrix.platform }}
24-
echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV
60+
echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV
2561
2662
- name: Checkout
2763
uses: actions/checkout@v4
2864

29-
- name: Docker meta
30-
id: meta
31-
uses: docker/metadata-action@v5
65+
- name: Download meta bake definition
66+
uses: actions/download-artifact@v4
3267
with:
33-
images: ${{ env.REGISTRY_IMAGE }}
34-
35-
- name: Set up QEMU
36-
uses: docker/setup-qemu-action@v3
37-
38-
- name: Set up Docker Buildx
39-
uses: docker/setup-buildx-action@v3
68+
name: bake-meta
69+
path: /tmp
4070

4171
- name: Login to GitHub Container Registry
4272
uses: docker/login-action@v3
@@ -45,20 +75,31 @@ jobs:
4575
username: ${{ github.repository_owner }}
4676
password: ${{ secrets.GITHUB_TOKEN }}
4777

48-
- name: Build and push by digest
49-
id: build
50-
uses: docker/build-push-action@v5
78+
- name: Set up QEMU
79+
uses: docker/setup-qemu-action@v3
80+
81+
- name: Set up Docker Buildx
82+
uses: docker/setup-buildx-action@v3
83+
84+
- name: Build
85+
id: bake
86+
uses: docker/bake-action@v5
5187
with:
52-
context: .
53-
platforms: ${{ matrix.platform }}
54-
labels: ${{ steps.meta.outputs.labels }}
55-
outputs: type=image,name=${{ env.REGISTRY_IMAGE }},push-by-digest=true,name-canonical=true,push=true,annotation-index.org.opencontainers.image.description=CircuitPython for Linux,annotation-index.org.opencontainers.image.license=MIT
88+
files: |
89+
./docker-bake.hcl
90+
/tmp/bake-meta.json
91+
/tmp/bake-annotations.json
92+
targets: image
93+
set: |
94+
*.tags=
95+
*.platform=${{ matrix.platform }}
96+
*.output=type=image,"name=${{ env.REGISTRY_IMAGE }}",push-by-digest=true,name-canonical=true,push=true
5697
5798
- name: Export digest
5899
run: |
59100
mkdir -p /tmp/digests
60-
digest="${{ steps.build.outputs.digest }}"
61-
touch "/tmp/digests/${digest#sha256:}"
101+
digest="${{ fromJSON(steps.bake.outputs.metadata).image['containerimage.digest'] }}"
102+
touch "/tmp/digests/${digest#sha256:}"
62103
63104
- name: Upload digest
64105
uses: actions/upload-artifact@v4
@@ -73,35 +114,35 @@ jobs:
73114
needs:
74115
- build
75116
steps:
117+
- name: Download meta bake definition
118+
uses: actions/download-artifact@v4
119+
with:
120+
name: bake-meta
121+
path: /tmp
122+
76123
- name: Download digests
77124
uses: actions/download-artifact@v4
78125
with:
79126
path: /tmp/digests
80127
pattern: digests-*
81128
merge-multiple: true
82129

83-
- name: Set up Docker Buildx
84-
uses: docker/setup-buildx-action@v3
85-
86-
- name: Docker meta
87-
id: meta
88-
uses: docker/metadata-action@v5
89-
with:
90-
images: ${{ env.REGISTRY_IMAGE }}
91-
92130
- name: Login to GitHub Container Registry
93131
uses: docker/login-action@v3
94132
with:
95133
registry: ghcr.io
96134
username: ${{ github.repository_owner }}
97135
password: ${{ secrets.GITHUB_TOKEN }}
98136

137+
- name: Set up Docker Buildx
138+
uses: docker/setup-buildx-action@v3
139+
99140
- name: Create manifest list and push
100141
working-directory: /tmp/digests
101142
run: |
102-
docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
103-
$(printf '${{ env.REGISTRY_IMAGE }}@sha256:%s ' *)
143+
docker buildx imagetools create $(jq -cr '.target."docker-metadata-action".tags | map(select(startswith("${{ env.REGISTRY_IMAGE }}")) | "-t " + .) | join(" ")' /tmp/bake-meta.json) \
144+
$(printf '${{ env.REGISTRY_IMAGE }}@sha256:%s ' *)
104145
105146
- name: Inspect image
106147
run: |
107-
docker buildx imagetools inspect ${{ env.REGISTRY_IMAGE }}:${{ steps.meta.outputs.version }}
148+
docker buildx imagetools inspect ${{ env.REGISTRY_IMAGE }}:$(jq -r '.target."docker-metadata-action".args.DOCKER_META_VERSION' /tmp/bake-meta.json)

docker-bake.hcl

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
variable "DEFAULT_TAG" {
2+
default = "rgrizzell/circuitpython:9.2.1"
3+
}
4+
5+
// Special target: https://github.com/docker/metadata-action#bake-definition
6+
target "docker-metadata-action" {
7+
tags = ["${DEFAULT_TAG}"]
8+
}
9+
10+
// Default target if none specified
11+
group "default" {
12+
targets = ["local"]
13+
}
14+
15+
target "image" {
16+
inherits = ["docker-metadata-action"]
17+
}
18+
19+
target "local" {
20+
inherits = ["image"]
21+
output = ["type=docker"]
22+
}
23+
24+
target "base" {
25+
inherits = ["image"]
26+
target = "base"
27+
platforms = [
28+
"linux/amd64",
29+
"linux/arm64"
30+
]
31+
}
32+
33+
target "runtime" {
34+
inherits = ["base"]
35+
target = "runtime"
36+
}

0 commit comments

Comments
 (0)