Skip to content

CI with GitHub Actions

diffcone provides composite actions for a common setup: record the full test suite once a night on your default branch, then run only the affected tests on each pull request, using that recording.

Action Where it runs What it does
bearing-research/diffcone/actions/record A nightly job on the default branch Records the full suite with execution evidence and saves it with actions/cache.
bearing-research/diffcone/actions/run Each pull request Restores the latest recording, plans the pull request, and runs the selected tests.
bearing-research/diffcone/actions/check Optional, after a full run Checks that every test that failed in a full run was selected, and writes a summary.
bearing-research/diffcone/actions/report Optional, on a schedule Reports on the recent runs of run and record, and opens an issue for each miss.

Each action installs the diffcone version that matches its own tag, so reference them all by the same release tag (or commit), never by a branch.

Record nightly

.github/workflows/diffcone-record.yml
name: diffcone record

on:
  schedule:
    - cron: "0 3 * * *"
  workflow_dispatch:

permissions:
  contents: read

jobs:
  record:
    runs-on: ubuntu-latest
    cache-mode: write
    steps:
      - uses: actions/checkout@v7
      - uses: astral-sh/setup-uv@v10.2.0
      - run: uv sync --locked
      - uses: bearing-research/diffcone/actions/record@v0.7.0
        id: record
        with:
          key-prefix: diffcone-ubuntu
          command: uv run python -m pytest
          pytest-args: -n auto
      - uses: actions/upload-artifact@v7
        if: always()
        with:
          name: diffcone-ubuntu
          path: ${{ steps.record.outputs.results }}

The upload is optional: it keeps a description of the recording for a report on recent runs.

key-prefix names the recording; use one per test environment (operating system, Python version) if you record several. For a src layout, add source-roots: src . to both record and run. If the recording fails (a collection error, for example), nothing is saved and pull requests keep using the previous one.

Run on pull requests

.github/workflows/tests.yml
on: pull_request

jobs:
  affected-tests:
    runs-on: ubuntu-latest
    cache-mode: read
    steps:
      - uses: actions/checkout@v7
        with:
          fetch-depth: 0
      - uses: astral-sh/setup-uv@v10.2.0
      - run: uv sync --locked
      - uses: bearing-research/diffcone/actions/run@v0.7.0
        with:
          key-prefix: diffcone-ubuntu
          command: uv run python -m pytest
          pytest-args: -n auto
      - uses: actions/upload-artifact@v7
        if: always()
        with:
          name: diffcone-results
          path: diffcone-results

The job fails when a selected test fails, like any test job. It also fails when a selected test wasn't collected by pytest, when command never loaded diffcone's selection plugin (a wrapper, or a tox or nox target, that doesn't pass its arguments on to pytest), and when discovery may be incomplete (unless you set allow-incomplete-discovery: true). The plan and the run's JUnit XML are written to diffcone-results/; upload them with if: always() so they're kept when tests fail.

The action plans the change the event brings, read from the event itself:

  • for a pull request, what it changes on top of its target branch: from the merge commit's first parent when the merge commit is checked out (the default), from the pull request's base commit when you check out its head (ref: ${{ github.event.pull_request.head.sha }}, as pull_request_target workflows do). That base is the target branch as it was when the event fired, so commits on it that the pull request lacks count as changes too: more is selected, never less. A pull_request_target workflow that doesn't check out the head runs the target branch's own code, which the pull request doesn't change, so nothing is selected;
  • for a push, every commit it brings, from the commit the branch pointed at before it;
  • for a merge queue (merge_group), every queued pull request, from the queue's base commit.

When the event names no base (a push that creates a branch, a manual or scheduled run) or the base can't be fetched (a force push dropped it), the action selects every test and says why in a warning. To plan against something else, set base.

Match the recording

command and pytest-args must be the same in both workflows, and the environment must match: the same Python version and installed packages, and the same PYTEST_ADDOPTS if you set it. Installing from a lock file makes this automatic.

  • When the environment differs, diffcone runs a plan from the code instead of from the recording. If that plan's discovery may be incomplete, it runs every test, unless you set allow-incomplete-discovery: true.
  • When the pull request changes the lock file or another dependency, build or CI file diffcone recognises (see discovery), it runs every test.

Check against a full run (optional)

While you build trust in the selection, keep your existing full test job and compare: did diffcone select every test that failed? Have the full job write JUnit XML (--junitxml=full.xml) and upload it, then add:

  diffcone-check:
    needs: [full-tests, affected-tests]
    if: always()
    runs-on: ubuntu-latest
    steps:
      - uses: actions/download-artifact@v8
        with:
          name: full-junit
          path: full
      - uses: actions/download-artifact@v8
        with:
          name: diffcone-results
          path: diffcone-results
      - uses: bearing-research/diffcone/actions/check@v0.7.0
        with:
          full: full/full.xml

The check writes its result to the job summary. It checks the plan that ran: when the environment differed from the recording's, the plan from the code. A miss is a new failure of the full run that the plan didn't select, or that diffcone's own run didn't execute. It doesn't fail the workflow unless you set fail-on-miss: true, but an error in the check itself (an input it can't read) always fails the step. Tests that already failed in the nightly run are reported as already failing, not as misses. Without diffcone.xml in the results (the run never started pytest), the check compares the plan only, and the summary and a warning say so.

