Skip to content

Latest commit

 

History

History
447 lines (396 loc) · 23.7 KB

ci.md

File metadata and controls

447 lines (396 loc) · 23.7 KB

Add Projections to CI

Once you've adopted the projections worth keeping, wire them into CI so every pull request and push gets a summary of what changed—without anyone needing to run anything locally. All it takes is one step in your workflow using the Projections GitHub Action. It writes the summary to the workflow run (and optionally posts a pull request comment) and uploads the projection documents as an artifact that you can review locally or in a hosted view.

Because the best place for that step depends on how your CI is set up, nothing modifies your workflows automatically. Instead, paste this prompt into your coding agent—it reads your workflows, suggests where the step belongs, and shows you the diff before writing anything:

Add Projections to this repository's CI: run `projections help agents ci` and follow what it prints.

In VS Code, clicking Add to CI… in the Explore view (or running Projections: Add to CI…) sends the same prompt to your agent or opens this page. If you'd rather wire it up yourself, read on.

The step

- uses: githubnext/projections@v0

Make sure the job's checkout doesn't persist credentials:

- uses: actions/checkout@v6
  with:
    persist-credentials: false # extractors never see the token

Here's why each setting matters:

  • The commit the job checked out is what the step projects, on pushes and pull requests alike: on a pull request that's GitHub's merge commit (refs/pull/<number>/merge), the same commit your build and tests ran on. It reads what's on disk and runs no Git commands of its own, so the default shallow checkout is enough. Comparing a pull request with its merge base is opt-in.
  • persist-credentials: false keeps the token out of .git/config so no repository code running in the job can read it. This is required when using trust: true, which runs code from the pull request. Without trust, the Action doesn't execute repository code, so it adds no new exposure beyond your existing steps.
  • Events: the Action runs on pull_request and push. Any other event (merge_group, workflow_dispatch, schedule, release) fails the step, and pull_request_target is rejected outright because it would run pull request code with the base repository's secrets. Run it on pushes to your default branch as well as pull requests, so the default branch has its own projections to compare with.
  • Permissions: contents: read is all you need, since uploading an artifact requires no extra permission. See Permissions if you turn on features that write back to the repository or pull request.

The Action automatically runs every projection defined in your repository, so adopting more projections later requires no CI changes.

Where it goes

Build, test, lint, project: projecting is one more thing CI does to the commit it checked out, and it works best the way a linter does, after the steps that build and test that commit. It can use whatever they produced, including the files a definition declares as build output, such as exported reports or indexes that Git ignores.

In most repositories, adding the step to the job that already builds your code is the best fit. If you have a single CI workflow that installs dependencies, builds, tests, and lints, your toolchains and packages are already in place there—a separate workflow would repeat that expensive setup. Drop the step in after install, build and test, update that job's checkout as shown above, and guard the step if the workflow also triggers on other events:

name: CI
on:
  pull_request:
  push:
    branches: [main]
  merge_group:
permissions:
  contents: read
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
        with:
          persist-credentials: false # added for Projections
      - uses: actions/setup-node@v6
        with:
          node-version-file: .node-version
          cache: npm
      - run: npm ci
      - run: npm run build
      - run: npm test
      - run: npm run lint
      - uses: githubnext/projections@v0 # added: after build and test
        if: ${{ !cancelled() && (github.event_name == 'pull_request' || github.event_name == 'push') }}

A few things to keep in mind when adding it to an existing job:

  • When a test fails: a failing step skips everything after it, so combine your event guard with !cancelled(), as above, to report projections whether or not the tests passed. If the build itself fails, extractors that need its output fail too, each reported on its own.
  • Steps that produce what a projection reads: put them before the Projections step. The Action extracts the checked-out commit in place, on pushes and pull requests alike, so a projection reads the files its definition declares as build output from the job's working tree.
  • Steps that modify tracked files: the Action extracts the checked-out commit in place, reusing your installed dependencies and build artifacts as long as tracked files haven't been altered (untracked and ignored files like node_modules and dist are fine). Place it ahead of any step that rewrites tracked files, such as an auto-formatter or version bumper.
  • Steps that push: setting persist-credentials: false will break any step that commits or pushes using the checkout's saved credentials. The cleanest fix is to pass a token directly to that step (for example, pushing with git push to https://x-access-token:${{ github.token }}@github.com/…, or passing a token input to an action). If the job doesn't need trust, you can leave checkout credentials enabled; otherwise, move Projections into its own job.
  • Matrix jobs: running the step in every matrix cell causes each one to upload an artifact with the same name, which fails. Restrict it to a single cell (for example, if: matrix.os == 'ubuntu-latest' && matrix.node == 24) or run it in a non-matrix job.
  • Multiple workflows: pick the job whose environment matches what your extractors need. Run projections list to see which projections execute repository code (and therefore need trust), and projections doctor to see the toolchains and declared prepare steps they use. File-only extractors need no toolchain setup at all, so they work in any job or in their own workflow.
  • Monorepo projects: for a project living in a subdirectory, set working-directory to that path inside the job that builds it.

