- GitHub Actions optimizations
- Cross-compile multi-arch Docker images in GitHub Actions
Cross-compile multi-arch Docker images in GitHub Actions
A multi-platform Docker build compiles each foreign architecture at native speed when the compile stage runs on the build platform (`FROM --platform=$BUILDPLATFORM`) and a cross-compiler targets each platform. The image still ships every platform; only the machine the compiler runs on changes.
ci.build.emulated-image-compilationstatic · checkable from the repoAI agents open the PR
StarSling agents run this exact audit on your workflows, apply the fix, and open a reviewable PR automatically.
Do this
When a Buildx step builds linux/arm64 on an x64 runner, every RUN in a stage based on the target platform executes through QEMU, so the whole Go, Rust or C compile runs at emulation speed. Setting GOARCH inside such a stage still runs the compiler itself under emulation. Move the compile stage to $BUILDPLATFORM and pass the target to the compiler.
# Same workflow, same platforms. The build stage runs on the runner's own
# CPU and Go cross-compiles for each target platform.
FROM --platform=$BUILDPLATFORM golang:1.25-alpine AS build
ARG TARGETOS TARGETARCH TARGETVARIANT
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH GOARM=${TARGETVARIANT#v} \
go build -o /out/app ./cmd/app
# The runtime stage keeps the target platform: it only copies the binary.
FROM alpine:3.22
COPY --from=build /out/app /usr/local/bin/app
ENTRYPOINT ["app"]Avoid this
# Built by docker/build-push-action with platforms: linux/amd64,linux/arm64
# on an ubuntu-latest runner. This stage runs on the TARGET platform, so the
# arm64 leg runs the whole Go compile under QEMU, GOARCH included.
FROM golang:1.25-alpine AS build
ARG TARGETARCH
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOARCH=$TARGETARCH go build -o /out/app ./cmd/app
FROM alpine:3.22
COPY --from=build /out/app /usr/local/bin/app
ENTRYPOINT ["app"]How to detect it
Find multi-platform builds:
grep -rnE 'platforms:|--platform' .github/workflows/and note each job whose platform list includes an architecture other than its runner's own, for examplelinux/arm64onubuntu-latest. A platform the runner builds natively needs no change.Open the Dockerfile each of those jobs builds. For every stage the final image copies from, check whether it runs a compile command (
go build,cargo build,make,cmake,npm rebuild,pip wheel) and whether itsFROMline, or the stage it is based on, carries--platform=$BUILDPLATFORM. A compile stage without it runs under emulation for every foreign platform.Size it from a recent run log: compare the Buildx
RUNstep times for the foreign platform with the same step for the native one. The change pays off when the foreign-architecture compile is at least half of the image build's layer time.Confirm the toolchain can target the platform from the build machine: Go with
CGO_ENABLED=0always can; cgo, Rust-syscrates and native Node add-ons need a target C toolchain such astonistiigi/xxorgcc-<arch>-linux-gnu. When none exists, build that platform on a native runner instead.
Tradeoffs and safety
Cross-compiling still runs one native compile per extra architecture, so the image build does more work than a single-platform build. Size the saving as the emulated seconds minus the native seconds for each foreign platform, and compare runner minutes as well as wall time.
cgo, Rust
-syscrates and native Node add-ons need a target C toolchain and target libraries.tonistiigi/xxorgcc-<arch>-linux-gnucovers most of them. When a toolchain has no cross target for a platform, build that platform on a native runner and merge the manifests, as in Build each architecture on native runners.A different C compiler or linker can change the binary. Gogs installs the target's GNU binutils so clang links with the same linker its emulated gcc build used; compare binary size and run the image after switching.
Keep every platform in the build, and run each image on its own architecture after merge or on a schedule: that run is the first place the cross-built binary executes.
Measured on open-source projects
Measured on StarSling-run forks replaying each project's upstream CI: five runs with the change and five without, all on StarSling Runners.
Gogs · 47,988 upstream stars on 2026-11-23. Image build step: 583 s → 94 s from a cold start, 6.2× faster. Measured scope: Build and push next-gen images step of the buildx-next job. Cache condition: Cold. Sources: Public benchmark fork · Merged agent PR.
LiveKit · 22,354 upstream stars on 2026-11-23. Docker build steps: 581 s → 118 s from a cold start, 4.9× faster. Measured scope: Build and push steps of both Docker jobs. Cache condition: Cold. Sources: Public benchmark fork · Merged agent PR.
transfer.sh · 15,910 upstream stars on 2026-11-23. Image build step: 442 s → 74 s from a cold start, 6.0× faster. Measured scope: Image build step of the buildx job. Cache condition: Cold. Sources: Public benchmark fork · Merged agent PR.
WriteFreely · 5,298 upstream stars on 2026-11-23. Image build step: 525 s → 102 s from a cold start, 5.1× faster. Measured scope: Build and push Docker images step of the build job. Cache condition: Cold. Sources: Public benchmark fork · Merged agent PR.
Cross-compile cgo with tonistiigi/xx
Go turns cgo off when it cross-compiles unless CGO_ENABLED=1 is set, and a cgo build then needs a C toolchain and libraries for the target. tonistiigi/xx supplies both: xx-apk installs the target's packages, xx-go sets GOOS, GOARCH and GOARM from TARGETPLATFORM and points cgo at a clang cross toolchain, and xx-verify checks that each binary targets the right CPU. Rust -sys crates and native Node add-ons follow the same shape with their own cross toolchains.
FROM --platform=$BUILDPLATFORM tonistiigi/xx:1.6.1 AS xx
FROM --platform=$BUILDPLATFORM golang:1.25-alpine AS build
COPY --from=xx / /
RUN apk add --no-cache clang lld
ARG TARGETPLATFORM
RUN xx-apk add --no-cache gcc musl-dev
WORKDIR /src
COPY . .
ENV CGO_ENABLED=1
RUN xx-go build -o /out/app ./cmd/app && xx-verify /out/appVerify it worked
Hand this prompt to your coding agent (Claude Code, Cursor, and the like) to run this audit and open the fix as a reviewable PR.
Find the Docker builds in this repository that compile a foreign CPU architecture
under emulation, and move that compile onto the build machine's own CPU.
1. Read this repository's agent instructions (AGENTS.md, CLAUDE.md, CONTRIBUTING)
and the Docker multi-platform and cross-compilation docs linked on this page
before changing anything.
2. grep .github/workflows/ for "platforms:" and "--platform". For each job, note
its runs-on label and every platform it builds. Skip a platform the runner
builds natively.
3. Open each Dockerfile those jobs build. For every stage the final image copies
from, check whether it runs a compile (go build, cargo build, make, cmake,
npm rebuild, pip wheel) and whether its FROM line, or the stage it is based
on, has --platform=$BUILDPLATFORM. List each compile stage that runs on the
target platform.
4. Measure before changing: from the last few runs' logs, take the Buildx RUN
step times for the foreign platform and the native one. Report them. Stop and
tell me if the foreign compile is under half of the image build's layer time.
5. For each listed stage, add --platform=$BUILDPLATFORM to its FROM and pass the
target to the compiler (Go: GOOS/GOARCH/GOARM from TARGETOS/TARGETARCH/
TARGETVARIANT). If it needs cgo or another native toolchain, use
tonistiigi/xx (xx-apk, xx-go or xx-clang) and add xx-verify on every binary.
If the toolchain cannot target a platform, stop and tell me; do not force it.
6. Keep every platform, test, scan and publish step. Leave runtime stages (the
ones that only copy the binary) on the target platform.
7. ASK ME TO APPROVE THE EXACT DIFF BEFORE EDITING ANY FILES. After approval,
work on a new branch and open a pull request; never push to the default
branch. In the PR body, list each stage changed, the before and after
foreign-platform compile times, and how each binary's architecture was
verified.Confirm the change landed
In the build log, confirm the compile
RUNstep for each foreign platform now shows the build platform (for example[linux/amd64->arm64 build ...]) and runs in about native time.Run
docker buildx imagetools inspect <image>and confirm the manifest lists every platform it listed before.Check each binary's architecture:
xx-verifyin the build, ordocker run --rm --platform linux/arm64 <image> uname -mon a host that runs that platform.Compare the image build step's median over five runs with the change and five without, and the job's runner minutes.
Go further
One fix, all of them, or forever.
You have the prompt for this one practice. Here is how much further you can take it, each step doing more for you than the last.
Fix this one thing
Copy the prompt above
Hand OPT102 to your coding agent and fix it in your repo today.
Fix everything, once
Install the ci-speedup skill
One prompt audits your whole repo against all 109 ci-speedup patterns (this one plus 108 more) and hands your agent every fix at once. Open source, MIT, runs locally.
Keep it fixed, forever
Sign up with GitHub
Connect GitHub and the fixes stay applied as your CI evolves, with agents that keep inspecting your workflows and opening optimization PRs you review.
Sources
1Docker: Multi-platform builds (opens in new tab)
2Docker: Faster multi-platform builds with Dockerfile cross-compilation (opens in new tab)
3tonistiigi/xx: Dockerfile cross-compilation helpers (opens in new tab)
Last updated 2026-11-29