Skip to content

Latest commit

 

History

History
372 lines (328 loc) · 19 KB

ci.md

File metadata and controls

372 lines (328 loc) · 19 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 checks out full history and doesn't persist credentials:

- uses: actions/checkout@v6
  with:
    fetch-depth: 0 # both the merge base and the head must be in the checkout
    persist-credentials: false # extractors never see the token

Here's why each setting matters:

  • fetch-depth: 0 ensures both the merge base and head commits (and the history between them) are present. A shallow checkout fails with Commit … is not in the checkout. The only downside is a slightly slower checkout in very large repositories.
  • 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 pull requests can reuse the merge base's artifact instead of re-extracting it (see what a pull request installs).
  • 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

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 and build, 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:
          fetch-depth: 0 # added for Projections
          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
      - uses: githubnext/projections@v0 # added: after the build, before the tests
        if: github.event_name == 'pull_request' || github.event_name == 'push'
      - run: npm run lint
      - run: npm test

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

  • Before or after the tests: because a failing step skips everything after it, placing Projections after the build and before tests ensures projections still get reported when a test fails. Putting it at the end waits for tests and skips on failure—unless you combine your event guard with if: ${{ !cancelled() }}.
  • Steps that modify tracked files: on a push, 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:
          fetch-depth: 0 # both the merge base and the head must be in the checkout
          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 a pull request installs

On a pull request, the Action compares the merge base against the pull request head. Neither of those is the synthetic merge commit that actions/checkout checks out (refs/pull/<number>/merge), so each side 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.
  • 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.

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; declare prepare steps if you'd rather pay for that install than see the label. On a push, the checkout already is the target commit, so it's extracted in place with everything the job set up.

How it compares

On pull_request, the Action runs projections compare --all <merge-base>..<head> --definition base --json, comparing the merge base to the head using the base branch's projection definitions (just like a local projections compare). On push, it extracts every projection for the pushed commit with projections show --all <commit> --json. In both cases, it uploads one document per projection along with a projections.artifact.json manifest as an Actions artifact named projections (see the artifact contract).

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.
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 before and after snapshots to this dedicated branch so the comment shows them. Needs contents: write.
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.

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.

Pushes produce <id>.light.png and <id>.dark.png for the pushed commit, while pull requests produce <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.

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 before-and-after thumbnails pinned to that commit. 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 was compared (a pull request's merge base and head, or the pushed commit) 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. 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.