If no existing job sets up what your extractors need, if every projection only reads files, or if your build job can't accommodate the step (for instance, it pushes and requires trust), give Projections a workflow of its own at .github/workflows/projections.yml:

name: Projections
on:
  pull_request:
  push:
    branches: [main]
permissions:
  contents: read
jobs:
  projections:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
        with:
          persist-credentials: false # extractors never see the token
      - uses: githubnext/projections@v0

Adjust the default branch, runner (runs-on), and path filters to match your repo. If any projection needs your toolchain, add the required setup and install steps ahead of the Projections step, or declare them as prepare steps in .github/projections/environment.json.

What it projects

The Action projects one commit: the one the job checked out. On a push that's the pushed commit; on a pull request it's GitHub's merge commit (refs/pull/<number>/merge), the pull request applied to its base branch, which is what your build and tests ran on. It runs projections show --all <commit>, with GITHUB_SHA naming the commit, and passes --in-place, so every projection is extracted on the checkout with everything the job set up: the dependencies it installed and the build output it produced (including the files a definition declares as build output), as long as tracked files are unmodified.

The step is pure: it reads what's on disk, extracts nothing but that commit, runs no Git commands of its own and doesn't reach the remote repository. It uploads one document per projection along with a projections.artifact.json manifest as an Actions artifact named projections (see the artifact contract). On a pull request, the summary (and, with comment: true, the comment) lists each projection of the merge commit.

Before running anything, the Action downloads the matching CLI binary for the runner's OS and architecture from GitHub Releases and verifies both its .sha256 digest and its build provenance attestation via gh attestation verify. By default, it picks the CLI release matching the Action's version tag: @v0.1.0 installs v0.1.0, and @v0 tracks the latest stable v0 release without jumping to v1. Each stable release advances its major tag (v0), while pinning a commit SHA, branch, or @latest installs the latest release.

Input Default Meaning
trust false Pass --trust, so extractors that run repository code run. See below.
compare false On pull requests, compare the merge base with the head instead of projecting the checked-out commit. See Comparing a pull request.
summary true Write each projection's result, steps and changes to the job summary.
comment false Also post the summary as one pull request comment, updated on each run. Needs pull-requests: write.
retention-days repository default How long GitHub keeps the artifact.
attest false Attest the artifact's manifest with GitHub artifact attestations. Needs id-token: write and attestations: write.
fail-on-error false Fail the job when a projection fails. By default failures are reported per projection, in the summary and as annotations, and the job succeeds.
snapshots none Projection IDs to render as PNG snapshots, light and dark.
snapshots-commit, snapshots-path false, .github/projections/snapshots On pushes to the default branch, commit the snapshots for embedding in a README. Needs contents: write.
snapshots-branch none On pull requests from this repository, commit the snapshots to this dedicated branch so the comment shows them. Needs contents: write.
reuse-base-artifact true With compare, reuse the merge base's own artifact instead of extracting the base again.
version the Action's version A CLI release such as v0.1.0, a major version such as v0 (its newest stable release), or latest.
build-from-source false Build the CLI from the Action's source instead (needs Rust), for testing unreleased versions.
working-directory, artifact-name, hosted-url, token, cli-repository Less common settings, documented in action.yml.

Comparing a pull request

Comparing is opt-in. With compare: true, a pull request run compares the pull request's merge base with its head instead, using the base branch's projection definitions, as a local projections compare does: projections compare --all <merge-base>..<head> --definition base --json. The summary then walks through each projection's changes, and snapshots renders both sides. Pushes always project the pushed commit.

- uses: actions/checkout@v6
  with:
    fetch-depth: 0 # compare: both the merge base and the head must be in the checkout
    persist-credentials: false
- uses: githubnext/projections@v0
  with:
    compare: true
    comment: true # needs pull-requests: write

