blob: 54fbbcef463d32e2e727a2229d30570708e38010 [file]
name: Build and test
on:
push:
branches: [main]
pull_request:
branches: [main]
workflow_dispatch:
permissions:
contents: read
env:
# The one version the quality job runs on. Not "the latest": what the ratchet measures is a
# property of the code rather than of the interpreter, so it wants one version pinned rather
# than the newest one tracked.
QUALITY_PY_VERSION: '3.14'
# Every job runs on a GitHub-hosted runner, on an image named outright: `ubuntu-24.04` rather than
# `ubuntu-latest`, so that the image moving under a job is a change to this file and not a red run
# nobody caused. A job that asks for the labels of the ASF's self-hosted runners waits for one, and
# here none came, so every such job was cancelled unstarted. A hosted runner has four cores, which is what
# `-n auto` and BEHAVIOUR_WORKERS below are sized to.
jobs:
test:
runs-on: ubuntu-24.04
timeout-minutes: 25
strategy:
fail-fast: false
matrix:
python-version:
- '3.14'
steps:
# The checkout comes first now: what installs the packages is a script in this
# repository, and this is the step that puts it there. A checkout needs neither package.
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
# Two commits, so that the merge commit a pull request is built as can name the
# parent it would merge into. That commit is what the ratchet compares against.
fetch-depth: 2
- uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0
with:
python-version: ${{ matrix.python-version }}
- name: Install uv
uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3
with:
version-file: .tool-versions
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: uv sync --frozen --no-dev
# `-n auto` is one worker per core. Each pays its own setup before it runs anything,
# which is the reason `make test` caps the count on a larger machine; a runner is
# not large enough for that to matter.
# Coverage is measured on one matrix version rather than three: the number is a
# property of the tests, not of the interpreter, and instrumenting all three would
# pay for it twice over for an answer that does not differ.
- name: Run tests
run: |
uv run pytest -rs -n auto ${{ matrix.python-version == '3.14' && '--cov=app --cov-report=json' || '' }}
# The base this branch is measured against: the readings the run on main published
# for the commit it would merge into. For a pull request that is the first parent of
# the merge commit the checkout produces. Anything that is not a push to main is
# checked, so that a `workflow run` on a branch is measured too - a stacked pull
# request gets no run of its own. See docs/quality-ratchet.md.
- name: Work out the base commit
if: matrix.python-version == '3.14'
id: base
run: echo "sha=$(git rev-parse HEAD^1)" >> "$GITHUB_OUTPUT"
- name: Fetch the base coverage
id: base-cache
if: matrix.python-version == '3.14' && github.event_name != 'push'
uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
with:
path: state/quality-readings.json
key: quality-python-coverage-${{ steps.base.outputs.sha }}
# The cache is a rendezvous, not the source of the answer: on a miss we measure the
# base ourselves rather than skipping the check. For coverage that means running the
# suite against it, which is why this is worth caching in the first place.
# continue-on-error because a base we cannot measure is not the branch's fault: the
# readings file then stays absent and the check reports the meter as unmeasured.
- name: Measure the base coverage, when the cache had none
if: matrix.python-version == '3.14' && github.event_name != 'push' && steps.base-cache.outputs.cache-hit != 'true'
continue-on-error: true
run: |
git worktree add --detach ../base "${{ steps.base.outputs.sha }}"
(cd ../base && uv run --frozen pytest -q -n auto --cov=app --cov-report=json)
uv run scripts/ci/quality.py readings --tree ../base \
--out state/quality-readings.json --meter python-coverage --require-coverage
git worktree remove --force ../base
- name: Check the coverage floor
if: matrix.python-version == '3.14' && github.event_name != 'push'
run: |
uv run scripts/ci/quality.py check --base state/quality-readings.json \
--meter python-coverage --require-coverage
# main publishes rather than checks: what a merge does to the numbers was decided on
# the pull request, against this same reading. Checking again here would only be
# able to fail after the fact, on a commit nobody can now change.
- name: Publish the coverage for branches to measure against
if: matrix.python-version == '3.14' && github.event_name == 'push'
run: |
uv run scripts/ci/quality.py readings --out state/quality-readings.json \
--meter python-coverage
- name: Save it for them
if: matrix.python-version == '3.14' && github.event_name == 'push'
uses: actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
with:
path: state/quality-readings.json
key: quality-python-coverage-${{ github.sha }}
# What the app answers - every GET route as each reader, and what a fixed sequence of writes
# changes - recorded with the commit a pull request would merge into and with the pull request,
# and compared file by file (scripts/ci/behaviour.py). Nothing is committed. As with the quality
# ratchet, the base comes from the run on main that produced that commit, through the actions
# cache, and is recorded here on a miss. A difference fails unless a commit the branch adds
# carries a `Behaviour-Allow:` trailer naming it. One interpreter rather than the matrix: the
# record is of the app, and both sides are recorded alike.
behaviour:
runs-on: ubuntu-24.04
timeout-minutes: 30
env:
# a runner to itself: one part at a time per core, of the four a hosted runner has
BEHAVIOUR_WORKERS: 4
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
# The whole history: the base is `HEAD^1`, and the trailers on the commits between it and
# here are read, which a shallow clone cannot walk.
fetch-depth: 0
- uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0
with:
python-version-file: .python-version
- name: Install uv
uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3
with:
version-file: .tool-versions
- name: Install dependencies
run: uv sync --frozen --no-dev
- name: Work out the base commit
id: base
run: echo "sha=$(git rev-parse HEAD^1)" >> "$GITHUB_OUTPUT"
- name: Fetch the base record
id: base-cache
if: github.event_name != 'push'
uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
with:
path: |
state/behaviour-base
state/behaviour-base.behaviour-record.json
state/behaviour-base.executed.json
key: behaviour-record-${{ steps.base.outputs.sha }}
- name: Record the base, when the cache had none
if: github.event_name != 'push' && steps.base-cache.outputs.cache-hit != 'true'
run: uv run scripts/ci/behaviour.py base --ref "${{ steps.base.outputs.sha }}"
- name: Record this change, and compare
id: compare
if: github.event_name != 'push'
run: uv run scripts/ci/behaviour.py check --ref "${{ steps.base.outputs.sha }}"
# Whatever the verdict: a difference that fails the check is the one most worth reading.
- name: Keep the difference for the reviewer
if: ${{ !cancelled() && steps.compare.outcome != 'skipped' }}
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: behaviour
path: state/behaviour.diff
if-no-files-found: ignore
- name: Record main, for the branches to compare against
if: github.event_name == 'push'
run: uv run scripts/ci/behaviour.py record --out state/behaviour-base
- name: Save it for them
if: github.event_name == 'push'
uses: actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
with:
path: |
state/behaviour-base
state/behaviour-base.behaviour-record.json
state/behaviour-base.executed.json
key: behaviour-record-${{ github.sha }}
frontend:
runs-on: ubuntu-24.04
timeout-minutes: 15
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
fetch-depth: 2
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
with:
node-version: '22'
- name: Enable corepack
run: corepack enable
- name: Install dependencies
working-directory: assets
run: yarn install --immutable
# svelte-check and eslint. These ran nowhere in CI before, so a branch
# could go red only on somebody's machine.
- name: Lint
working-directory: assets
run: yarn lint
- name: Run unit tests
working-directory: assets
run: yarn test --coverage
- name: Work out the base commit
id: base
run: echo "sha=$(git rev-parse HEAD^1)" >> "$GITHUB_OUTPUT"
- name: Fetch the base coverage
id: base-cache
if: github.event_name != 'push'
uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
with:
path: state/quality-readings.json
key: quality-frontend-coverage-${{ steps.base.outputs.sha }}
# As in the test job: a miss measures the base rather than skipping it. vitest is
# run from this checkout's node_modules, linked in, so the base is read by the same
# toolchain rather than by an install of its own.
- name: Measure the base coverage, when the cache had none
if: github.event_name != 'push' && steps.base-cache.outputs.cache-hit != 'true'
continue-on-error: true
run: |
git worktree add --detach ../base "${{ steps.base.outputs.sha }}"
ln -s "$PWD/assets/node_modules" ../base/assets/node_modules
(cd ../base/assets && node_modules/.bin/vitest run --coverage)
python3 scripts/ci/quality.py readings --tree ../base \
--out state/quality-readings.json --meter frontend-coverage --require-coverage
git worktree remove --force ../base
- name: Check the coverage floor
if: github.event_name != 'push'
run: |
python3 scripts/ci/quality.py check --base state/quality-readings.json \
--meter frontend-coverage --require-coverage
- name: Publish the coverage for branches to measure against
if: github.event_name == 'push'
run: |
python3 scripts/ci/quality.py readings --out state/quality-readings.json \
--meter frontend-coverage --require-coverage
- name: Save it for them
if: github.event_name == 'push'
uses: actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
with:
path: state/quality-readings.json
key: quality-frontend-coverage-${{ github.sha }}
# The ratchet: complexity, dead code and duplication, each compared against the same
# meters read on the commit this branch would merge into. Nothing here has to be fixed
# to merge - the numbers just may not climb. docs/quality-ratchet.md says what to do
# when it fails.
#
# The two coverage meters are absent by design: they need a suite to have run, so they
# live in the jobs above that run one. This job skips them for want of a report.
quality:
runs-on: ubuntu-24.04
timeout-minutes: 20
steps:
# The checkout comes first now: what installs the packages is a script in this
# repository, and this is the step that puts it there. A checkout needs neither package.
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
# The whole history, where the other jobs take two commits. This one reads the
# `Quality-Allow:` trailers on the commits between the base and here, and a
# shallow clone cannot walk that range - it would find none and report a
# regression the branch had already argued for. Depth 2 is enough for `HEAD^1`
# and nothing else.
fetch-depth: 0
- uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0
with:
python-version: ${{ env.QUALITY_PY_VERSION }}
- name: Install uv
uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3
with:
version-file: .tool-versions
python-version: ${{ env.QUALITY_PY_VERSION }}
- name: Install Python dependencies
run: uv sync --frozen --all-groups
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
with:
node-version: '22'
- name: Install frontend dependencies
working-directory: assets
run: |
corepack enable
yarn install --immutable
- name: Work out the base commit
id: base
run: echo "sha=$(git rev-parse HEAD^1)" >> "$GITHUB_OUTPUT"
- name: Fetch the base readings
id: base-cache
if: github.event_name != 'push'
uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
with:
path: state/quality-readings.json
key: quality-counts-${{ steps.base.outputs.sha }}
# These seven read source rather than a report, so measuring the base is a worktree
# and a second pass of the linters - half a minute, against the cost of not checking
# at all. `--ref` naming an ancestor of HEAD resolves to that commit itself.
- name: Measure the base readings, when the cache had none
if: github.event_name != 'push' && steps.base-cache.outputs.cache-hit != 'true'
continue-on-error: true
run: |
uv run scripts/ci/quality.py base --ref "${{ steps.base.outputs.sha }}" \
--out state/quality-readings.json
- name: Check the quality ratchet
if: github.event_name != 'push'
run: uv run scripts/ci/quality.py check --base state/quality-readings.json
- name: Publish the readings for branches to measure against
if: github.event_name == 'push'
run: uv run scripts/ci/quality.py readings --out state/quality-readings.json
- name: Save them for it
if: github.event_name == 'push'
uses: actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
with:
path: state/quality-readings.json
key: quality-counts-${{ github.sha }}
e2e:
runs-on: ubuntu-24.04
timeout-minutes: 30
steps:
# The checkout comes first now: what installs the packages is a script in this
# repository, and this is the step that puts it there. A checkout needs neither package.
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0
with:
python-version-file: .python-version
- name: Install uv
uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3
with:
version-file: .tool-versions
- name: Install Python dependencies
run: uv sync --frozen --no-dev
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
with:
node-version: '22'
- name: Build frontend assets
working-directory: assets
run: |
corepack enable
yarn install --immutable
yarn build
- name: Install Playwright
working-directory: e2e
run: npm ci
# Between the two halves of installing Playwright, which is the earliest it can run:
# it needs `npm ci` for the compiler and the types, and nothing after that. Six
# seconds against the browser's twenty and the suite's ten minutes, so a spec that
# does not type-check is answered before either is spent.
#
# Playwright transpiles a spec rather than type-checking it, so until this step
# existed nothing checked these files at all: a spec whose types are wrong runs
# anyway, and passes for as long as the wrongness does not happen to throw.
#
# What it does not catch is a deliberate `as unknown as` - a double assertion beats
# any checker, which is why the one on #729 needed a reviewer. What it does catch is
# the far commoner way that goes wrong: a name misspelt in a typed position, an
# untyped parameter that silently unchecks every call on it, an index that might be
# undefined, and a binding nothing reads. See e2e/README.md.
- name: Type-check the end-to-end specs
working-directory: e2e
run: npm run typecheck
- name: Install the browser
working-directory: e2e
run: npx playwright install --with-deps chromium
- name: Run Playwright end-to-end tests
working-directory: e2e
run: npx playwright test
# CONTRIBUTING.md asks for screenshots on a user-facing UI change, and the specs
# already capture them - they were then thrown away with the runner. Keeping them
# means a reviewer can see the change without checking the branch out, and that the
# author has something to attach that is current with the branch rather than taken
# by hand. `always()` because a failing run is when they are worth most.
- name: Keep the screenshots the suite captured
if: always()
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: e2e-screenshots
path: e2e/screenshots/
if-no-files-found: ignore
retention-days: 14
# What a deployment installs is `uv.lock`, without the dependency groups.
# The jobs above install the `test` group as well, so a package that only that group declares is
# present in every one of them and absent in production, where the server fails to start on its
# first import. This job installs the lock file without groups and imports the application from it.
production-install:
runs-on: ubuntu-24.04
timeout-minutes: 10
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0
with:
python-version-file: .python-version
- name: Install uv
uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3
with:
version-file: .tool-versions
# `--frozen`: install what uv.lock says and resolve nothing. `--no-default-groups`: what production installs.
- name: Install uv.lock without groups
run: UV_PROJECT_ENVIRONMENT="$RUNNER_TEMP/production" uv sync --frozen --no-default-groups
- name: Import the application
run: '"$RUNNER_TEMP/production/bin/python" -I scripts/ci/production_import.py'