GitHub Actions

Add VISUALCI_TOKEN (a project token) to the repository's Actions secrets. Then add VisualCI's action to the job that runs your tests, after them:

on:
  pull_request: # one build per pull request commit
  push:
    branches: [main] # and one per merge to main: the baselines
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ github.event.pull_request.head.sha || github.sha }}
          fetch-depth: 0 # VisualCI finds the baseline through the commit's ancestors
      - run: npm test -- -u # write snapshots instead of failing; see the snapshot guides
      - uses: DocSpring/visualci/clients/github-action@v0
        if: always()
        with:
          token: ${{ secrets.VISUALCI_TOKEN }}
          upload: |
            src --from auto
            screenshots --platform chromium-linux --threshold 0.1

@v0 follows each new release of the action and its CLI. To keep a new release from changing a build, pin a release tag such as @v0.2.34 in its place, as you would VISUALCI_VERSION in other CIs.

Run it on pull_request and on pushes to your main branch only: with push on every branch too, each commit to a pull request runs twice and sends two builds.

The action installs the CLI, creates the build, uploads and finalizes it. upload takes one visualci upload per line, as its arguments (paths, --from, --prefix, --platform, --format); leave it out to send the uploads in visualci.yml. Without a token the step fails, so a misnamed secret can't pass quietly. The token comes from token, or from VISUALCI_TOKEN in the workflow's or job's env. The exceptions are where GitHub passes no repository secrets: a fork's pull request and runs in a fork get none, and Dependabot's pull requests get only Dependabot secrets. The action skips those with a notice; send a fork's from a second workflow, and add VISUALCI_TOKEN to the Dependabot secrets to review Dependabot's.

Input What it does
token The project token, else VISUALCI_TOKEN from env. With neither, the step fails (see above for the runs it skips).
upload What to send, one visualci upload argument line per line.
name The build's name, one per suite, each with its own GitHub check. Default default.
wait Wait for the result, and fail the step when the project's CI mode says CI should fail.
allow-empty The build may have no snapshots on purpose.
finalize, finalize-only For several jobs sharing one build (below).
version The CLI release to install. Default: the release that tag of the action came with.

Several jobs, one build

Every job of a workflow run that uses the same name joins one build. Send from each job with finalize: false, then finish the build in a job that needs them:

  shots:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        browser: [chromium, webkit]
    steps:
      # ... checkout and tests ...
      - uses: DocSpring/visualci/clients/github-action@v0
        with:
          token: ${{ secrets.VISUALCI_TOKEN }}
          finalize: false
          upload: screenshots/${{ matrix.browser }} --platform ${{ matrix.browser }}-linux
  visualci:
    needs: shots
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - uses: DocSpring/visualci/clients/github-action@v0
        with:
          token: ${{ secrets.VISUALCI_TOKEN }}
          finalize-only: true

Pull requests from forks

A fork's pull request runs without the repository's secrets, so it can't send a build. Split the work in two: the pull request's workflow runs the tests and saves the snapshots as an artifact, and a workflow_run workflow, which has the secrets, sends them.

# .github/workflows/test.yml: runs the fork's code, with no secrets
on: pull_request
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: npm test -- -u
      - uses: actions/upload-artifact@v4
        with:
          name: visualci
          path: screenshots
# .github/workflows/visualci.yml: sends the snapshots, never runs the fork's code
on:
  workflow_run:
    workflows: [test]
    types: [completed]
permissions:
  actions: read # download the artifact
  pull-requests: read # find the pull request
  contents: read
jobs:
  visualci:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4 # your default branch: the fork's commits are only fetched
        with:
          fetch-depth: 0
      - uses: DocSpring/visualci/clients/github-action@v0
        with:
          token: ${{ secrets.VISUALCI_TOKEN }}
          artifact: visualci
          upload: . --platform chromium-linux

The action builds for the pull request's commit, not the workflow_run commit. It looks up the pull request by the fork's branch and checks it against that commit, and fetches the pull request's commits (without checking them out) so VisualCI finds the baseline. upload paths are relative to the artifact. A fork's branch is named with its owner (octocat:main), so it's never taken for your default branch.