The check matches JUnit names to tests through pytest's rootdir and --junit-prefix, as the plan recorded them, and the action has no inputs to change them. So run the full job's pytest from the same directory, with the same --junit-prefix, as the run job. Otherwise, run diffcone check yourself with --rootdir or --junit-prefix (see checking a plan).

Or: record on every push to the default branch

If your default branch already runs the full suite on every push, that run can be the recording, and pull requests run only the selected tests. Each push then checks the plan for its own change against the full run, so a test diffcone should have selected fails the default branch's workflow.

.github/workflows/tests.yml
on:
  push:
    branches: [main]
  pull_request:

jobs:
  tests:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
      - uses: astral-sh/setup-uv@v10.2.0
      - run: uv sync --locked
      - if: github.event_name == 'push'
        id: record
        uses: bearing-research/diffcone/actions/record@v0.7.0
        with:
          key-prefix: diffcone-ubuntu
          command: uv run python -m pytest
          pytest-args: -n auto
          check: true
          fail-on-test-failure: true
      - if: github.event_name == 'pull_request'
        uses: bearing-research/diffcone/actions/run@v0.7.0
        with:
          key-prefix: diffcone-ubuntu
          command: uv run python -m pytest
          pytest-args: -n auto
      - uses: actions/upload-artifact@v7
        if: always()
        with:
          name: diffcone-ubuntu
          path: ${{ steps.record.outputs.results || 'diffcone-results' }}

On a push, record with check: true:

  1. restores the newest recording and plans the commit's change from it;
  2. runs the full suite under the recorder;
  3. compares the plan with the full run. A new failure the plan didn't select is run again: its whole test file, with the same pytest options, the files in the order the full run ran them, so a test that fails only after another test of its file fails again. If it fails again, it is a miss, and the job fails. If it passes the second time, it is reported as flaky. A test that fails only after a test in another file passes in the re-run, so it is reported as flaky too. Only a test can be run again: a file that failed to collect, or a failure that matches no target, is a miss straight away. Failures the recorded run already had are not misses. If diffcone check itself fails (an input it can't read), or the plan of the commit's change fails even without the recording, the job fails too;
  4. saves the new recording, together with the verdict. Re-running a job that found a miss finds it again.

The check is skipped, with a note, when there is nothing to compare with:

  • no recording yet;
  • a recording diffcone can't use: made with other source roots or pytest arguments or by an older version of diffcone, or for a commit that can't be fetched or isn't from before the pushed commit;
  • a commit with no parent.

The recording is still made and saved. Until a new one is saved, pull requests that restore an unusable recording plan from the code.

With fail-on-test-failure: true a failing test fails the job too, as a plain test run would, after the recording is saved. A test file that can't be collected stops the recording itself, which fails the job.

As with the nightly setup, only runs on the default branch can save the recording; pull requests only restore it.

Report on recent runs

Once every job runs through diffcone, a scheduled report job reads what the jobs uploaded and sums it up: for pull requests, the share of tests each job selected (leaving out the tests [[always_run]] names), how often it planned without a recording (and what differed) and the changed paths [build] reads set aside; for recordings, whether each pushed commit was checked, its new failures, flaky tests and misses. It adds the report as a comment on an issue labelled diffcone-report, and opens one issue, labelled diffcone-miss, for each test diffcone missed.

.github/workflows/diffcone-report.yml
name: diffcone report

on:
  schedule:
    - cron: "0 6 * * *"
  workflow_dispatch:

permissions:
  actions: read
  issues: write

jobs:
  report:
    runs-on: ubuntu-latest
    steps:
      - uses: bearing-research/diffcone/actions/report@v0.7.0
        with:
          workflow: tests.yml

