Skip to content

Commit e3cf268

Browse files
authored
Use matrix build for multi-platform Docker build (#260)
1 parent e7242d5 commit e3cf268

File tree

1 file changed

+92
-28
lines changed

1 file changed

+92
-28
lines changed

.github/workflows/build-env-docker.yml

+92-28
Original file line numberDiff line numberDiff line change
@@ -3,67 +3,131 @@
33

44
name: Create and publish a Docker image for bindings build environment
55

6-
# Configures this workflow to run every time a change is pushed to the branch called `release`.
76
on:
87
push:
98
branches: ['main']
109

11-
# Defines two custom environment variables for the workflow. These are used for the Container registry domain, and a name for the Docker image that this workflow builds.
10+
concurrency:
11+
group: docker-build
12+
cancel-in-progress: true
13+
1214
env:
1315
REGISTRY: ghcr.io
14-
IMAGE_NAME: sourcegraph/scip-bindings-env
16+
IMAGE_NAME: ghcr.io/sourcegraph/scip-bindings-env
1517

16-
# There is a single job in this workflow. It's configured to run on the latest available version of Ubuntu.
1718
jobs:
18-
build-and-push-image:
19+
docker_release_build:
1920
runs-on: ubuntu-latest
2021
# Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job.
2122
permissions:
2223
contents: read
2324
packages: write
2425
attestations: write
2526
id-token: write
27+
strategy:
28+
fail-fast: false
29+
matrix:
30+
platform:
31+
- linux/amd64
32+
- linux/arm64
2633
steps:
27-
- name: Checkout repository
28-
uses: actions/checkout@v4
34+
- name: Checkout
35+
uses: actions/checkout@v3
36+
37+
- name: Docker meta
38+
id: meta
39+
uses: docker/metadata-action@v4
40+
with:
41+
images: ${{ env.IMAGE_NAME }}
42+
tags: |
43+
type=raw,value=latest
2944
3045
- name: Set up QEMU
31-
uses: docker/setup-qemu-action@v3
46+
uses: docker/setup-qemu-action@v2
47+
48+
- name: Set up Docker Buildx
49+
uses: docker/setup-buildx-action@v2
3250

33-
# Uses the `docker/login-action` action to log in to the Container registry registry using the account and password that will publish the packages. Once published, the packages are scoped to the account defined here.
3451
- name: Log in to the Container registry
3552
uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1
3653
with:
3754
registry: ${{ env.REGISTRY }}
3855
username: ${{ github.actor }}
3956
password: ${{ secrets.GITHUB_TOKEN }}
4057

41-
# This step uses [docker/metadata-action](https://github.com/docker/metadata-action#about) to extract tags and labels that will be applied to the specified image. The `id` "meta" allows the output of this step to be referenced in a subsequent step. The `images` value provides the base name for the tags and labels.
42-
- name: Extract metadata (tags, labels) for Docker
43-
id: meta
44-
uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
45-
with:
46-
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
47-
tags: |
48-
type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'main') }}
49-
50-
# This step uses the `docker/build-push-action` action to build the image, based on your repository's `Dockerfile`. If the build succeeds, it pushes the image to GitHub Packages.
51-
# It uses the `context` parameter to define the build's context as the set of files located in the specified path. For more information, see "[Usage](https://github.com/docker/build-push-action#usage)" in the README of the `docker/build-push-action` repository.
52-
# It uses the `tags` and `labels` parameters to tag and label the image with the output from the "meta" step.
53-
- name: Build and push Docker image
54-
id: push
58+
- name: Build and push by digest
59+
id: build
5560
uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4
5661
with:
5762
file: dev/Dockerfile.bindings
5863
push: true
59-
tags: ${{ steps.meta.outputs.tags }}
64+
platforms: ${{ matrix.platform }}
6065
labels: ${{ steps.meta.outputs.labels }}
61-
platforms: linux/amd64,linux/arm64
66+
outputs: type=image,name=${{ env.IMAGE_NAME }},push-by-digest=true,name-canonical=true,push=true
67+
cache-from: type=registry,ref=${{ env.IMAGE_NAME}}:latest
68+
cache-to: type=inline
69+
70+
- name: Export digest
71+
run: |
72+
mkdir -p /tmp/digests
73+
digest="${{ steps.build.outputs.digest }}"
74+
touch "/tmp/digests/${digest#sha256:}"
75+
76+
- name: Upload digest
77+
uses: actions/upload-artifact@v3
78+
with:
79+
name: digests
80+
path: /tmp/digests/*
81+
if-no-files-found: error
82+
retention-days: 1
6283

63-
# This step generates an artifact attestation for the image, which is an unforgeable statement about where and how it was built. It increases supply chain security for people who consume the image. For more information, see "[AUTOTITLE](/actions/security-guides/using-artifact-attestations-to-establish-provenance-for-builds)."
6484
- name: Generate artifact attestation
6585
uses: actions/attest-build-provenance@v1
6686
with:
67-
subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME}}
68-
subject-digest: ${{ steps.push.outputs.digest }}
87+
subject-name: ${{ env.IMAGE_NAME}}
88+
subject-digest: ${{ steps.build.outputs.digest }}
6989
push-to-registry: true
90+
91+
docker_release_merge:
92+
runs-on: ubuntu-latest
93+
needs: [docker_release_build]
94+
# Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job.
95+
permissions:
96+
contents: read
97+
packages: write
98+
attestations: write
99+
id-token: write
100+
steps:
101+
- name: Log in to the Container registry
102+
uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1
103+
with:
104+
registry: ${{ env.REGISTRY }}
105+
username: ${{ github.actor }}
106+
password: ${{ secrets.GITHUB_TOKEN }}
107+
108+
- name: Download digests
109+
uses: actions/download-artifact@v3
110+
with:
111+
name: digests
112+
path: /tmp/digests
113+
114+
- name: Set up Docker Buildx
115+
uses: docker/setup-buildx-action@v2
116+
117+
- name: Docker meta
118+
id: meta
119+
uses: docker/metadata-action@v4
120+
with:
121+
images: ${{ env.IMAGE_NAME }}
122+
tags: |
123+
type=raw,value=latest
124+
125+
- name: Create manifest list and push
126+
working-directory: /tmp/digests
127+
run: |
128+
docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
129+
$(printf '${{ env.IMAGE_NAME }}@sha256:%s ' *)
130+
131+
- name: Inspect image
132+
run: |
133+
docker buildx imagetools inspect ${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.version }}

0 commit comments

Comments
 (0)