Skip to content

Commit cfe650a

Browse files
committed
github/workflows: Use buildx to build images for multiple platforms
docker buildx is able to create multi-platform docker images in a single invocation (using emulation where required), but multi-platform images cannot be stored locally, only pushed directly to a regsitry. So refacor the workflow a little. Set common configuration as environment variables. Add actions to initialize QEMU and buildx. The remaining steps are modified to: * First build just the amd64 image and use this for validation using docker-compose (--load only supports single-platform images) * Build, tag and push the "latest" images for all platforms in a single invocation * For tags, override IMAGE_TAG and run the same command again. Built layers are cached so no redundant work is performed, despite running the same command multiple times. Signed-off-by: Peter Hoyes <[email protected]>
1 parent 21ce832 commit cfe650a

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

.github/workflows/docker.yml

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ on:
66
tags:
77
- '*'
88

9+
env:
10+
QEMU_PLATFORMS: arm64
11+
IMAGE_PLATFORMS: linux/amd64,linux/arm64
12+
IMAGE_PREFIX: ${{ secrets.DOCKERHUB_PREFIX }}
13+
914
jobs:
1015
docker:
1116
runs-on: ubuntu-latest
@@ -16,32 +21,27 @@ jobs:
1621
sudo apt install -yq python3-pip
1722
pip install --upgrade setuptools
1823
pip install setuptools_scm
24+
- name: Set up QEMU
25+
uses: docker/setup-qemu-action@v3
26+
with:
27+
platforms: ${QEMU_PLATFORMS}
28+
- name: Set up Docker Buildx
29+
uses: docker/setup-buildx-action@v3
1930
- name: Login to DockerHub
2031
uses: docker/login-action@v3
2132
with:
2233
username: ${{ secrets.DOCKERHUB_USERNAME }}
2334
password: ${{ secrets.DOCKERHUB_TOKEN }}
24-
- name: Build docker image
35+
- name: Build amd64 docker image and validate
2536
run: |
26-
./dockerfiles/build.sh
37+
./dockerfiles/build.sh --load
2738
docker-compose -f dockerfiles/staging/docker-compose.yml up --exit-code-from client client
2839
docker-compose -f dockerfiles/staging/docker-compose.yml down
2940
docker images
30-
- name: Tag latest images
31-
run: |
32-
docker tag labgrid-client ${{ secrets.DOCKERHUB_PREFIX }}client
33-
docker tag labgrid-exporter ${{ secrets.DOCKERHUB_PREFIX }}exporter
34-
docker tag labgrid-coordinator ${{ secrets.DOCKERHUB_PREFIX }}coordinator
35-
- name: Tag release image
41+
- name: Build, tag and push latest image for all platforms
42+
run: ./dockerfiles/build.sh --platform ${IMAGE_PLATFORMS} --push
43+
- name: Tag and push release image for all platforms
3644
if: startsWith(github.ref, 'refs/tags')
37-
run: |
38-
docker tag labgrid-client ${{ secrets.DOCKERHUB_PREFIX }}client:${GITHUB_REF_NAME}
39-
docker tag labgrid-exporter ${{ secrets.DOCKERHUB_PREFIX }}exporter:${GITHUB_REF_NAME}
40-
docker tag labgrid-coordinator ${{ secrets.DOCKERHUB_PREFIX }}coordinator:${GITHUB_REF_NAME}
41-
- name: Push to dockerhub
42-
run: |
43-
docker push --all-tags ${{ secrets.DOCKERHUB_PREFIX }}client
44-
docker push --all-tags ${{ secrets.DOCKERHUB_PREFIX }}exporter
45-
docker push --all-tags ${{ secrets.DOCKERHUB_PREFIX }}coordinator
46-
- name: Show images again
47-
run: docker images
45+
env:
46+
IMAGE_TAG: ${{ github.ref_name }}
47+
run: ./dockerfiles/build.sh --platform ${IMAGE_PLATFORMS} --push

0 commit comments

Comments
 (0)