diff --git a/.github/workflows/image.yml b/.github/workflows/image.yml index 1ab90e3..1cb6040 100644 --- a/.github/workflows/image.yml +++ b/.github/workflows/image.yml @@ -17,8 +17,21 @@ env: IMAGE: ghcr.io/zeroae/ci-runner jobs: + # Each architecture builds and smoke-tests on its own native runner (no + # QEMU). Only main publishes: a push to main, the schedule (which runs on + # main), or a dispatch run on main. Other branches build and smoke. build: - runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + include: + - arch: amd64 + uname: x86_64 + runner: ubuntu-latest + - arch: arm64 + uname: aarch64 + runner: ubuntu-24.04-arm + runs-on: ${{ matrix.runner }} steps: - uses: actions/checkout@v4 @@ -28,7 +41,7 @@ jobs: uses: docker/build-push-action@v6 with: context: image - platforms: linux/amd64 + platforms: linux/${{ matrix.arch }} pull: true load: true tags: ci-runner:smoke @@ -36,35 +49,69 @@ jobs: - name: Smoke run: >- docker run --rm + -e EXPECT_ARCH=${{ matrix.uname }} -v "$PWD/image/smoke.sh:/smoke.sh:ro" --entrypoint bash ci-runner:smoke /smoke.sh - - name: Decide whether to publish - id: publish + - name: Log in to GHCR + if: github.ref == 'refs/heads/main' + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + # The same build again, from the builder's cache, pushed untagged by + # digest. The merge job tags the manifest list of both digests. + - name: Push by digest + if: github.ref == 'refs/heads/main' + id: push + uses: docker/build-push-action@v6 + with: + context: image + platforms: linux/${{ matrix.arch }} + outputs: type=image,name=${{ env.IMAGE }},push-by-digest=true,name-canonical=true,push=true + + - name: Save the digest + if: github.ref == 'refs/heads/main' run: | - # Only main publishes: a push to main, the schedule (which runs on - # main), or a dispatch run on main. Other branches build and smoke. - publish=false - if [ "${{ github.ref }}" = refs/heads/main ]; then publish=true; fi - echo "publish=$publish" >> "$GITHUB_OUTPUT" - echo "date=$(date -u +%Y%m%d)" >> "$GITHUB_OUTPUT" - echo "sha=${GITHUB_SHA::7}" >> "$GITHUB_OUTPUT" + mkdir -p digests + echo "${{ steps.push.outputs.digest }}" > "digests/${{ matrix.arch }}" + + - name: Upload the digest + if: github.ref == 'refs/heads/main' + uses: actions/upload-artifact@v4 + with: + name: digest-${{ matrix.arch }} + path: digests/${{ matrix.arch }} + retention-days: 1 + + publish: + needs: build + if: github.ref == 'refs/heads/main' + runs-on: ubuntu-latest + steps: + - uses: actions/download-artifact@v4 + with: + pattern: digest-* + path: digests + merge-multiple: true + + - uses: docker/setup-buildx-action@v3 - name: Log in to GHCR - if: steps.publish.outputs.publish == 'true' uses: docker/login-action@v3 with: registry: ghcr.io username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - - name: Publish - if: steps.publish.outputs.publish == 'true' + - name: Publish the manifest list run: | - tag="${{ steps.publish.outputs.date }}-${{ steps.publish.outputs.sha }}" - for t in latest "$tag"; do - docker tag ci-runner:smoke "$IMAGE:$t" - docker push "$IMAGE:$t" - done - echo "Published $IMAGE:latest and $IMAGE:$tag" >> "$GITHUB_STEP_SUMMARY" + tag="$(date -u +%Y%m%d)-${GITHUB_SHA::7}" + refs=() + for arch in amd64 arm64; do refs+=("$IMAGE@$(cat "digests/$arch")"); done + docker buildx imagetools create -t "$IMAGE:latest" -t "$IMAGE:$tag" "${refs[@]}" + docker buildx imagetools inspect "$IMAGE:$tag" + echo "Published $IMAGE:latest and $IMAGE:$tag (linux/amd64, linux/arm64)" >> "$GITHUB_STEP_SUMMARY" diff --git a/README.md b/README.md index 9d05c77..f3ea24c 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,8 @@ the host's Docker daemon, so a `docker run -v` source is a path on the host. a bind mount from any of them works as it does on a GitHub-hosted runner, `mktemp` files and `~/` config included. Both are on the 50 GB root disk. A mount from anywhere else (`/opt`, `/etc`, `/var`) gets an empty host path. Nothing is pinned: every boot pulls -`:latest`, and the image is rebuilt weekly. +`:latest`, and the image is rebuilt weekly, for linux/amd64 and linux/arm64, +each built and smoke-tested on its own native runner. **Deploy jobs** keep their AWS access as before (typically GitHub OIDC into the repo's own role). A trust policy that pins `runner_environment: github-hosted` diff --git a/image/smoke.sh b/image/smoke.sh index 64e1f6f..7114217 100755 --- a/image/smoke.sh +++ b/image/smoke.sh @@ -1,7 +1,9 @@ #!/usr/bin/env bash # Smoke test for the ci-runner image. Run it inside the image: -# docker run --rm -v "$PWD/image/smoke.sh:/smoke.sh:ro" --entrypoint bash IMAGE /smoke.sh +# docker run --rm -e EXPECT_ARCH=aarch64 -v "$PWD/image/smoke.sh:/smoke.sh:ro" --entrypoint bash IMAGE /smoke.sh # Prints every added tool's version and fails if any check fails. +# EXPECT_ARCH (x86_64 or aarch64, as `uname -m` prints it) is the architecture +# the image was built for; without it the machine's own is expected. set -uo pipefail fail=0 @@ -45,6 +47,25 @@ check npm npm --version check aws aws --version expect python3.12 3.12 "$(python3 -c 'import sys; print("%d.%d" % sys.version_info[:2])' 2>&1)" +echo "== architecture" +# Each binary the image adds must be built for the image's architecture, so a +# wrong download (the arm64 AWS CLI or compose plugin) fails here, not in a job. +arch=${EXPECT_ARCH:-$(uname -m)} +elf_arch() { + # e_machine, bytes 18-19 of the ELF header: 62 is x86_64, 183 is aarch64. + case $(od -An -tu2 -j18 -N2 "$1" 2>/dev/null | tr -d ' ') in + 62) echo x86_64 ;; 183) echo aarch64 ;; *) echo "not ELF: $1" ;; + esac +} +expect uname "$arch" "$(uname -m)" +expect docker-bin "$arch" "$(elf_arch "$(command -v docker)")" +expect buildx-bin "$arch" "$(elf_arch /usr/local/lib/docker/cli-plugins/docker-buildx)" +expect compose-bin "$arch" "$(elf_arch /usr/local/lib/docker/cli-plugins/docker-compose)" +expect gh-bin "$arch" "$(elf_arch "$(command -v gh)")" +expect node-bin "$arch" "$(elf_arch "$(command -v node)")" +expect aws-bin "$arch" "$(elf_arch "$(readlink -f "$(command -v aws)")")" +expect aws-exe "exe/$arch" "$(aws --version 2>&1 | grep -o 'exe/[a-z0-9_]*')" + echo "== runner user" id runner expect whoami runner "$(whoami)"