A CI pipeline that runs on every push but does not block a single merge is theatre. The point of this baseline is the opposite: a small, honest GitHub Actions workflow that lints, tests and reports coverage, wired to branch protection so a red run actually stops the pull request.
Most Python CI I inherit has the same two faults. It is copied from a tutorial that pins nothing and trusts everything, and the checks are advisory — they go green or red beside the merge button, and nobody has made red mean no. What follows is a workflow you can drop into any Python repository, plus the settings that give it teeth. The stack is uv for environment management, ruff for linting and formatting, and pytest for tests. No Makefile, no bespoke scripts, nothing you have to maintain separately from the repo.
The workflow, top to bottom
Put this in .github/workflows/ci.yml. Everything below the code explains why each block is there.
Free · 4 minutes
Is your engineering team shipping safely, or quietly accumulating risk?
Fourteen questions on how work gets from idea to production — cadence, testing, rollback, and the key-person risk in your delivery. Banded finding on screen, full sheet by email.
name: CI
on:
push:
branches: [main]
pull_request:
# Least privilege: the token can read the repo and nothing else.
permissions:
contents: read
# Cancel an in-flight run when a newer commit lands on the same ref.
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: ["3.11", "3.12", "3.13"]
steps:
- name: Check out the repository
uses: actions/checkout@v5
- name: Install uv and the toolchain
uses: astral-sh/setup-uv@v8
with:
enable-cache: true
python-version: ${{ matrix.python-version }}
- name: Install dependencies from the lockfile
run: uv sync --locked --dev
- name: Lint and format check (ruff)
run: |
uv run ruff check .
uv run ruff format --check .
- name: Run tests with coverage
run: uv run pytest --cov --cov-report=xml --cov-report=term-missing
- name: Upload the coverage report
if: matrix.python-version == '3.13'
uses: actions/upload-artifact@v4
with:
name: coverage-xml
path: coverage.xml
if-no-files-found: error
That is the whole thing. It runs three Python versions in parallel, installs exactly what the lockfile says, lints, tests, and keeps one coverage artefact. On a small repository it finishes in well under a minute after the first cache warm-up.
Triggers, least privilege and killing stale runs
The on block runs the workflow on pushes to main and on every pull request. That pull-request trigger is the one that matters for governance: it is what produces the check a reviewer sees before merging. Pushing directly to main should be rare once branch protection is on, but keeping the push trigger means the default branch is always verified.
The permissions block is the part most templates omit, and its absence is a real exposure. Without it, the automatically-provided GITHUB_TOKEN inherits whatever the repository or organisation default is, which is often write access to contents. A test job does not need to write anything. Setting contents: read at the top of the file makes every job start from read-only and add back only what it needs. When a later job genuinely needs to publish — a package, a deployment, an OIDC token — you grant that scope on that job alone. The same discipline underpins keyless deploys using federated credentials, where the whole point is that the workflow holds no standing secret and asks for a short-lived token scoped to exactly one job.
The concurrency block cancels an older run when you push a fixup two minutes later. Group by workflow and ref, set cancel-in-progress: true, and you stop paying for runs whose result no longer matters. On a busy repository this is the single biggest reduction in wasted minutes you can make in three lines.
uv, ruff and pytest doing the actual work
The astral-sh/setup-uv action installs uv and, with python-version, provisions the interpreter for each matrix leg — so you do not need a separate setup-python step. Setting enable-cache: true persists uv’s cache across runs, which is where most of the speed comes from; if you want to go further on that, see the piece on dependency caching in GitHub Actions that actually hits.
uv sync --locked --dev is deliberate. The --locked flag tells uv to fail if uv.lock is out of date rather than silently resolving new versions, so CI installs precisely what a developer committed. The --dev flag pulls in the development dependency group — where ruff, pytest and pytest-cov live. If your lockfile drifts, this step goes red before a single test runs, which is exactly what you want: a reproducibility failure is a failure. uv itself is worth pinning too, and the same reasoning applies to installing tools locally, covered in installing Python CLI tools the sane way.
Linting is two commands, not one. ruff check . runs the linter; ruff format --check . asserts the code is already formatted and fails if it is not, without rewriting anything. Running the formatter in check mode in CI, and letting developers run ruff format locally, keeps formatting arguments out of pull requests entirely.
pytest --cov requires pytest-cov in your dev group. It writes coverage.xml for tooling and prints term-missing so the uncovered lines show in the log. The upload step is gated to a single matrix leg with an if condition, because uploading three identical artefacts with the same name would collide. If you want to enforce a floor, add --cov-fail-under=80 to the pytest command and the job fails below the threshold.
Pinning actions so a push upstream can’t rewrite your CI
The workflow above references actions by major-version tag — @v5, @v8, @v4 — which is readable but mutable. A tag can be moved to point at new code, which means the thing running inside your pipeline, with your token, can change without a commit in your repository. For anything touching regulated software, pin to a full commit SHA and record the human-readable version in a comment:
- uses: actions/checkout@<full-40-char-sha> # v5.0.0
You do not resolve those SHAs by hand. Let Dependabot’s github-actions ecosystem, or a tool such as pinact, pin and then bump them for you — you get a reviewable pull request every time an upstream action changes, instead of a silent swap. Astral now publishes immutable, fully-specified version tags for setup-uv from v8 onward, which is the same instinct expressed by the maintainer; SHA pinning gives you that guarantee for every action you use.
Making the checks actually block a merge
A green tick is not a gate. Until you require the check, a maintainer can merge over a red run, and eventually someone will. Turn the workflow into a rule with branch protection on main. From the command line, using the GitHub CLI:
gh api -X PUT repos/OWNER/REPO/branches/main/protection --input - <<'JSON'
{
"required_status_checks": {
"strict": true,
"checks": [
{ "context": "test (3.11)" },
{ "context": "test (3.12)" },
{ "context": "test (3.13)" }
]
},
"enforce_admins": true,
"required_pull_request_reviews": { "required_approving_review_count": 1 },
"restrictions": null
}
JSON
Three things there earn their place. The context names are test (3.11) and so on — a matrix job reports one check per leg, named after the job and the parameter, and the names must match exactly or the rule silently requires nothing. strict: true means a branch must be up to date with main before it can merge, so a check cannot pass against a stale base. enforce_admins: true is the one people flinch at and the one that matters: without it, the rule binds everyone except the people most able to break main. If you would rather manage this as code across many repositories, GitHub repository rulesets express the same controls and version well.
That is a defensible baseline: every change verified on three interpreters, dependencies reproduced from a lockfile, actions pinned, and a merge that cannot happen while the pipeline is red. When you find yourself pasting this file into a fifth repository, that is the signal to extract it — the move from copied YAML to shared pipeline is covered in reusable workflows and composite actions. Start here first; a baseline you actually understand beats a clever one you inherited.
Free interactive tool
Website compliance checklist
What your site has to do, based on what it actually does
Answer as much or as little as you like — the list builds as you go. Nothing is stored against your name and no email is required.
Everything that applies
Ordered by what to do first: legal requirements you can close quickly, then larger pieces of work, then what is expected rather than required. Not exhaustive, and not a legal audit.
Dated PDF, yours to keep or circulate.
Most technology problems are not technology problems. They are control problems.
The systems exist. The investment has been made. The question is whether leadership can understand, direct, evidence, and sustain what those systems produce. Find out where control exists — and where it only appears to.