diff --git a/.github/workflows/docker-app-check.yml b/.github/workflows/docker-app-check.yml new file mode 100644 index 00000000000..7a5c62618d1 --- /dev/null +++ b/.github/workflows/docker-app-check.yml @@ -0,0 +1,82 @@ +name: Docker app image check + +on: + pull_request: + paths: + - .github/workflows/docker-app-check.yml + - Dockerfile + - .dockerignore + - server/scripts/prepare-runner-vendor.mjs + - packages/paperclip-runner/scripts/build-inputs.mjs + +permissions: {} + +concurrency: + group: docker-app-check-${{ github.event.pull_request.number }} + cancel-in-progress: true + +jobs: + runner_path: + name: Build the Runner path of the application image + runs-on: ubuntu-latest + timeout-minutes: 60 + permissions: + contents: read + 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: Refresh lockfile for Docker build context + 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 + sudo apt-get clean || true + sudo rm -rf /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 || true + docker system prune -af || true + df -h + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@37fe631027851001ddb9b187196cc803df7f5f0e # v4 + with: + buildkitd-config-inline: | + [registry."docker.io"] + mirrors = ["mirror.gcr.io"] + + - name: Build the stages that make and package the Runner + timeout-minutes: 45 + run: | + set -euo pipefail + for target in build runner-provider-pack; do + docker buildx build \ + --target "$target" \ + --build-arg "PAPERCLIP_BUILD_COMMIT=$GITHUB_SHA" \ + --cache-from type=registry,ref=ghcr.io/paperclipai/paperclip:buildcache-amd64 \ + --output type=cacheonly \ + . + done diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 99c45466003..e1b3e712cd9 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -33,6 +33,50 @@ concurrency: cancel-in-progress: false jobs: + runner_image_report: + name: Report the Runner image of this commit + if: github.repository == 'paperclipai/paperclip' && github.repository_id == '1170821064' && github.event_name == 'push' && github.ref == 'refs/heads/master' + continue-on-error: true + runs-on: ubuntu-latest + timeout-minutes: 8 + permissions: + contents: read + 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: Report the Runner an image build would use + timeout-minutes: 2 + run: | + set -euo pipefail + node scripts/runner-image-resolve.mjs resolve --dir "$RUNNER_TEMP/runner-image-resolve" + echo "Report only: no build read this result." >> "$GITHUB_STEP_SUMMARY" + source_verification: name: Watch exact-source verification if: github.repository == 'paperclipai/paperclip' && github.repository_id == '1170821064' && github.ref == 'refs/heads/master' diff --git a/doc/DOCKER.md b/doc/DOCKER.md index 771f800779b..86b7d32d261 100644 --- a/doc/DOCKER.md +++ b/doc/DOCKER.md @@ -89,6 +89,15 @@ both on pull requests, for Linux AMD64 and ARM64, and publishes them from [`doc/runner-image-contract.md`](runner-image-contract.md) for the layout, the identity rules and how to consume the image. +`.github/workflows/docker.yml` builds the application image on `master` and on +tags only. `.github/workflows/docker-app-check.yml` builds the `build` and +`runner-provider-pack` stages on a pull request that changes the `Dockerfile`, +`.dockerignore` or the step that vendors the Runner into the server, so a +change to how the application image gets its Runner fails before it merges. +It refreshes the lockfile before the build, as `docker.yml` does. It holds no +credential: it reads the build cache that `docker.yml` writes on `master` +anonymously, never writes it, and exports and publishes nothing. + ## One-liner (build + run) ```sh diff --git a/doc/runner-image-contract.md b/doc/runner-image-contract.md index e022b4f484e..5d531f6bf33 100644 --- a/doc/runner-image-contract.md +++ b/doc/runner-image-contract.md @@ -147,8 +147,12 @@ built. ### Choosing the Runner for an image build `scripts/runner-image-resolve.mjs` decides whether a build of a checkout can -copy the published image. No build uses its answer yet. It has two commands, -so that the one that builds reads no registry credential: +copy the published image. No build uses its answer yet: on a `master` push, +the `runner_image_report` job of `.github/workflows/docker.yml` runs it and +writes the answer to the job summary, and nothing reads that job. The job +runs on the committed files, holds no credential (the Runner package is +public, so the lookup is anonymous) and cannot fail the run. The script has +two commands, so that the one that builds reads no registry credential: ```sh node scripts/runner-image-resolve.mjs inputs --dir "$dir" diff --git a/server/src/__tests__/docker-runner-workflows.test.ts b/server/src/__tests__/docker-runner-workflows.test.ts new file mode 100644 index 00000000000..f61ac5a69ac --- /dev/null +++ b/server/src/__tests__/docker-runner-workflows.test.ts @@ -0,0 +1,86 @@ +import { readFileSync } from "node:fs"; +import path from "node:path"; +import { fileURLToPath } from "node:url"; +import { describe, expect, it } from "vitest"; + +const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..", "..", ".."); +const read = (name: string) => readFileSync(path.join(repoRoot, ".github", "workflows", name), "utf8"); +const jobsOf = (workflow: string) => Object.fromEntries(workflow.slice(workflow.indexOf("\njobs:\n")).split(/\n(?= {2}[a-z_-]+:\n)/).slice(1) + .map((text) => [/^ {2}([a-z_-]+):/.exec(text)![1], text])); +const permissions = (job: string) => + [.../\n {4}permissions:\n((?: {6}.+\n)+)/.exec(job)![1].matchAll(/^ {6}([a-z-]+): (\w+)/gm)].map((match) => `${match[1]}: ${match[2]}`); +const steps = (job: string) => job.slice(job.indexOf("\n steps:\n")).split(/\n(?= {6}- )/).slice(1); +const scripts = (text: string) => [...text.matchAll(/^( +)run: (\|\n((?:\1 {2,}.*\n|\n)+)|.+\n)/gm)].map((match) => match[3] ?? match[2]); +const credential = /github\.token|secrets\.|GITHUB_TOKEN|GH_TOKEN|REGISTRY_PASSWORD/; +const registryWrite = /docker\/login-action|skopeo|imagetools create|actions\/attest|--push|push=true|cache-to/; + +function expectPinnedAndExpressionFree(text: string) { + const uses = [...text.matchAll(/^\s*(?:- )?uses: (\S+)/gm)].map((match) => match[1]); + expect(uses.length).toBeGreaterThanOrEqual(4); + for (const action of uses) expect(action).toMatch(/^[\w.-]+\/[\w./-]+@[0-9a-f]{40}$/); + const shell = scripts(text); + expect(shell.length).toBeGreaterThanOrEqual(2); + for (const script of shell) expect(script).not.toContain("${{"); +} + +describe("the report-only Runner image job in docker.yml", () => { + const workflow = read("docker.yml"); + const jobs = jobsOf(workflow); + const report = jobs.runner_image_report; + + it("holds no credential, can only read the repository, and cannot write to a registry or sign", () => { + expect(workflow).toMatch(/^permissions: \{\}$/m); + expect(permissions(report)).toEqual(["contents: read"]); + expect(report).not.toMatch(credential); + expect(report).not.toMatch(/github\.actor|REGISTRY_USERNAME/); + expect(report).not.toMatch(registryWrite); + expect(report).toContain("persist-credentials: false"); + expectPinnedAndExpressionFree(report); + }); + + it("bounds the step that builds and the lookup", () => { + for (const command of ["runner-image-resolve.mjs inputs", "runner-image-resolve.mjs resolve"]) { + const running = steps(report).filter((step) => step.includes(command)); + expect(running).toHaveLength(1); + expect(running[0]).toMatch(/^ {8}timeout-minutes: \d+$/m); + } + }); + + it("cannot fail the run or feed another job", () => { + expect(report).toContain("\n continue-on-error: true\n"); + expect(report).toMatch(/^ {4}timeout-minutes: \d+$/m); + expect(report).not.toMatch(/^ {4}(?:needs|outputs):/m); + expect(workflow).not.toContain("needs.runner_image_report"); + for (const [name, job] of Object.entries(jobs)) { + if (name !== "runner_image_report") expect(/^ {4}needs: .*$/m.exec(job)?.[0] ?? "").not.toContain("runner_image_report"); + } + expect(/^ {4}if: (.+)$/m.exec(report)![1]).toBe( + "github.repository == 'paperclipai/paperclip' && github.repository_id == '1170821064' && github.event_name == 'push' && github.ref == 'refs/heads/master'", + ); + }); +}); + +describe("the pull-request check of the application image's Runner path", () => { + const workflow = read("docker-app-check.yml"); + const jobs = jobsOf(workflow); + + it("runs a contributor's code with no credential and no way to publish", () => { + expect(workflow).toMatch(/^permissions: \{\}$/m); + expect(workflow).toMatch(/^on:\n {2}pull_request:\n/m); + expect(workflow).not.toMatch(/pull_request_target|workflow_run|workflow_dispatch|\n {2}push:/); + expect(Object.keys(jobs)).toEqual(["runner_path"]); + expect(permissions(jobs.runner_path)).toEqual(["contents: read"]); + expect(workflow).not.toMatch(credential); + expect(workflow).not.toMatch(registryWrite); + expect(workflow).toContain("persist-credentials: false"); + expectPinnedAndExpressionFree(workflow); + }); + + it("bounds every image build and exports nothing from it", () => { + const building = steps(jobs.runner_path).filter((step) => step.includes("docker buildx build")); + expect(building).toHaveLength(1); + expect(building[0]).toMatch(/^ {8}timeout-minutes: \d+$/m); + expect(building[0].match(/--output /g)).toEqual(["--output "]); + expect(building[0]).toContain("--output type=cacheonly"); + }); +});