Comparing runs Git, to find the merge base and to fetch either commit when the checkout lacks it, so the checkout needs full history (fetch-depth: 0); a shallow checkout fails with Commit … is not in the checkout. Neither side is the merge commit the job built, so each is handled slightly differently:

  • The merge base is reused instead of re-extracted whenever the merge base's own CI run succeeded and uploaded a projections artifact (typically from the push run on your default branch). Only a run from the same workflow file in this repository for a push or in-repo pull request qualifies—a fork's pull request runs its own workflow files, so its artifacts are never reused. The Action downloads the artifact and the CLI verifies each extraction, marking it as imported rather than official because the CLI step has no token to authenticate it. That's why it helps to run the step on pushes to the default branch in a passing run, and why retention-days matters. Set reuse-base-artifact: false to extract the base here instead.
  • File-only projections read both commits directly from Git objects without installing anything.
  • Toolchain projections (running with trust) need each commit checked out on disk, so the CLI extracts them in a temporary copy. If you haven't declared prepare steps, that copy borrows the dependencies your job already installed—exact when the commit's lockfiles match the checkout, or labelled approximate (listing which lockfiles differ) when they don't. Build output isn't copied over. If you declare prepare steps, the copy runs them instead to produce a clean install for each extracted commit. TypeScript projections (and custom extractors that Node or Bun runs) don't need them for this: when a commit's JavaScript lockfile differs from the checkout's, the copy installs that commit's own packages with scripts disabled, using the package manager's cache, and falls back to the job's installation, labelled approximate, if the install fails (see Node packages a commit installs itself).
  • Build output a definition declares is read only where a commit is extracted in place, and the head isn't: unless prepare steps produce it in the copy, that projection's head is reported unavailable (BUILD_OUTPUTS_UNAVAILABLE). The merge base still comes from its push run's artifact, which read the build output in place.

In the typical case, adding the step to your build job avoids any extra installs: the merge base comes from the default branch's artifact, and the head borrows the job's dependencies (whose lockfiles match the head unless the base branch also changed them). If a pull request edits a lockfile and its merge base has no cached artifact, the base is labelled approximate rather than triggering a fresh install (except for TypeScript projections, which install it); declare prepare steps if you'd rather pay for that install than see the label.

Migrating from a comparing workflow

Earlier versions of the Action compared every pull request's merge base with its head. A workflow that relies on that, for its job summary, its comment: true pull request comment or its before-and-after snapshots, keeps it by adding compare: true and keeping fetch-depth: 0 on the checkout, as above. Without it, pull request runs project the merge commit: the summary and comment list each projection's extraction instead of its changes, snapshots show the merge commit alone (<id>.<theme>.png), and the artifact's manifest records the merge commit as revision instead of a mergeBase. To review a pull request's changes without compare, run projections pr <number> locally.

Permissions

On its own, the step only needs contents: read—uploading an artifact needs no extra permission. Add pull-requests: write only if you enable comment: true, and contents: write only for snapshots-commit or snapshots-branch. When editing an existing workflow, scope permissions: to that specific job rather than the entire workflow. If you enable trust: true, keep the job restricted to read-only permissions and no secrets (see Trust). Pull requests from forks never receive a writable token, so they won't get a PR comment, though their job summary and artifact are still generated.

Attesting the artifact

Turning on attest: true uses actions/attest to attest projections.artifact.json, which records the SHA-256 of every document in the artifact. Anyone downloading that artifact can then authenticate it: running projections show <commit> --ci --artifact DIR verifies the manifest with gh attestation verify and treats it as the commit's official extraction only if the attestation points to a run of this repository's Projections workflow on a push or an in-repo pull request. Without an attestation, an imported directory is still reusable, just labelled imported.

Attestation requires id-token: write and attestations: write on the job, and the repository must have artifact attestations available (all public repositories, or private repositories on GitHub Enterprise Cloud). Fork pull requests skip attestation because they don't receive an id-token and aren't the repository's own CI output.

Snapshots

Passing snapshots tells the Action to render the listed projections into light and dark PNG images using the real renderer via projections show --png and the runner's installed Chrome (GitHub-hosted runners include Chrome; set browser if yours doesn't). It renders straight from the documents it just collected without re-running extractors, and the render step receives no token.