Don't check out or run the fork's code in the workflow_run workflow: it has your secrets. The action only uploads the artifact's files; it never runs anything from it.

Commit, branch and pull request

On a pull_request event, GITHUB_SHA is a merge commit GitHub made. The CLI reads the branch's own commit and the pull request number from the event payload (GITHUB_EVENT_PATH) instead, so the check lands on the branch's commit. On other events it reads GITHUB_SHA. The branch comes from GITHUB_HEAD_REF on pull requests and GITHUB_REF_NAME otherwise.

The checkout step above checks out that same commit, so the history the CLI sends starts from it.

fetch-depth: 0 gives the CLI the commit's history, which it sends so VisualCI can find the nearest approved ancestor.

Make the check required

VisualCI's check stays pending until someone, or an agent, approves the build's changes. The check needs the VisualCI GitHub App on the repository, and a required check needs GitHub Pro, Team or Enterprise for a private repository. To stop pull requests merging before the review:

  1. Let one pull request run VisualCI first, so GitHub has seen the check.
  2. In the repository, open Settings → Rules → Rulesets and choose New ruleset → New branch ruleset.
  3. Name it, set Enforcement status to Active, and under Target branches add the default branch.
  4. Tick Require status checks to pass, choose Add checks, and pick VisualCI (or VisualCI: <name> for each named suite). Set its source to the VisualCI app, so a workflow can't post a check with the same name.
  5. Choose Create.

To add the same ruleset from the command line, see Make the check block merges.

The older Settings → Branches → Branch protection rules page works the same way: Require status checks to pass before merging, then search for the check by name.

Skipping the tests on some pull requests

A required check that never arrives blocks the pull request forever. So don't use paths: on the workflow's trigger to skip VisualCI: when the workflow doesn't run, the check never appears. Run the workflow on every pull request, decide inside it whether the visual tests need to run, and when they don't, send an empty build that carries the baseline forward:

jobs:
  test:
    runs-on: ubuntu-latest
    env:
      VISUALCI_TOKEN: ${{ secrets.VISUALCI_TOKEN }}
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ github.event.pull_request.head.sha || github.sha }}
          fetch-depth: 0
      - uses: dorny/paths-filter@v3
        id: changes
        with:
          filters: |
            visual:
              - 'src/**'
              - 'templates/**'
      - name: Install VisualCI
        run: |
          curl -fsSLo visualci.tar.gz https://github.com/DocSpring/visualci/releases/download/v0.2.34/visualci-x86_64-unknown-linux-musl.tar.gz
          echo "0528c8a6805c3b928eca239fcf6df5cdd09ea03ff1dcd659c66ca888dc40e948  visualci.tar.gz" | sha256sum -c
          mkdir -p "$RUNNER_TEMP" && tar xzf visualci.tar.gz --strip-components=1 -C "$RUNNER_TEMP" visualci-x86_64-unknown-linux-musl/visualci
          echo "$RUNNER_TEMP" >> "$GITHUB_PATH"
      - name: Start a VisualCI build
        # A skipped run sends nothing: --subset carries every snapshot forward.
        run: echo "VISUALCI_BUILD_ID=$(visualci build create ${{ steps.changes.outputs.visual != 'true' && '--subset' || '' }})" >> "$GITHUB_ENV"
      - if: steps.changes.outputs.visual == 'true'
        run: npm test -- -u
      - name: Send snapshots
        if: always()
        run: |
          if [ "${{ steps.changes.outputs.visual }}" = true ]; then
            visualci upload --from auto src tests
            visualci build finalize
          else
            visualci build finalize --allow-empty
          fi

When the tests are skipped, the build is created with --subset, so the snapshots it wasn't sent are carried forward instead of counted as removed, and it's finalized with --allow-empty, so a build with no snapshots passes. Its check passes as unchanged. A run that does test sends every snapshot, so one that disappears still shows as removed, and an upload that finds nothing still fails.

