Fastest multi-arch Docker builds on Github Actions

📅 2025-10-26

⌛ 399 days ago

📖 4 min read


In 2022 I hacked together a hybrid setup for @SuperSeriousBot. GitHub’s x86 runners did the amd64 build, my own arm64 box did the arm64 one over SSH, and buildx juggled both. Janky, but about 10x faster than emulating arm64.

GitHub now has its own arm64 runners, so I wanted to see if I could drop the self-hosted machine and still get fast multi-arch builds.

I can. The QEMU workflow took 10 minutes 20 seconds. The new one takes 2 minutes 45 seconds, and there’s no extra box for me to look after.

Screenshot comparing multi-arch build times: 10m20s with QEMU vs 2m45s with native arm64 runners

The old setup

For GOTM I was building linux/amd64 + linux/arm64 images with QEMU and a single Buildx call:

name: Release

on:
  push:
    branches:
      - "master"

jobs:
  publish:
    name: Build Docker Image
    runs-on: ubuntu-latest
    permissions:
      packages: write
      contents: read

    steps:
      - uses: actions/checkout@v4

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3

      - name: Set up Docker Buildx
        id: builder
        uses: docker/setup-buildx-action@v3

      - name: Login to GHCR
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and push
        uses: docker/build-push-action@v5
        with:
          context: .
          platforms: linux/amd64,linux/arm64
          file: ./Dockerfile
          push: true
          tags: |
            ghcr.io/obviyus/gotm-remix:${{ github.sha }}
            ghcr.io/obviyus/gotm-remix:latest
          cache-from: type=gha
          cache-to: type=gha,mode=max

It worked, but every arm64 layer ran under emulation. Even with the cache cranked up (cache-to hit 1.2 GB at one point), every release sat there for ten minutes while QEMU slowly worked through the Bun + Remix build.

arm64 runners

runs-on: ubuntu-24.04-arm gets you a real arm64 machine. No self-hosting, no SSH. So now each architecture can build on its own runner, in parallel.

The new workflow has two jobs: build each architecture natively, then stitch the manifests together.

name: Release

on:
  push:
    branches:
      - master

env:
  IMAGE: ghcr.io/obviyus/gotm-remix

jobs:
  build:
    strategy:
      fail-fast: false
      matrix:
        include:
          - platform: linux/amd64
            runner: ubuntu-latest
            artifact: linux-amd64
          - platform: linux/arm64
            runner: ubuntu-24.04-arm
            artifact: linux-arm64
    runs-on: ${{ matrix.runner }}
    permissions:
      contents: read
      packages: write
    steps:
      - uses: actions/checkout@v5
      - uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - uses: docker/setup-buildx-action@v3
      - name: Build & push by digest
        id: build
        uses: docker/build-push-action@v6
        with:
          context: .
          file: ./Dockerfile
          platforms: ${{ matrix.platform }}
          outputs: type=image,name=${{ env.IMAGE }},push-by-digest=true,name-canonical=true,push=true
          cache-from: type=gha,scope=${{ github.ref_name }}-gotm-remix
          cache-to: type=gha,mode=max,scope=${{ github.ref_name }}-gotm-remix
          provenance: mode=max
          sbom: true
      - name: Export digest
        run: |
          mkdir -p ${{ runner.temp }}/digests
          echo "${{ steps.build.outputs.digest }}" | sed 's/^sha256://' | xargs -I{} touch "${{ runner.temp }}/digests/{}"
      - uses: actions/upload-artifact@v4
        with:
          name: digests-${{ matrix.artifact }}
          path: ${{ runner.temp }}/digests/*
          retention-days: 1

  merge:
    needs: build
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
    steps:
      - uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - uses: docker/setup-buildx-action@v3
      - uses: actions/download-artifact@v4
        with:
          path: ${{ runner.temp }}/digests
          pattern: digests-*
          merge-multiple: true
      - name: Create and push manifest
        working-directory: ${{ runner.temp }}/digests
        run: |
          docker buildx imagetools create \
            -t $IMAGE:latest \
            -t $IMAGE:${{ github.sha }} \
            $(printf "$IMAGE@sha256:%s " *)
      - name: Inspect
        run: docker buildx imagetools inspect $IMAGE:latest

Why it’s faster

  • The arm64 build runs on actual ARM cores instead of being translated instruction by instruction by QEMU. This is most of the win.
  • Both architectures build at the same time on separate runners, and each one pushes its own image.
  • push-by-digest=true pushes each platform’s image without tagging it. The merge job then just points a manifest at the two digests with imagetools create, so it doesn’t rebuild or re-upload anything.
  • The cache is scoped to ${{ github.ref_name }}-gotm-remix, so branch builds don’t evict the master cache.

That’s about 3.7x faster than the QEMU workflow (for this project at least!), and no runner of my own to maintain.

Caveats

  • There are fewer arm64 runners than x86 ones. Queue times have been fine so far, but I’d expect some waiting around big releases or during US daytime.
  • docker/build-push-action@v6 turns on SBOMs and provenance, which older Dockerfiles might not expect.
  • The digest artifact dance looks weird, but it means the merge job only needs a registry login and two digest strings. It doesn’t need the builders or any local images.