Skip to content

Commit d09780c

Browse files
committed
fix: multi-arch builds
1 parent 1be9ed3 commit d09780c

1 file changed

Lines changed: 94 additions & 9 deletions

File tree

.github/workflows/build.yaml

Lines changed: 94 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ on:
77
- main
88
pull_request:
99

10+
env:
11+
REGISTRY: ghcr.io
12+
IMAGE_NAME: query-doctor/sync
13+
1014
jobs:
1115
build:
1216
name: Build container
13-
env:
14-
REGISTRY: ghcr.io
15-
IMAGE_NAME: query-doctor/sync
1617
permissions:
1718
id-token: write
1819
contents: read
@@ -37,6 +38,11 @@ jobs:
3738
uses: docker/metadata-action@v5
3839
with:
3940
images: ${{ env.IMAGE_NAME }}
41+
tags: |
42+
type=ref,event=branch
43+
type=semver,pattern={{version}}
44+
type=semver,pattern={{major}}.{{minor}}
45+
4046
- uses: actions/checkout@v3
4147
- name: Login to ghcr
4248
uses: docker/login-action@v3
@@ -45,7 +51,15 @@ jobs:
4551
username: "${{ github.actor }}"
4652
password: "${{ secrets.GITHUB_TOKEN }}"
4753
- name: Setup Docker Buildx
54+
id: buildx
4855
uses: docker/setup-buildx-action@v3
56+
- name: Cache Docker layers
57+
uses: actions/cache@v2
58+
with:
59+
path: /tmp/.buildx-cache
60+
key: ${{ runner.os }}-buildx-${{ github.sha }}
61+
restore-keys: |
62+
${{ runner.os }}-buildx-
4963
- name: Set sync_version from deno.json
5064
run: |
5165
platform=${{ matrix.platform }}
@@ -57,18 +71,89 @@ jobs:
5771
with:
5872
context: .
5973
file: Dockerfile
74+
builder: ${{ steps.buildx.outputs.name }}
6075
outputs: type=docker
76+
labels: ${{ steps.meta.outputs.labels }}
6177
platforms: ${{ matrix.platform }}
62-
push: ${{ github.event_name == 'push' }}
78+
tags: ${{ steps.meta.outputs.tags }}
79+
cache-from: type=local,src=/tmp/.buildx-cache
80+
cache-to: type=local,mode=max,dest=/tmp/.buildx-cache-new
81+
# This ugly bit is necessary if you don't want your cache to grow forever
82+
# until it hits GitHub's limit of 5GB.
83+
# Temp fix
84+
# https://github.com/docker/build-push-action/issues/252
85+
# https://github.com/moby/buildkit/issues/1896
86+
- name: Move cache
87+
run: |
88+
rm -rf /tmp/.buildx-cache
89+
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
90+
- name: Export digest
91+
if: ${{ github.event_name == 'push' }}
92+
run: |
93+
mkdir -p ${{ runner.temp }}/digests
94+
digest="${{ steps.build.outputs.digest }}"
95+
touch "${{ runner.temp }}/digests/${digest#sha256:}"
96+
- name: Upload digest
97+
uses: actions/upload-artifact@v4
98+
if: ${{ github.event_name == 'push' }}
99+
with:
100+
name: digests-${{ env.PLATFORM_PAIR }}
101+
path: ${{ runner.temp }}/digests/*
102+
if-no-files-found: error
103+
retention-days: 1
104+
- name: Save digest to outputs
105+
run: |
106+
echo "digest=${{ steps.build.outputs.digest }}" >> $GITHUB_OUTPUT
107+
108+
merge:
109+
runs-on: ubuntu-24.04
110+
if: ${{ github.event_name == 'push' }}
111+
permissions:
112+
contents: read
113+
needs:
114+
- build
115+
steps:
116+
- name: Download digests
117+
uses: actions/download-artifact@v4
118+
with:
119+
path: ${{ runner.temp }}/digests
120+
pattern: digests-*
121+
merge-multiple: true
122+
123+
- name: Login to ghcr
124+
uses: docker/login-action@v3
125+
with:
126+
registry: ${{ env.REGISTRY }}
127+
username: "${{ github.actor }}"
128+
password: "${{ secrets.GITHUB_TOKEN }}"
129+
130+
- name: Set up Docker Buildx
131+
uses: docker/setup-buildx-action@v3
132+
133+
- name: Docker meta
134+
id: meta
135+
uses: docker/metadata-action@v5
136+
with:
137+
images: ${{ env.IMAGE_NAME }}
63138
tags: |
64-
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.sync_version }}
65-
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
139+
type=ref,event=branch
140+
type=ref,event=pr
141+
type=semver,pattern={{version}}
142+
type=semver,pattern={{major}}.{{minor}}
143+
66144
- name: Attest
67145
uses: actions/attest-build-provenance@v2
68-
if: ${{ github.event_name == 'push' }}
69146
with:
70147
subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
71-
subject-digest: ${{ steps.build.outputs.digest }}
148+
subject-digest: ${{ needs.build.outputs.digest }}
72149
push-to-registry: true
73150

74-
151+
- name: Create manifest list and push
152+
working-directory: ${{ runner.temp }}/digests
153+
run: |
154+
docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
155+
$(printf '${{ env.IMAGE_NAME }}@sha256:%s ' *)
156+
157+
- name: Inspect image
158+
run: |
159+
docker buildx imagetools inspect ${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.version }}

0 commit comments

Comments
 (0)