From ec8f4aaa2001ae8b82bfe2234bfa0797e709a1a4 Mon Sep 17 00:00:00 2001 From: Collin Pendleton Date: Tue, 21 Jul 2026 10:51:48 -0500 Subject: [PATCH] ci: build each arch on native runners instead of QEMU Split the single multi-platform buildx job into a matrix that builds linux/amd64 on ubuntu-latest and linux/arm64 on ubuntu-24.04-arm, then merges the two into one multi-arch manifest. The old job emulated arm64 via QEMU, so `npm install --omit=dev` had to compile better-sqlite3 from source on Alpine/musl under emulation. That took 30+ minutes on a cache miss and threw "qemu: uncaught target signal 4 (Illegal instruction)" during node-gyp, which risks publishing an arm64 image with a broken native binary. - per-arch GHA cache scopes so the two runners don't clobber each other - images pushed by digest, then stitched with `buildx imagetools create` so all existing tags (branch, pr, semver, latest) are unchanged - pull requests still build both arches for validation but push nothing, so the merge job is skipped --- .github/workflows/docker-publish.yml | 104 +++++++++++++++++++++++---- 1 file changed, 92 insertions(+), 12 deletions(-) diff --git a/.github/workflows/docker-publish.yml b/.github/workflows/docker-publish.yml index 2dd0c0c..4616883 100644 --- a/.github/workflows/docker-publish.yml +++ b/.github/workflows/docker-publish.yml @@ -15,19 +15,42 @@ env: IMAGE_NAME: ${{ github.repository }} jobs: - build-and-push: - runs-on: ubuntu-latest + # Build each architecture on its own native runner. No QEMU emulation, so + # better-sqlite3 compiles natively instead of taking ~30min under qemu (and + # risking a broken arm64 binary from an emulated node-gyp crash). + build: + name: Build ${{ matrix.platform }} + runs-on: ${{ matrix.runner }} + strategy: + fail-fast: false + matrix: + include: + - platform: linux/amd64 + runner: ubuntu-latest + - platform: linux/arm64 + runner: ubuntu-24.04-arm permissions: contents: read packages: write steps: + - name: Prepare platform name + run: echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV + env: + platform: ${{ matrix.platform }} + - name: Checkout repository uses: actions/checkout@v4 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 + - name: Extract metadata (labels) for Docker + id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + - name: Log in to GitHub Container Registry if: github.event_name != 'pull_request' uses: docker/login-action@v3 @@ -36,7 +59,57 @@ jobs: username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - - name: Extract metadata (tags, labels) for Docker + # On pull requests this only validates the build: push=false, no digest is + # exported, and the merge job below is skipped. + - name: Build and push by digest + id: build + uses: docker/build-push-action@v6 + with: + context: . + platforms: ${{ matrix.platform }} + labels: ${{ steps.meta.outputs.labels }} + cache-from: type=gha,scope=${{ env.PLATFORM_PAIR }} + cache-to: type=gha,mode=max,scope=${{ env.PLATFORM_PAIR }} + outputs: type=image,name=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }},push-by-digest=true,name-canonical=true,push=${{ github.event_name != 'pull_request' }} + + - name: Export digest + if: github.event_name != 'pull_request' + run: | + mkdir -p /tmp/digests + digest="${{ steps.build.outputs.digest }}" + touch "/tmp/digests/${digest#sha256:}" + + - name: Upload digest + if: github.event_name != 'pull_request' + uses: actions/upload-artifact@v4 + with: + name: digests-${{ env.PLATFORM_PAIR }} + path: /tmp/digests/* + if-no-files-found: error + retention-days: 1 + + # Stitch the per-arch images into a single multi-arch manifest under the real tags. + merge: + name: Merge manifests + if: github.event_name != 'pull_request' + runs-on: ubuntu-latest + needs: [build] + permissions: + contents: read + packages: write + + steps: + - name: Download digests + uses: actions/download-artifact@v4 + with: + path: /tmp/digests + pattern: digests-* + merge-multiple: true + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Extract metadata (tags) for Docker id: meta uses: docker/metadata-action@v5 with: @@ -49,13 +122,20 @@ jobs: type=semver,pattern={{major}} type=raw,value=latest,enable={{is_default_branch}} - - name: Build and push Docker image - uses: docker/build-push-action@v5 + - name: Log in to GitHub Container Registry + uses: docker/login-action@v3 with: - context: . - push: ${{ github.event_name != 'pull_request' }} - tags: ${{ steps.meta.outputs.tags }} - labels: ${{ steps.meta.outputs.labels }} - cache-from: type=gha - cache-to: type=gha,mode=max - platforms: linux/amd64,linux/arm64 + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Create manifest list and push + working-directory: /tmp/digests + run: | + docker buildx imagetools create \ + $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \ + $(printf '${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@sha256:%s ' *) + + - name: Inspect image + run: | + docker buildx imagetools inspect ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.version }}