name: Docker merge-queue build # Build-before-merge for the standard image. While a change waits in the merge # queue, build the production image for the exact merge-group commit, probe it, # and keep it as a run artifact. When the queue fast-forwards master to that # commit, docker.yml pushes these exact bytes (scripts/merge-group-image.mjs) # instead of rebuilding them. Without a matching successful run, docker.yml # builds from source as before. Nothing here can publish: this workflow has no # registry credentials, no secrets, and read-only access. # # Inert until the merge queue is enabled for master: no merge_group event fires # before then. on: merge_group: permissions: {} # Each merge group has its own ref; a group that the queue rebuilt replaces the # older run for that ref. concurrency: group: docker-merge-group-${{ github.ref }} cancel-in-progress: true jobs: macos_runner_runtime: uses: ./.github/workflows/macos-runner-runtime.yml permissions: contents: read actions: read runner_image: name: Resolve the Runner image of this commit if: vars.RUNNER_IMAGE_CONSUME_ENABLED == 'true' && github.repository == 'paperclipai/paperclip' && github.repository_id == '1170821064' && github.event.merge_group.base_ref == 'refs/heads/master' continue-on-error: true runs-on: ubuntu-latest timeout-minutes: 20 permissions: contents: read actions: read attestations: read outputs: mode: ${{ steps.verify.outputs.mode || 'inline' }} inputs_digest: ${{ steps.verify.outputs.inputs_digest }} build_contexts: ${{ steps.verify.outputs.build_contexts }} labels: ${{ steps.verify.outputs.labels }} build_args: ${{ steps.verify.outputs.build_args }} steps: - name: Checkout uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 with: persist-credentials: false - name: Setup pnpm uses: pnpm/action-setup@0977fd99725f1db4007ccb2928dbb4e90d06cc86 # v6 with: version: 9.15.4 run_install: false - name: Setup Node.js uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7 with: node-version: 24 package-manager-cache: false - name: Set up Docker Buildx uses: docker/setup-buildx-action@37fe631027851001ddb9b187196cc803df7f5f0e # v4 with: buildkitd-config-inline: | [registry."docker.io"] mirrors = ["mirror.gcr.io"] - name: Compute the Runner inputs of this commit timeout-minutes: 5 run: node scripts/runner-image-resolve.mjs inputs --dir "$RUNNER_TEMP/runner-image-resolve" - name: Find the published Runner image of these inputs id: resolve timeout-minutes: 5 run: node scripts/runner-image-resolve.mjs resolve --dir "$RUNNER_TEMP/runner-image-resolve" - name: Verify the attestation of the Runner image id: verify if: steps.resolve.outputs.mode == 'published' timeout-minutes: 5 env: GH_TOKEN: ${{ github.token }} IMAGE: ${{ steps.resolve.outputs.image }} INPUTS_DIGEST: ${{ steps.resolve.outputs.inputs_digest }} LABELS: ${{ steps.resolve.outputs.labels }} BUILD_ARGS: ${{ steps.resolve.outputs.build_args }} run: | set -euo pipefail if ! gh attestation verify "oci://$IMAGE" \ --repo paperclipai/paperclip \ --signer-workflow paperclipai/paperclip/.github/workflows/runner-image.yml \ --source-ref refs/heads/master; then echo "::warning title=Runner image not verified::The attestation of $IMAGE did not verify. The image build makes its own Runner." exit 1 fi { echo "mode=published" echo "inputs_digest=$INPUTS_DIGEST" echo "build_contexts=runner-artifacts=docker-image://$IMAGE" echo "build_args=$BUILD_ARGS" echo "labels<> "$GITHUB_OUTPUT" build: needs: [macos_runner_runtime, runner_image] if: ${{ !cancelled() && needs.macos_runner_runtime.result == 'success' && github.repository == 'paperclipai/paperclip' && github.repository_id == '1170821064' && github.event.merge_group.base_ref == 'refs/heads/master' }} strategy: fail-fast: false matrix: include: - platform: linux/amd64 runner: ubuntu-latest arch: amd64 - platform: linux/arm64 runner: ubuntu-24.04-arm arch: arm64 # GitHub-hosted runners: the protected release fleets and larger release # runner groups admit only docker.yml on master. runs-on: ${{ matrix.runner }} timeout-minutes: 60 permissions: contents: read steps: - name: Checkout uses: actions/checkout@v7 with: persist-credentials: false # Same history as docker.yml, for `git describe` below. fetch-depth: 0 filter: blob:none - name: Download the native macOS SSH runtime uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 with: name: ${{ needs.macos_runner_runtime.outputs.artifact_name }} path: docker/remote-runtimes/ # The merge queue fast-forwards master to this exact commit, so these # stamps match what a master build of the same commit would carry. - name: Compute build version id: build-version run: | set -euo pipefail version="$(git describe --tags --match 'v*' --long --dirty 2>/dev/null || true)" echo "version=${version}" >> "$GITHUB_OUTPUT" echo "Stamping build version: ${version:-}" - name: Compute tool cache epoch id: tools-epoch run: echo "epoch=$(date -u +%G-W%V)" >> "$GITHUB_OUTPUT" - name: Setup pnpm uses: pnpm/action-setup@v6 with: version: 9.15.4 run_install: false - name: Setup Node.js uses: actions/setup-node@v7 with: node-version: 24 - name: Refresh lockfile for Docker build context if: needs.runner_image.outputs.mode != 'published' run: | set -euo pipefail pnpm install --resolution-only --ignore-scripts --no-frozen-lockfile changed="$(git status --porcelain)" if [ -n "$changed" ] && printf '%s\n' "$changed" | grep -Fvq ' pnpm-lock.yaml'; then echo "Unexpected files changed during lockfile refresh:" echo "$changed" exit 1 fi - name: Free runner disk run: | set -euo pipefail avail_kb="$(df -Pk / | awk 'NR==2 {print $4}')" if [ "${avail_kb:-0}" -ge 52428800 ]; then echo "Root filesystem already has >= 50 GiB free; skipping cleanup." df -h exit 0 fi pids=() (pnpm store prune || true) & pids+=("$!") (sudo apt-get clean || true) & pids+=("$!") (docker system prune -af || true) & pids+=("$!") for dir in \ /usr/share/dotnet \ /usr/share/swift \ /usr/local/lib/android \ /usr/local/share/boost \ /usr/local/share/powershell \ /opt/ghc \ /opt/hostedtoolcache/CodeQL \ /opt/hostedtoolcache/PyPy \ /opt/hostedtoolcache/Ruby; do (sudo rm -rf "$dir" || true) & pids+=("$!") done for pid in "${pids[@]}"; do wait "$pid" || true done df -h - name: Enable containerd image store for OCI archive loading run: | set -euo pipefail if docker info --format '{{json .DriverStatus}}' | grep -q 'containerd.snapshotter'; then echo "Docker containerd image store already enabled." exit 0 fi sudo python3 - <<'PYTHON' import json from pathlib import Path config = Path("/etc/docker/daemon.json") settings = json.loads(config.read_text()) if config.exists() else {} settings.setdefault("features", {})["containerd-snapshotter"] = True config.write_text(json.dumps(settings)) PYTHON sudo systemctl restart docker status="$(docker info --format '{{json .DriverStatus}}')" echo "$status" echo "$status" | grep -q 'containerd.snapshotter' || { echo "Docker containerd image store failed to activate." >&2 exit 1 } - name: Set up Docker Buildx uses: docker/setup-buildx-action@v4 with: buildkitd-config-inline: | [registry."docker.io"] mirrors = ["mirror.gcr.io"] - name: Compute schema migration labels id: schema run: | set -euo pipefail last=$(find packages/db/src/migrations -maxdepth 1 -name '*.sql' -printf '%f\n' | LC_ALL=C sort | tail -1) count=$(find packages/db/src/migrations -maxdepth 1 -name '*.sql' | wc -l | tr -d ' ') echo "last=${last}" >> "$GITHUB_OUTPUT" echo "count=${count}" >> "$GITHUB_OUTPUT" # Same labels as a master build. The version label names master, not the # temporary merge-queue branch this commit is tested on. - name: Docker meta id: meta uses: docker/metadata-action@v6 with: images: ghcr.io/${{ github.repository }} labels: | org.opencontainers.image.version=master io.github.paperclipai.schema.last-migration=${{ steps.schema.outputs.last }} io.github.paperclipai.schema.migration-count=${{ steps.schema.outputs.count }} # Reads the public registry cache anonymously and writes none of it. - name: Build image archive id: build uses: docker/build-push-action@v7 with: context: . target: production build-args: | PAPERCLIP_BUILD_VERSION=${{ steps.build-version.outputs.version }} PAPERCLIP_BUILD_COMMIT=${{ github.sha }} PAPERCLIP_MACOS_RUNTIME_INPUTS_DIGEST=${{ needs.macos_runner_runtime.outputs.inputs_digest }} CLI_TOOLS_CACHE_EPOCH=${{ steps.tools-epoch.outputs.epoch }} ${{ needs.runner_image.outputs.build_args }} build-contexts: ${{ needs.runner_image.outputs.build_contexts }} platforms: ${{ matrix.platform }} push: false outputs: type=oci,dest=${{ runner.temp }}/merge-group-image/image.tar,name=paperclip-merge-group:${{ matrix.arch }} cache-from: type=registry,ref=ghcr.io/${{ github.repository }}:buildcache-${{ matrix.arch }} labels: | ${{ steps.meta.outputs.labels }} ${{ needs.runner_image.outputs.labels }} # PID 1 must be an init that reaps adopted orphans; see docker.yml. - name: Verify PID 1 reaps orphaned processes env: ARCHIVE: ${{ runner.temp }}/merge-group-image/image.tar PLATFORM: ${{ matrix.platform }} IMAGE: paperclip-merge-group:${{ matrix.arch }} run: | set -euo pipefail docker load --input "$ARCHIVE" docker run --rm -i --pull never --platform "$PLATFORM" "$IMAGE" sh -s < scripts/assert-orphan-reaping.sh docker image rm "$IMAGE" # Written only after the probe passes. Promotion trusts nothing without it. - name: Record the verified image env: ARCH: ${{ matrix.arch }} RUNNER_INPUTS_DIGEST: ${{ needs.runner_image.outputs.inputs_digest }} run: | set -euo pipefail node scripts/merge-group-image.mjs record \ --arch "$ARCH" \ --archive "$RUNNER_TEMP/merge-group-image/image.tar" \ --out "$RUNNER_TEMP/merge-group-image/record.json" \ --runner-inputs-digest "$RUNNER_INPUTS_DIGEST" - name: Upload the verified image uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 with: name: merge-group-image-${{ matrix.arch }} path: | ${{ runner.temp }}/merge-group-image/image.tar ${{ runner.temp }}/merge-group-image/record.json if-no-files-found: error # Layers are already compressed. compression-level: 0 retention-days: 3