The report reads the artifacts whose names start with diffcone-, so upload the results of run and record (as in the workflows above) under a name that starts with diffcone- and differs per job, and per cell of a matrix: diffcone-${{ matrix.os }}-py${{ matrix.python-version }}, for example. The report has one row per name; when a job was re-run, it reads the latest upload.

Each report covers the runs that finished since the previous report that posted something started. A run still going when a report starts goes in the next one. A report that only writes the job summary (both report-label and miss-label empty), or fails to download an artifact, leaves its runs to the next report; a failed download also fails the job. Each request that lists runs or downloads artifacts is tried three times. A report that can't find where the previous one started reads the last hours hours, says it is incomplete and fails the job, so the next report covers those runs again.

The report leaves out:

  • artifacts that have expired;
  • recording artifacts uploaded by pull-request runs (a pull request from a fork runs its own code);
  • artifacts whose files it can't read, which it counts as a problem.

report takes hours (the window for the first report: runs that completed in this many hours before it; default 24), workflow (only that workflow's runs; default all), artifact-prefix (default diffcone-), report-label and miss-label (set either to an empty string to skip that issue), and comment (always, the default, or on-problem: comment only when there is a problem; the job summary always has the report). The problems are:

  • a miss;
  • a check that failed to reach a verdict;
  • a pull-request run that ran none of its selected tests: its plan failed, diffcone run refused it (discovery may be incomplete and allow-incomplete-discovery is off, or the working tree differs from the plan's), or pytest was interrupted or could not start;
  • a pull-request run whose selected tests pytest did not collect, or whose command never loaded diffcone's selection plugin;
  • an incomplete report (an artifact it could not read, or a list of runs or artifacts it could not get from GitHub).

The report reads what run and record upload from the same release on, so give all the actions the same tag.

Each miss issue has a link that opens a new issue on diffcone's tracker with the test and the plan's reason filled in. Nothing is sent until you review it and submit it.

Report on another repository

repository reports on another repository's runs, and issues-repository posts somewhere else: a team can watch several projects from one place. A public repository's runs need no extra access; posting to another repository needs a token that can write its issues (issues-token, for example a fine-grained token saved as a secret). Issues posted elsewhere name the repository the runs came from, and each repository gets its own report issue.

      - uses: bearing-research/diffcone/actions/report@v0.7.0
        with:
          repository: my-org/my-project
          workflow: tests.yml
          comment: on-problem

To read the same report locally, download the artifacts of some runs into one directory per run and run diffcone report:

gh run download 123456 --pattern 'diffcone-*' --dir runs/123456
diffcone report --dir runs

How the cache is shared

Each recording is saved with actions/cache under its own key, <key-prefix>--<commit>-<run id>-<attempt>, and pull requests restore the newest one with their prefix (diffcone-ubuntu never restores a recording of diffcone-ubuntu-py312). If a recording's commit can no longer be fetched (after a force push), the run plans without it and says so. A shallow checkout gets the base it plans against: a base relative to HEAD (HEAD^1, HEAD~3) by deepening the checkout until it is there, a commit, branch or tag name by fetching it. GitHub lets pull requests, including those from forks, read caches saved on the default branch but never write them, so a pull request can't change the recording. The cache-mode settings above give each job only the access it needs.

The saved directory also holds diffcone's analysis cache, pruned to the recorded commit, so pull requests start planning without re-reading unchanged files.

Action inputs

record and run take:

Input Description
key-prefix Required. The name of the recording in the cache.
command Required. The pytest command, without arguments.
pytest-args Arguments for pytest, as one shell-quoted string.
source-roots Space-separated source roots (default .).
diffcone-ref Install diffcone from this git ref instead of the action's own version.

record also takes env-vars (environment variables your tests depend on, see execution evidence), junit (where to keep the recording run's JUnit XML; default .diffcone/baseline.xml), check (default false: check each push's change against its full run, as above) and fail-on-test-failure (default false). run also takes base (a commit, a branch or tag name, or HEAD^1; by default the event's own, see run on pull requests) and allow-incomplete-discovery (default false; see when the target list may be short).

check takes full (required: the full run's JUnit XML), results (the directory run wrote; default diffcone-results), others (more selective runs to compare, as NAME=JUNIT pairs), fail-on-miss (default false) and diffcone-ref. report also takes github-token (default the workflow's token, which reads the runs and posts the issues) and diffcone-ref.

Outputs: run sets selected (how many tests the plan that ran selected: the plan from the code when the environment differed from the recording's); record sets results (the directory to upload) and, with check, misses; report sets ok and report (the directory holding report.md and report.json).