Files
PaperClipAI/.github/workflows/docker.yml
T
DottaandPaperclip 320d7fe76d perf(docker): consume attested Runner image in merge-queue builds and promote on master (#22993)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - The repository includes `.github/workflows/docker-merge-group.yml`
and `scripts/merge-group-image.mjs` to pre-build production images in
the GitHub merge queue so `.github/workflows/docker.yml` can promote
verified bytes on `master` instead of rebuilding from scratch
> - Currently, `docker.yml` skips merge-queue promotion whenever
`needs.runner_image.outputs.mode == 'published'`, and
`docker-merge-group.yml` builds the Runner inline on 4-core
`ubuntu-latest` while waiting up to 15 minutes during promotion lookup
> - With `RUNNER_IMAGE_CONSUME_ENABLED=true`, that meant enabling the
merge queue would never promote pre-built images on normal commits
> - This pull request wires read-only attested Runner image consumption
into `docker-merge-group.yml`, records and verifies `runnerInputsDigest`
in `scripts/merge-group-image.mjs`, routes canonical `amd64` merge-group
builds to `cloud-release-32`, parallelizes disk cleanup, caps promotion
wait to 3 minutes, and allows `docker.yml` to promote verified
merge-queue images when `runner_image` resolves to `published`
> - The benefit is enabling fast pre-merge image promotion for
merge-queue commits without weakening attestation, Runner-digest
matching, or credential isolation

## Linked Issues or Issue Description

**What existing behavior does this improve?**
Pre-merge Docker image building and promotion across
`.github/workflows/docker-merge-group.yml`,
`scripts/merge-group-image.mjs`, and `.github/workflows/docker.yml`.

**Subsystem affected**
Cross-cutting (multiple of the above)

**Current behavior**
`docker-merge-group.yml` builds the Runner inline on `ubuntu-latest`
with serial disk cleanup, and `docker.yml` disables merge-queue
promotion whenever `needs.runner_image.outputs.mode == 'published'`.
`scripts/merge-group-image.mjs` also waits up to 15 minutes for an
in-flight merge-group run.

**Proposed behavior**
`docker-merge-group.yml` resolves an already-published Runner image
(non-blocking lookup, read-only permissions, `gh attestation verify`),
runs `amd64` on `vars.CORE_PUBLICATION_RUNNER || 'cloud-release-32'`,
skips unnecessary disk cleanup / runs cleanup in parallel, and records
`runnerInputsDigest` in `record.json`. `docker.yml` attempts merge-queue
promotion regardless of whether `runner_image` resolved `published` or
`inline`, passing `RUNNER_INPUTS_DIGEST` to `merge-group-image.mjs
verify` so promotion only succeeds when the merge-group image used the
exact same Runner inputs digest. Promotion wait is capped at 3 minutes
so `docker.yml` falls back quickly to building from source if a
merge-group build is still running.

**Reason and benefit**
Makes merge-queue pre-built image promotion work with published Runner
images so queued commits can promote verified bytes to GHCR instead of
rebuilding after merge.

**Breaking changes**
None.

## What Changed

- Added a read-only `runner_image` resolution and attestation
verification job to `.github/workflows/docker-merge-group.yml`
(non-blocking lookup, `persist-credentials: false`, SHA-pinned actions,
no secrets or registry writes).
- Routed canonical `amd64` merge-group builds in
`docker-merge-group.yml` to `vars.CORE_PUBLICATION_RUNNER ||
'cloud-release-32'`, skipped lockfile refresh when using a published
Runner, parallelized `Free runner disk` with a `>= 50 GiB` skip, and
passed `build-contexts`, `build-args`, and `labels` into `Build image
archive`.
- Updated `scripts/merge-group-image.mjs` to record and verify
`runnerInputsDigest`, include `scripts/runner-image-resolve.mjs` in
`pipelineFiles`, and cap `findWaitMs` at 3 minutes.
- Updated `.github/workflows/docker.yml` to output `inputs_digest` from
`runner_image`, run `Find the merge-queue build of this commit` even
when `mode == 'published'`, and verify `--runner-inputs-digest` before
promoting.
- Updated `scripts/merge-group-image.test.mjs`,
`server/src/__tests__/docker-build-stamp.test.ts`, and
`server/src/__tests__/docker-runner-workflows.test.ts`.

## Verification

- `node --test scripts/merge-group-image.test.mjs
scripts/__tests__/docker-cache-export-policy.test.mjs
scripts/__tests__/release-verify-workflow.test.mjs
scripts/macos-runner-runtime-workflow.test.mjs` (all passed)
- Vitest: `server/src/__tests__/docker-build-stamp.test.ts` (18/18
passed), `server/src/__tests__/docker-runner-workflows.test.ts` (11/11
passed)

## Risks

- Low risk. Promotion remains strictly fail-closed: `docker.yml` runs
`$BEFORE:scripts/merge-group-image.mjs`, refuses promotion on any
pipeline file change, verifies commit SHA, tree SHA, architecture,
archive blob digests, and exact `runnerInputsDigest`, and falls back to
building from source on any mismatch or timeout.

## Model Used

- Gemini 3 Pro (Antigravity agent with tool use, shell execution, and
local test verification)

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-11-25 15:22:50 -06:00

780 lines
37 KiB
YAML

name: Docker
on:
push:
branches:
- "master"
tags:
- "v*"
- "nightly/v*"
- "beta/v*"
# Release workflows push lane tags with GITHUB_TOKEN, and GitHub suppresses
# push-triggered runs for those, so release.yml (canary-finalize.yml for a
# canary) dispatches this workflow at the new tag ref instead. The tag mapping below keys off github.ref either
# way.
workflow_dispatch:
# Least privilege: nothing at the workflow level; each job declares exactly
# the token scopes it uses (checkout needs contents:read, GHCR pushes need
# packages:write).
permissions: {}
# Serialise builds per ref without killing an in-flight one: a newer push
# supersedes only the pending slot, so the image build that is already
# running always finishes and publishes. Canary TAG refs each get their
# own group on purpose: their builds run in parallel so every published
# canary gets its sha images regardless of merge cadence. The mutable
# `:canary` channel tags are NOT written by the build matrix (which
# would race across parallel runs) — each canary-tag run retags the
# channel afterwards, only if it still matches the npm `canary`
# dist-tag, so the channel moves monotonically and always mirrors npm.
concurrency:
group: docker-${{ github.ref }}
cancel-in-progress: false
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'
continue-on-error: true
runs-on: ubuntu-latest
timeout-minutes: 70
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: 55
env:
WAIT: ${{ github.event_name == 'push' && github.ref == 'refs/heads/master' }}
GITHUB_TOKEN: ${{ github.event_name == 'push' && github.ref == 'refs/heads/master' && github.token || '' }}
run: |
set -euo pipefail
if [ "$WAIT" = true ]; then
node scripts/runner-image-resolve.mjs resolve --dir "$RUNNER_TEMP/runner-image-resolve" --wait
else
node scripts/runner-image-resolve.mjs resolve --dir "$RUNNER_TEMP/runner-image-resolve"
fi
- 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."
echo "The attestation of \`$IMAGE\` did not verify: the image build makes its own Runner." >> "$GITHUB_STEP_SUMMARY"
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<<runner-image-labels-end"
echo "$LABELS"
echo "runner-image-labels-end"
} >> "$GITHUB_OUTPUT"
echo "The attestation of \`$IMAGE\` verified: the image build copies this Runner." >> "$GITHUB_STEP_SUMMARY"
source_verification:
name: Watch exact-source verification
if: github.repository == 'paperclipai/paperclip' && github.repository_id == '1170821064' && github.ref == 'refs/heads/master'
runs-on: ${{ vars.AWS_POST_MERGE_CI_ENABLED == 'true' && github.run_attempt >= 1 && github.repository == 'paperclipai/paperclip' && github.repository_id == '1170821064' && github.ref == 'refs/heads/master' && (github.event_name == 'push' || github.event_name == 'workflow_dispatch') && 'runs-on/fleet=paperclip-post-merge-x64/env=public-ci' || 'ubuntu-latest' }}
timeout-minutes: 44
permissions:
contents: read
actions: write
checks: read # runner-loss evidence (annotations) for readiness retries
outputs:
verified: ${{ steps.watch.outputs.verified }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1
with:
persist-credentials: false
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020
with:
node-version: 24
- name: Stop images when source checks fail and report runner waits
id: watch
env:
GITHUB_TOKEN: ${{ github.token }}
GITHUB_TOKEN_SOURCE: github.token
run: node scripts/watch-release-source.mjs
reuse_canary_image:
if: github.repository == 'paperclipai/paperclip' && github.repository_id == '1170821064' && startsWith(github.ref, 'refs/tags/canary/v')
runs-on: ubuntu-latest
timeout-minutes: 10
permissions:
contents: read
packages: write
attestations: read
outputs:
reused: ${{ steps.reuse.outputs.reused }}
steps:
- name: Checkout
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1
- name: Setup Node.js
uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020
with:
node-version: 24
- name: Login to GitHub Container Registry
uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Reuse the verified master image when present
id: reuse
env:
GH_TOKEN: ${{ github.token }}
run: node scripts/reuse-canary-image.mjs
# Multi-arch by native runner, not QEMU.
#
# This was one job building linux/amd64,linux/arm64 together on an x86
# runner. The arm64 half is emulated there, and it did not merely run slow:
# it wedged, every time, in `RUN pnpm --filter @paperclipai/server build`,
# emitting nothing for 38-45 minutes until `timeout-minutes: 60` killed the
# job. Verified across three consecutive runs on 2026-09-04; the amd64 half
# reached `production 5/5` minutes earlier in every one.
#
# A timed-out job is reported as *cancelled*, not failed, so the run read
# "cancelled" and the production image simply stopped publishing without
# anything going red in an obvious way.
#
# It also starved the queue. A run that burns the full hour holds the
# top-level concurrency slot for that hour, and `cancel-in-progress: false`
# keeps exactly one pending slot — so with merges arriving faster than one
# an hour, most runs were superseded before they ever started a job. Five of
# ten master commits sampled that day never produced an image at all.
#
# Each platform now builds on a runner of its own architecture and pushes by
# digest; `merge` assembles the manifest list. Both architectures remain
# available to self-hosted installations and downstream image composers.
build-and-push:
needs: [reuse_canary_image, runner_image, macos_runner_runtime]
if: ${{ !cancelled() && needs.macos_runner_runtime.result == 'success' && (needs.reuse_canary_image.result == 'skipped' || (needs.reuse_canary_image.result == 'success' && needs.reuse_canary_image.outputs.reused == 'false')) }}
strategy:
# Independent legs: one architecture failing should still publish
# nothing, but it must not also hide the other's logs behind a
# cancellation.
fail-fast: false
matrix:
include:
- platform: linux/amd64
runner: ubuntu-latest
arch: amd64
- platform: linux/arm64
runner: ubuntu-24.04-arm
arch: arm64
runs-on: ${{ vars.CORE_RELEASE_AWS_ENABLED == 'true' && github.repository == 'paperclipai/paperclip' && github.repository_id == '1170821064' && github.ref == 'refs/heads/master' && format('runs-on/fleet=paperclip-core-image-{0}/env=public-ci', matrix.arch == 'amd64' && 'x64' || 'arm64') || matrix.runner }}
# Stop before the AWS worker's 45-minute maximum lifetime. Hosted tag
# builds retain their existing limit.
timeout-minutes: ${{ vars.CORE_RELEASE_AWS_ENABLED == 'true' && github.repository == 'paperclipai/paperclip' && github.repository_id == '1170821064' && github.ref == 'refs/heads/master' && 40 || 60 }}
permissions:
contents: read
packages: write
actions: read # download a verified merge-queue image artifact of this commit
steps:
- name: Checkout
uses: actions/checkout@v7
with:
# Full history and tags so `git describe` below can compute the
# release version to stamp into the image.
fetch-depth: 0
# Keep the version's full commit graph without downloading blobs
# from every historical revision of this large repository.
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/
# `.git` is dockerignored, so a running image cannot derive its own
# version and otherwise reports the source package.json placeholder in
# analytics and the debug panel. Compute it here from the pristine
# checkout (real CalVer drift from the nearest release tag) and pass it
# into both builds. Empty when no release tag is reachable — the server
# then keeps its existing fallbacks.
- name: Compute build version
id: build-version
run: |
set -euo pipefail
case "${GITHUB_REF}" in
refs/tags/nightly/v*)
# Lane tags carry the exact published version; stamp it verbatim
# instead of describing drift from the nearest stable tag.
version="${GITHUB_REF#refs/tags/nightly/v}"
;;
refs/tags/beta/v*)
version="${GITHUB_REF#refs/tags/beta/v}"
;;
*)
version="$(git describe --tags --match 'v*' --long --dirty 2>/dev/null || true)"
;;
esac
echo "version=${version}" >> "$GITHUB_OUTPUT"
echo "Stamping build version: ${version:-<none>}"
# ISO week stamp for the Dockerfile's tool layer: the layer caches
# across commits and re-pulls the @latest CLI tools when the week rolls
# over, instead of on every build.
- 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
# No dependency cache here: this workflow publishes release images, and
# restoring a shared Actions cache into the build inputs would let a
# poisoned cache entry reach the published artifact.
- 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 [ -z "$changed" ]; then
echo "Lockfile already matches package metadata."
exit 0
fi
if printf '%s\n' "$changed" | grep -Fvq ' pnpm-lock.yaml'; then
echo "Unexpected files changed during lockfile refresh:"
echo "$changed"
exit 1
fi
echo "Using refreshed pnpm-lock.yaml in the Docker build context."
- name: Free runner disk
if: ${{ !(vars.CORE_RELEASE_AWS_ENABLED == 'true' && github.repository == 'paperclipai/paperclip' && github.repository_id == '1170821064' && github.ref == 'refs/heads/master') }}
run: |
set -euo pipefail
echo "Disk before cleanup:"
df -h
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."
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
echo "Disk after cleanup:"
df -h
- name: Login to GitHub Container Registry
uses: docker/login-action@v4
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
# Build-before-merge. When the merge queue already built and probed this
# exact commit (docker-merge-group.yml, with no registry credentials),
# push those bytes. The script requires a successful merge-group run of
# this repository for this SHA and checks the record's commit, tree,
# architecture, Runner inputs digest, and archive digest. Any missing or
# mismatched proof, or any failure here, leaves
# `steps.promote.outputs.digest` empty and the build below runs as
# before. The probe below and the exact-source gate apply either way.
- name: Find the merge-queue build of this commit
id: find
if: github.repository == 'paperclipai/paperclip' && github.repository_id == '1170821064' && github.event_name == 'push' && github.ref == 'refs/heads/master'
continue-on-error: true
env:
GITHUB_TOKEN: ${{ github.token }}
BEFORE: ${{ github.event.before }}
ARCH: ${{ matrix.arch }}
# Master's copy from before this push selects the run; see the promote
# step below.
run: |
set -euo pipefail
trusted="$RUNNER_TEMP/merge-group-image-trusted.mjs"
git show "$BEFORE:scripts/merge-group-image.mjs" > "$trusted"
node "$trusted" find --arch "$ARCH"
- name: Download the merge-queue image
if: steps.find.outputs.run_id != ''
continue-on-error: true
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8
with:
name: merge-group-image-${{ matrix.arch }}
run-id: ${{ steps.find.outputs.run_id }}
github-token: ${{ github.token }}
path: ${{ runner.temp }}/merge-group-image
- name: Promote the merge-queue image
id: promote
if: steps.find.outputs.run_id != ''
continue-on-error: true
env:
ARCH: ${{ matrix.arch }}
DIRECTORY: ${{ runner.temp }}/merge-group-image
IMAGE_NAME: ghcr.io/${{ github.repository }}
BEFORE: ${{ github.event.before }}
RUNNER_INPUTS_DIGEST: ${{ needs.runner_image.outputs.inputs_digest }}
run: |
set -euo pipefail
# Say plainly when this runner image cannot promote, instead of
# failing later inside the copy.
if ! command -v skopeo >/dev/null; then
echo "::notice title=Merge-queue promotion unavailable::skopeo is not installed on this runner; building from source."
exit 1
fi
echo "Promoting with $(skopeo --version)"
# Run the verifier as master had it before this push, not the copy
# this push brings, so a push cannot weaken the check it must pass.
# The verifier refuses any push that changes the pipeline since
# BEFORE. A push whose BEFORE has no verifier builds from source.
trusted="$RUNNER_TEMP/merge-group-image-trusted.mjs"
git show "$BEFORE:scripts/merge-group-image.mjs" > "$trusted"
# Verify without writing a step output: the digest is set only after
# the exact bytes are in the registry.
verified="$(GITHUB_OUTPUT='' node "$trusted" verify --arch "$ARCH" --dir "$DIRECTORY" --since "$BEFORE" --runner-inputs-digest "$RUNNER_INPUTS_DIGEST")"
digest="$(printf '%s\n' "$verified" | sed -n 's/^digest=//p')"
printf '%s' "$digest" | grep -Eq '^sha256:[a-f0-9]{64}$'
# --preserve-digests fails rather than re-encode, so the registry
# holds exactly the archived image under the recorded digest.
skopeo copy --all --preserve-digests "oci-archive:$DIRECTORY/image.tar" "docker://$IMAGE_NAME@$digest"
echo "Promoted the merge-queue image $IMAGE_NAME@$digest"
echo "digest=$digest" >> "$GITHUB_OUTPUT"
- name: Set up Docker Buildx
if: steps.promote.outputs.digest == ''
uses: docker/setup-buildx-action@v4
with:
buildkitd-config-inline: |
[registry."docker.io"]
mirrors = ["mirror.gcr.io"]
# Deployment tooling reads these labels from the registry to verify an
# image's schema expectations against a migrator before deploying it,
# without pulling the image. The server refuses to start when the
# database is missing bundled migrations, so orchestrators need a cheap
# way to check image/migrator compatibility up front.
- name: Compute schema migration labels
id: schema
if: steps.promote.outputs.digest == ''
run: |
set -euo pipefail
last=$(ls packages/db/src/migrations/*.sql | sed 's|.*/||' | LC_ALL=C sort | tail -1)
count=$(ls packages/db/src/migrations/*.sql | wc -l | tr -d ' ')
echo "last=${last}" >> "$GITHUB_OUTPUT"
echo "count=${count}" >> "$GITHUB_OUTPUT"
# Lane tag mapping: nightly/v* tags publish `:nightly`, and only
# stable v* tags move `:latest` and the versioned tags.
# `:sha-<short>` is published on every build. `:canary` is
# deliberately absent here — the channel tag is moved by the
# dist-tag-checked retag step below, never by the build matrix,
# so parallel canary builds cannot race it backwards.
- name: Docker meta
id: meta
if: steps.promote.outputs.digest == ''
env:
LAST_MIGRATION: ${{ steps.schema.outputs.last }}
MIGRATION_COUNT: ${{ steps.schema.outputs.count }}
run: node scripts/docker-image-metadata.mjs
# Skip exporting mode=max registry cache (~2.5-3m after image push) on
# master commits when the content-addressed cache key (cacheable inputs +
# weekly CLI-tool epoch) matches the last published master image and no
# intermediate commit edited cacheable inputs. Tag builds stay read-only
# against :buildcache-<arch> so older release builds never clobber master.
- name: Compute BuildKit cache export policy
id: cache-policy
if: steps.promote.outputs.digest == ''
env:
EVENT_NAME: ${{ github.event_name }}
REF: ${{ github.ref }}
IMAGE_NAME: ghcr.io/${{ github.repository }}
CACHE_REF: ghcr.io/${{ github.repository }}:buildcache-${{ matrix.arch }}
PLATFORM: ${{ matrix.platform }}
RUNNER_IMAGE_MODE: ${{ needs.runner_image.outputs.mode }}
TOOLS_EPOCH: ${{ steps.tools-epoch.outputs.epoch }}
run: node scripts/docker-cache-export-policy.mjs
# Pre-warm the local Docker daemon with the most recent published master
# image's layers while Buildx builds and pushes, so the post-push PID 1
# orphan-reaping probe (`docker run --pull always`) only downloads and
# extracts the small changed top layers (~30s instead of ~3m).
- name: Pre-warm base layers for PID 1 probe
if: steps.promote.outputs.digest == '' && vars.CORE_RELEASE_AWS_ENABLED == 'true' && github.repository == 'paperclipai/paperclip' && github.repository_id == '1170821064' && github.event_name == 'push' && github.ref == 'refs/heads/master'
timeout-minutes: 1
continue-on-error: true
env:
IMAGE_NAME: ghcr.io/${{ github.repository }}
PLATFORM: ${{ matrix.platform }}
PID_FILE: ${{ runner.temp }}/pid1-prewarm.pid
LOG_FILE: ${{ runner.temp }}/pid1-prewarm.log
run: bash scripts/docker-pid1-prewarm.sh start
- name: Build and push by digest
id: build
if: steps.promote.outputs.digest == ''
uses: docker/build-push-action@v7
with:
context: .
# Pin the self-hosted image to the production stage explicitly:
# the Dockerfile now declares a later `cloud` stage, and without a
# target the default would silently become that stage.
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 }}
# By digest, not by tag: two runners cannot each push the same tag
# and end up with a manifest list. Each leg publishes an untagged
# image and `merge` names them together.
outputs: type=image,name=ghcr.io/${{ github.repository }},push-by-digest=true,name-canonical=true,push=true
# Registry-backed BuildKit cache instead of type=gha: the Actions
# cache is capped at 10GB per repo, and two multi-arch mode=max jobs
# evict each other, so most builds ran effectively cold. The cache
# ref lives in ghcr next to the image and is written only by this
# workflow (docker.yml runs on master/tag pushes, never on PRs).
#
# Per-arch refs now the legs are separate runners: a shared ref would
# have each leg overwrite the other's cache on every build.
cache-from: type=registry,ref=ghcr.io/${{ github.repository }}:buildcache-${{ matrix.arch }}
cache-to: ${{ steps.cache-policy.outputs.cache_to }}
labels: |
${{ steps.meta.outputs.labels }}
${{ steps.cache-policy.outputs.cache_label }}
${{ needs.runner_image.outputs.labels }}
# Exactly one of the two paths produced this leg's digest.
- name: Select image digest
id: image
env:
PROMOTED: ${{ steps.promote.outputs.digest }}
BUILT: ${{ steps.build.outputs.digest }}
run: |
set -euo pipefail
digest="${PROMOTED:-$BUILT}"
test -n "$digest"
echo "digest=$digest" >> "$GITHUB_OUTPUT"
# PID 1 must be an init that reaps adopted orphans. With node there, the
# orphans agent runs leave behind are never wait()ed and pin as zombies
# until the cgroup pid limit is exhausted and every fork() in the
# container fails.
#
# Probed here, on this leg's own architecture, against the exact digest
# just pushed. On master this runs while the exact-source gate is still
# pending, so the image pull (~95 s for amd64) is off the publication
# path, and arm64 is exercised too (the publisher runner is x86_64 only).
# Only a probed digest is exported, and merge-and-push signs an index
# only when its every Linux image is one of these probed digests.
- name: Verify PID 1 reaps orphaned processes
env:
# Through the environment, not interpolated into the script body.
IMAGE_DIGEST: ${{ steps.image.outputs.digest }}
IMAGE_NAME: ghcr.io/${{ github.repository }}
PLATFORM: ${{ matrix.platform }}
PID_FILE: ${{ runner.temp }}/pid1-prewarm.pid
run: |
set -euo pipefail
test -n "$IMAGE_DIGEST"
bash scripts/docker-pid1-prewarm.sh stop 15
echo "Verifying orphan reaping in $IMAGE_NAME@$IMAGE_DIGEST ($PLATFORM)"
docker run --rm -i --pull always --platform "$PLATFORM" "$IMAGE_NAME@$IMAGE_DIGEST" sh -s < scripts/assert-orphan-reaping.sh
# The digest is the only thing `merge` needs from this job. Carried as an
# empty file named for it, which is the upstream pattern — the name is
# the payload, so several legs can upload without colliding on content.
- name: Export digest
run: |
set -euo pipefail
mkdir -p /tmp/digests
digest="${{ steps.image.outputs.digest }}"
test -n "$digest"
touch "/tmp/digests/${digest#sha256:}"
- name: Upload digest
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7
with:
name: digests-production-${{ matrix.arch }}
path: /tmp/digests/*
if-no-files-found: error
retention-days: 1
- name: Stop background base-layer pre-warm if still running
if: always()
continue-on-error: true
env:
PID_FILE: ${{ runner.temp }}/pid1-prewarm.pid
run: bash scripts/docker-pid1-prewarm.sh stop 0
# Wait for proof on ephemeral verification capacity. Publication runners do
# only registry/signing work. Each Spot observer stays below its 45m lifetime.
publication_verification:
name: Require exact-source verification
if: ${{ !cancelled() && github.repository == 'paperclipai/paperclip' && github.repository_id == '1170821064' && github.ref == 'refs/heads/master' && needs.build-and-push.result == 'success' && needs.source_verification.result == 'success' && needs.source_verification.outputs.verified != 'true' }}
needs: [build-and-push, source_verification]
runs-on: ${{ vars.AWS_POST_MERGE_CI_ENABLED == 'true' && github.run_attempt >= 1 && github.repository == 'paperclipai/paperclip' && github.repository_id == '1170821064' && github.ref == 'refs/heads/master' && (github.event_name == 'push' || github.event_name == 'workflow_dispatch') && 'runs-on/fleet=paperclip-post-merge-x64/env=public-ci' || 'ubuntu-latest' }}
timeout-minutes: 44
permissions:
contents: read
actions: write # restart only exact-source producers deferred before admission
checks: read # runner-loss evidence (annotations) for readiness retries
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1
with:
persist-credentials: false
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020
with:
node-version: 24
- name: Require source proof and migrator admission before publication
env:
GITHUB_TOKEN: ${{ github.token }}
GITHUB_TOKEN_SOURCE: github.token
run: node scripts/watch-release-source.mjs --require-proof
# Names the per-architecture digests as one manifest list under the real
# tags. Nothing is publicly tagged until this runs, so a half-published
# multi-arch image is not a state anything can pull.
merge-and-push:
if: ${{ !cancelled() && needs.build-and-push.result == 'success' && (needs.source_verification.result == 'success' || needs.source_verification.result == 'skipped') && (needs.publication_verification.result == 'success' || (needs.publication_verification.result == 'skipped' && (github.repository != 'paperclipai/paperclip' || github.repository_id != '1170821064' || github.ref != 'refs/heads/master' || needs.source_verification.outputs.verified == 'true'))) }}
needs: [build-and-push, source_verification, publication_verification]
runs-on: ${{ github.repository == 'paperclipai/paperclip' && github.repository_id == '1170821064' && github.ref == 'refs/heads/master' && (vars.CORE_PUBLICATION_RUNNER || 'cloud-release-32') || 'ubuntu-latest' }}
timeout-minutes: 20
permissions:
contents: read
actions: read
packages: write
id-token: write
attestations: write
steps:
# Checked out for `packages/db` (schema labels) and the source-proof and
# standard-image contract scripts.
- name: Checkout
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
- name: Download digests
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8
with:
path: /tmp/digests
pattern: digests-production-*
merge-multiple: true
- name: Login to GitHub Container Registry
uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f # v4
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@37fe631027851001ddb9b187196cc803df7f5f0e # v4
- name: Setup Node.js
uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020
with:
node-version: 24
- name: Compute schema migration labels
id: schema
run: |
set -euo pipefail
last=$(ls packages/db/src/migrations/*.sql | sed 's|.*/||' | LC_ALL=C sort | tail -1)
count=$(ls packages/db/src/migrations/*.sql | wc -l | tr -d ' ')
echo "last=${last}" >> "$GITHUB_OUTPUT"
echo "count=${count}" >> "$GITHUB_OUTPUT"
# Same lane mapping as the build job; this is where it is actually
# applied, since the legs push untagged. `:canary` is deliberately
# absent, exactly as in the build job's mapping: the channel tag is
# moved only by the dist-tag-checked promote_canary_channel job below,
# so parallel canary-tag builds can never race the channel backwards.
- name: Docker meta
id: meta
env:
LAST_MIGRATION: ${{ steps.schema.outputs.last }}
MIGRATION_COUNT: ${{ steps.schema.outputs.count }}
run: node scripts/docker-image-metadata.mjs
# Revalidate the latest attempt after runner assignment without waiting
# here. A concurrent source retry must invalidate the earlier proof.
- name: Revalidate exact-source proof before publication
if: github.repository == 'paperclipai/paperclip' && github.repository_id == '1170821064' && github.ref == 'refs/heads/master'
env:
GITHUB_TOKEN: ${{ github.token }}
GITHUB_TOKEN_SOURCE: github.token
run: node scripts/watch-release-source.mjs --check-proof
- name: Create manifest list and push
working-directory: /tmp/digests
env:
DOCKER_METADATA_OUTPUT_JSON: ${{ steps.meta.outputs.json }}
run: |
set -euo pipefail
docker buildx imagetools create \
$(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
$(printf 'ghcr.io/${{ github.repository }}@sha256:%s ' *)
# The orphan-reaping probe ran in each build leg against the digest that
# leg pushed; /tmp/digests holds only probed digests. Pulling the image
# again here cost ~95 s after the source gate, on the publication path.
# This generic contract is useful to any downstream image composer. Only
# canonical master pushes own the full-SHA tag and its signed provenance.
# Capture exact registry bytes and sign that digest, so later tag movement
# cannot change what was verified. Registry content is addressed by
# digest: the resolve step fails unless every Linux image in the index is
# one the build legs probed.
- name: Resolve standard image digest
id: standard
if: github.repository == 'paperclipai/paperclip' && github.repository_id == '1170821064' && github.event_name == 'push' && github.ref == 'refs/heads/master'
run: node scripts/standard-image-contract.mjs --resolve "$GITHUB_SHA" --verified-digests /tmp/digests
- name: Attest the standard production image
if: steps.standard.outputs.digest != ''
uses: actions/attest@1e69f48acb82d1966a394da916b4c1698aa569d6 # v4
with:
subject-name: ghcr.io/paperclipai/paperclip
subject-digest: ${{ steps.standard.outputs.digest }}
push-to-registry: true
# Moves the mutable `:canary` channel tag. Kept OUT
# of the build jobs and serialized in its own lane, and — the load-
# bearing property — CONVERGENT rather than self-interested: a
# promotion does not promote "its own" canary, it retags the channel
# to whatever the npm `canary` dist-tag names at execution time,
# provided that version's sha images are published. GitHub's shared
# concurrency lane keeps one running and one pending promotion and
# REPLACES the pending slot with the latest enqueued — an older build
# finishing late can therefore evict the newest canary's pending
# promotion. With convergent promotion that eviction is harmless:
# whichever promotion survives resolves the current dist-tag fresh
# and lands the channel there (the current canary's images always
# exist by the time any later promotion runs, because per-tag build
# groups mean canary builds are never superseded and each run's
# promotion is gated on its own completed pushes). Every interleaving
# converges the Docker channel onto the npm channel.
promote_canary_channel:
if: ${{ !cancelled() && startsWith(github.ref, 'refs/tags/canary/v') && (needs.merge-and-push.result == 'success' || (needs.reuse_canary_image.result == 'success' && needs.reuse_canary_image.outputs.reused == 'true')) }}
needs: [reuse_canary_image, merge-and-push]
runs-on: ubuntu-latest
timeout-minutes: 10
permissions:
contents: read
packages: write
concurrency:
group: docker-canary-channel-promotion
cancel-in-progress: false
steps:
- name: Login to GitHub Container Registry
uses: docker/login-action@v4
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Converge the channel tags onto the current npm canary
env:
IMAGE: ghcr.io/${{ github.repository }}
GH_TOKEN: ${{ github.token }}
run: |
current="$(curl -fsS "https://registry.npmjs.org/-/package/@paperclipai%2Fdb/dist-tags" | jq -er .canary)"
sha="$(gh api "repos/${GITHUB_REPOSITORY}/commits/$(printf 'canary/v%s' "$current" | jq -sRr @uri)" --jq .sha 2>/dev/null || true)"
if [ -z "$sha" ]; then
echo "canary/v${current} does not resolve yet; a later promotion converges the channel"
exit 0
fi
short="$(printf '%s' "$sha" | cut -c1-7)"
if ! docker buildx imagetools inspect "$IMAGE:sha-${short}" >/dev/null 2>&1; then
echo "images for ${current} (sha-${short}) not published yet; its own promotion converges the channel"
exit 0
fi
docker buildx imagetools create -t "$IMAGE:canary" "$IMAGE:sha-${short}"
echo "channel tags moved to canary ${current} (sha-${short})"