The skipped build passes only when the branch's last build of the suite is approved (or had nothing to approve). If that build is still being compared, waiting for review, rejected or failed, the skipped build shows its result instead, with a warning naming it, and turns green when that build is approved: a skipped push can't pass on the old baseline while the branch has a change nobody approved. A subset build that sent some snapshots carries the branch's last unapproved version of each snapshot it wasn't sent, for review in this build (a rejected one stays rejected), waiting up to 30 minutes for that build to finish comparing. A last build superseded by a pull request's review goes by that review: only an approval lets the skipped build pass. A last build that was sent its snapshots but never finalized is finalized by the skipped build (once it's been quiet for 5 minutes), which then stands on its review.

Blacksmith, Depot and other runners

Blacksmith, Depot and other GitHub Actions runner providers run the same workflow with a different runs-on. Nothing else changes on x86_64 runners. On an arm64 runner, the action installs the arm64 CLI itself; the snippets that download the CLI directly need visualci-aarch64-unknown-linux-musl.tar.gz and that file's checksum from the release.

With the CLI directly

The same steps without the action, for a workflow that needs them spelled out:

jobs:
  test:
    runs-on: ubuntu-latest
    env:
      VISUALCI_TOKEN: ${{ secrets.VISUALCI_TOKEN }}
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ github.event.pull_request.head.sha || github.sha }}
          fetch-depth: 0
      - name: Install VisualCI
        run: |
          curl -fsSLo visualci.tar.gz https://github.com/DocSpring/visualci/releases/download/v0.2.34/visualci-x86_64-unknown-linux-musl.tar.gz
          echo "0528c8a6805c3b928eca239fcf6df5cdd09ea03ff1dcd659c66ca888dc40e948  visualci.tar.gz" | sha256sum -c
          mkdir -p "$RUNNER_TEMP" && tar xzf visualci.tar.gz --strip-components=1 -C "$RUNNER_TEMP" visualci-x86_64-unknown-linux-musl/visualci
          echo "$RUNNER_TEMP" >> "$GITHUB_PATH"
      - name: Start a VisualCI build
        run: echo "VISUALCI_BUILD_ID=$(visualci build create)" >> "$GITHUB_ENV"
      - run: npm test -- -u
      - name: Send snapshots
        if: always()
        run: |
          visualci upload --from auto src tests
          visualci build finalize

Parallel jobs

Matrix or sharded jobs can share one build. Each job creates the build with the same nonce, and finalizes its own shard:

export VISUALCI_BUILD_ID=$(visualci build create --nonce "$GITHUB_RUN_ID-$GITHUB_RUN_ATTEMPT" --shards 4)
# ... tests and uploads ...
visualci build finalize --shard "$SHARD_INDEX" # 0 to 3

See Other CI providers.

Re-running failed jobs

"Re-run failed jobs" starts a new attempt ($GITHUB_RUN_ATTEMPT), and only the failed jobs run in it. A suite whose jobs passed the first time gets a new build that is sent nothing. That build doesn't fail: VisualCI sees that an earlier build of the same suite (the same --name) already compared this commit on this branch, and gives the new build that build's result. Its check shows the earlier build's result, and a review of the earlier build reaches it too. The new build has a warning naming the earlier build. If the earlier build is still being compared, the new one waits for it. If the first run's finalizing job never ran (a job before it failed), its build was never finalized: the new build finalizes it and follows it, once nothing has arrived for it for 5 minutes, even after VisualCI closed it because the run failed. Re-runs need no changes to the workflow.

Forks and missing secrets

Pull requests from forks don't get the repository's secrets, so VISUALCI_TOKEN is empty there. Decide at the job level, not in a step: a step's if can't read the env that same step sets, so if: env.VISUALCI_TOKEN != '' on a step that sets it never sees the token. Either set env on the job and test it in each VisualCI step, or skip the steps for forks:

jobs:
  test:
    env:
      VISUALCI_TOKEN: ${{ secrets.VISUALCI_TOKEN }}
    steps:
      - name: Start a VisualCI build
        if: env.VISUALCI_TOKEN != ''
        run: echo "VISUALCI_BUILD_ID=$(visualci build create)" >> "$GITHUB_ENV"
      # ...
      - name: Send snapshots
        if: always() && env.VISUALCI_TOKEN != ''
        run: |
          visualci upload --from auto src tests
          visualci build finalize

When the tests are skipped on purpose but the commit should still get a check, create the build with --subset and finalize it with --allow-empty (see Skipping the tests on some pull requests).