Skip to content

Commit f57a3e4

Browse files
Attempt #1 on merging multi-platform docker builds
1 parent 1c756d2 commit f57a3e4

File tree

1 file changed

+78
-21
lines changed

1 file changed

+78
-21
lines changed

.github/workflows/ghcr-publish.yml

Lines changed: 78 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,15 @@ on:
99
# schedule:
1010
# - cron: '18 10 * * *'
1111
push:
12-
branches: [ "main" ]
12+
branches: ["main"]
1313
# Publish semver tags as releases.
14-
tags: [ 'v*.*.*' ]
14+
tags: ["v*.*.*"]
1515
pull_request:
16-
branches: [ "main" ]
16+
branches: ["main"]
1717

1818
env:
1919
# Use docker.io for Docker Hub if empty
20-
REGISTRY: ghcr.io
21-
IMAGE_NAME: ${{ github.repository }}
22-
20+
REGISTRY_IMAGE: ghcr.io/sourcebot
2321

2422
jobs:
2523
build:
@@ -32,53 +30,73 @@ jobs:
3230
id-token: write
3331
strategy:
3432
matrix:
35-
architecture: [amd64, arm64]
33+
platform:
34+
- linux/amd64
35+
- linux/arm64
3636

3737
steps:
38+
- name: Prepare
39+
run: |
40+
platform=${{ matrix.platform }}
41+
echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV
42+
3843
- name: Checkout repository
3944
uses: actions/checkout@v4
4045
with:
41-
submodules: 'true'
42-
46+
submodules: "true"
47+
4348
# Extract metadata (tags, labels) for Docker
4449
# https://github.com/docker/metadata-action
4550
- name: Extract Docker metadata
4651
id: meta
4752
uses: docker/metadata-action@v5
4853
with:
49-
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
54+
images: ${{ env.REGISTRY_IMAGE }}
5055

5156
# Install the cosign tool except on PR
5257
# https://github.com/sigstore/cosign-installer
5358
- name: Install cosign
5459
if: github.event_name != 'pull_request'
5560
uses: sigstore/[email protected]
5661
with:
57-
cosign-release: 'v2.2.4'
62+
cosign-release: "v2.2.4"
5863

5964
- name: Set up Docker Buildx
60-
uses: docker/setup-buildx-action@v1
61-
65+
uses: docker/setup-buildx-action@v3
66+
6267
- name: Login to GitHub Packages Docker Registry
6368
uses: docker/login-action@v1
6469
if: github.event_name != 'pull_request'
6570
with:
66-
registry: ${{ env.REGISTRY }}
71+
registry: ghcr.io
6772
username: ${{ github.actor }}
6873
password: ${{ secrets.GITHUB_TOKEN }}
6974

7075
- name: Build and push Docker image
71-
id: build-and-push
72-
uses: docker/build-push-action@v5
76+
id: build
77+
uses: docker/build-push-action@v6
7378
with:
7479
context: .
75-
push: ${{ github.event_name != 'pull_request' }}
7680
tags: ${{ steps.meta.outputs.tags }}
7781
labels: ${{ steps.meta.outputs.labels }}
7882
cache-from: type=gha
7983
cache-to: type=gha,mode=max
80-
platforms: linux/${{ matrix.architecture }}
81-
84+
platforms: ${{ matrix.platform }}
85+
86+
- name: Export digest
87+
run: |
88+
mkdir -p /tmp/digests
89+
digest="${{ steps.build.outputs.digest }}"
90+
touch "/tmp/digests/${digest#sha256:}"
91+
92+
- name: Upload digest
93+
uses: actions/upload-artifact@v4
94+
with:
95+
name: digests-${{ env.PLATFORM_PAIR }}
96+
path: /tmp/digests/*
97+
if-no-files-found: error
98+
retention-days: 1
99+
82100
# Sign the resulting Docker image digest except on PRs.
83101
# This will only write to the public Rekor transparency log when the Docker
84102
# repository is public to avoid leaking data. If you would like to publish
@@ -89,7 +107,46 @@ jobs:
89107
env:
90108
# https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions#using-an-intermediate-environment-variable
91109
TAGS: ${{ steps.meta.outputs.tags }}
92-
DIGEST: ${{ steps.build-and-push.outputs.digest }}
110+
DIGEST: ${{ steps.build.outputs.digest }}
93111
# This step uses the identity token to provision an ephemeral certificate
94112
# against the sigstore community Fulcio instance.
95-
run: echo "${TAGS}" | xargs -I {} cosign sign --yes {}@${DIGEST}
113+
run: echo "${TAGS}" | xargs -I {} cosign sign --yes {}@${DIGEST}
114+
115+
merge:
116+
runs-on: ubuntu-latest
117+
if: github.event_name != 'pull_request'
118+
needs:
119+
- build
120+
steps:
121+
- name: Download digests
122+
uses: actions/download-artifact@v4
123+
with:
124+
path: /tmp/digests
125+
pattern: digests-*
126+
merge-multiple: true
127+
128+
- name: Set up Docker Buildx
129+
uses: docker/setup-buildx-action@v3
130+
131+
- name: Extract Docker metadata
132+
id: meta
133+
uses: docker/metadata-action@v5
134+
with:
135+
images: ${{ env.REGISTRY_IMAGE }}
136+
137+
- name: Login to GitHub Packages Docker Registry
138+
uses: docker/login-action@v1
139+
with:
140+
registry: ghcr.io
141+
username: ${{ github.actor }}
142+
password: ${{ secrets.GITHUB_TOKEN }}
143+
144+
- name: Create manifest list and push
145+
working-directory: /tmp/digests
146+
run: |
147+
docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
148+
$(printf '${{ env.REGISTRY_IMAGE }}@sha256:%s ' *)
149+
150+
- name: Inspect image
151+
run: |
152+
docker buildx imagetools inspect ${{ env.REGISTRY_IMAGE }}:${{ steps.meta.outputs.version }}

0 commit comments

Comments
 (0)