Skip to content

Commit 96bf0f7

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

File tree

2 files changed

+114
-36
lines changed

2 files changed

+114
-36
lines changed

.github/workflows/docker-build.yml

+78-36
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,72 @@ 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+
annotations: |
35+
org.opencontainers.image.description=CircuitPython for Unix/Linux
36+
org.opencontainers.image.license=MIT
37+
38+
- name: Rename meta bake definition file
39+
run: |
40+
mv "${{ steps.meta.outputs.bake-file }}" "/tmp/bake-meta.json"
41+
42+
- name: Upload meta bake definition
43+
uses: actions/upload-artifact@v4
44+
with:
45+
name: bake-meta
46+
path: /tmp/bake-meta.json
47+
if-no-files-found: error
48+
retention-days: 1
49+
1250
build:
1351
runs-on: ubuntu-latest
52+
needs:
53+
- prepare
1454
strategy:
1555
fail-fast: false
1656
matrix:
17-
platform:
18-
- linux/amd64
19-
- linux/arm64
57+
platform: ${{ fromJson(needs.prepare.outputs.matrix) }}
2058
steps:
2159
- name: Prepare
2260
run: |
2361
platform=${{ matrix.platform }}
24-
echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV
62+
echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV
2563
2664
- name: Checkout
2765
uses: actions/checkout@v4
2866

29-
- name: Docker meta
30-
id: meta
31-
uses: docker/metadata-action@v5
67+
- name: Download meta bake definition
68+
uses: actions/download-artifact@v4
3269
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
70+
name: bake-meta
71+
path: /tmp
4072

4173
- name: Login to GitHub Container Registry
4274
uses: docker/login-action@v3
@@ -45,20 +77,30 @@ jobs:
4577
username: ${{ github.repository_owner }}
4678
password: ${{ secrets.GITHUB_TOKEN }}
4779

48-
- name: Build and push by digest
49-
id: build
50-
uses: docker/build-push-action@v5
80+
- name: Set up QEMU
81+
uses: docker/setup-qemu-action@v3
82+
83+
- name: Set up Docker Buildx
84+
uses: docker/setup-buildx-action@v3
85+
86+
- name: Build
87+
id: bake
88+
uses: docker/bake-action@v5
5189
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
90+
files: |
91+
./docker-bake.hcl
92+
/tmp/bake-meta.json
93+
targets: image
94+
set: |
95+
*.tags=
96+
*.platform=${{ matrix.platform }}
97+
*.output=type=image,"name=${{ env.REGISTRY_IMAGE }}",push-by-digest=true,name-canonical=true,push=true
5698
5799
- name: Export digest
58100
run: |
59101
mkdir -p /tmp/digests
60-
digest="${{ steps.build.outputs.digest }}"
61-
touch "/tmp/digests/${digest#sha256:}"
102+
digest="${{ fromJSON(steps.bake.outputs.metadata).image['containerimage.digest'] }}"
103+
touch "/tmp/digests/${digest#sha256:}"
62104
63105
- name: Upload digest
64106
uses: actions/upload-artifact@v4
@@ -73,35 +115,35 @@ jobs:
73115
needs:
74116
- build
75117
steps:
118+
- name: Download meta bake definition
119+
uses: actions/download-artifact@v4
120+
with:
121+
name: bake-meta
122+
path: /tmp
123+
76124
- name: Download digests
77125
uses: actions/download-artifact@v4
78126
with:
79127
path: /tmp/digests
80128
pattern: digests-*
81129
merge-multiple: true
82130

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-
92131
- name: Login to GitHub Container Registry
93132
uses: docker/login-action@v3
94133
with:
95134
registry: ghcr.io
96135
username: ${{ github.repository_owner }}
97136
password: ${{ secrets.GITHUB_TOKEN }}
98137

138+
- name: Set up Docker Buildx
139+
uses: docker/setup-buildx-action@v3
140+
99141
- name: Create manifest list and push
100142
working-directory: /tmp/digests
101143
run: |
102-
docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
103-
$(printf '${{ env.REGISTRY_IMAGE }}@sha256:%s ' *)
144+
docker buildx imagetools create $(jq -cr '.target."docker-metadata-action".tags | map(select(startswith("${{ env.REGISTRY_IMAGE }}")) | "-t " + .) | join(" ")' /tmp/bake-meta.json) \
145+
$(printf '${{ env.REGISTRY_IMAGE }}@sha256:%s ' *)
104146
105147
- name: Inspect image
106148
run: |
107-
docker buildx imagetools inspect ${{ env.REGISTRY_IMAGE }}:${{ steps.meta.outputs.version }}
149+
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)