Snapshots show the commit the step projected: <id>.light.png and <id>.dark.png, for the pushed commit or a pull request's merge commit. Only a pull request run with compare: true renders the base side too, as <id>.before.<theme>.png (merge base) and <id>.after.<theme>.png (head). The images and a snapshots.json index are uploaded in a separate projections-snapshots artifact so the projections artifact matches its manifest exactly. Snapshot failures are logged as warnings and never fail the job. Because a custom renderer is repository code, snapshots only use it when trust: true is set; otherwise they fall back to the built-in renderer for that schema and log a note.

A job that runs as root, such as one in a container:, can't start Chrome with its sandbox on. Set browser-no-sandbox: true there to turn the sandbox off; the log then carries a warning saying so. This is a security trade-off: a custom renderer the snapshots draw with (with trust: true) then runs without the browser's process isolation, so a renderer that exploits a browser bug reaches the job's container as root. Turn it on only where the container is itself the isolation. GitHub-hosted runners without a container don't need it.

permissions:
  contents: write       # snapshots-commit and snapshots-branch
  pull-requests: write  # comment
# …
      - uses: githubnext/projections@v0
        with:
          snapshots: architecture, files
          snapshots-commit: true                # pushes to the default branch
          snapshots-branch: projections-snapshots # pull request comments
          comment: true

Embedding in a README. With snapshots-commit: true, pushes to the default branch commit any changed images to snapshots-path (by default .github/projections/snapshots) through the GitHub API, similar to the original repo-visualizer Action's should_push. Unchanged images don't create empty commits, nothing is pushed if the branch moved in the meantime, and commits made with GITHUB_TOKEN won't trigger recursive workflows. Embed them with a picture element so they adapt to the reader's theme:

<picture>
  <source
    media="(prefers-color-scheme: dark)"
    srcset=".github/projections/snapshots/files.dark.png"
  />
  <img
    alt="Repository files"
    src=".github/projections/snapshots/files.light.png"
  />
</picture>

Showing in pull request comments. Because GitHub comments can't render Actions artifacts inline, setting snapshots-branch commits an in-repo pull request's images under pull/<number>/ on a dedicated orphan branch (created automatically if missing, and never your base or head branch). The comment then embeds theme-aware thumbnails pinned to that commit: the merge commit's, or, with compare: true, before and after. Readers need read access to the repository, just as they do for the code. Fork pull requests receive a read-only token, and without snapshots-branch nothing is committed—in both cases, the comment links to the snapshots artifact instead. The branch accumulates commits as images change, and you can delete it anytime to start fresh.

Reviewing the results

  • In the job summary: every run reports what it projected (the pushed commit or a pull request's merge commit) or, with compare: true, what it compared (a pull request's merge base and head), and lists each projection's outcome. For each comparison, it walks through the changes in the comparator's order—listing each step in plain language (Added entity …, Renamed relationship … to …) along with any comparator summaries. Failed projections show diagnostics and appear as workflow annotations. A comparison whose subject the pull request adds or removes, such as a new workflow, reads "added in this pull request (7 entities, 6 relationships)" rather than "13 changes: 13 added", and one whose subject exists at neither revision reads "not at either revision". With comment: true, that same summary (plus any snapshots) is posted and updated as a single pull request comment.
  • Locally from the pull request: run projections pr <number> to reproduce a pull request's comparisons on your machine—from merge base to head using the base definitions—without trusting the PR's code. Pass --output FILE to save the review for later.
  • In VS Code (alternate path): if you use the VS Code extension, download and unzip the projections artifact, then open projections/<id>.json with Projections: Open Artifact… (or from the Explorer context menu). It displays the saved extraction or comparison without executing anything; any provenance (including whether it claims to be official) comes from the document itself.
  • In a hosted view: because the artifact adheres to a documented contract, external services can render it too. Set hosted-url to that service's origin (for example, https://projections.example), and both the job summary and pull request comment will link straight to its view of the pull request or commit.

Trust

The Action enforces the same trust rule as the CLI. Built-in extractors that only read files always run. Custom extractors and built-in extractors that invoke your toolchain require trust: true; without it, they appear as failed projections explaining why they were skipped. Custom renderers are repository code as well, so snapshots only use them when trust: true is enabled.

On a pull request, trust: true runs code from the pull request's head in CI. Only enable it on the standard pull_request event (never pull_request_target), with no secrets, read-only permissions, and persist-credentials: false—matching how GitHub already isolates workflows for fork pull requests. If a build job already runs tests on pull requests, it already executes pull request code, so adding Projections there with trust: true stays within the same trust boundary. Reading the resulting artifact never executes repository code: the job summary, Projections: Open Artifact…, and hosted views only display saved documents.