Sub topic 13
Running Ruff in pre-commit and CI
This chapter assumes you can run ruff check and ruff format, and that Ruff is configured in pyproject.toml. It also assumes you know basic Git and can read a YAML file. Here you will run the same checks automatically on every commit and on every pull request.
Why two layers
Pre-commit and CI do different jobs.
- A pre-commit hook runs on the developer's machine before a commit is created. It gives fast feedback and can fix files in place.
- A CI job runs on a server for every push or pull request. It cannot be skipped with
git commit --no-verify, so it is the enforcement point.
Use both. The hook saves time, and CI guarantees that nothing unchecked reaches the main branch.
Setting up pre-commit
The pre-commit framework reads .pre-commit-config.yaml from the repository root. Ruff publishes an official hook repository.
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.14.0
hooks:
- id: ruff-check
args: [--fix]
- id: ruff-formatInstall the framework and register the hook once per clone:
pip install pre-commit
pre-commit install
pre-commit run --all-filesThe last command runs every hook against the whole repository. Run it once after adding the config, so existing files are checked too.
Three details matter here:
- Order. Put
ruff-checkbeforeruff-format. Fixes such as import sorting can change layout, and the formatter should have the last word. - Pinned version.
revfixes the Ruff version used by the hook. Pin it to a release you have tested, and update it deliberately withpre-commit autoupdate. - Changed files. When a hook modifies a file, the commit fails. Review the changes, stage them with
git add, and commit again.
Running Ruff in CI
In CI, do not modify files. Only check them. The commands below exit with a non-zero status when they find a problem, and that status fails the job.
name: lint
on:
push:
branches: [main]
pull_request:
jobs:
ruff:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"
- run: pip install ruff==0.14.0
- run: ruff check --output-format=github .
- run: ruff format --check .Notes on this job:
--output-format=githubmakes violations appear as annotations on the pull request diff.ruff format --checkreports files that would change and exits with status 1, but it does not rewrite them.- Installing a fixed version, as in
ruff==0.14.0, keeps results reproducible. Without a pin, a new Ruff release can add rules or change formatting and break an unrelated pull request. - Do not use
--fixin CI. A fix applied on the runner is lost when the job ends, and the job can pass without the repository being corrected.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Hook passes locally but CI fails | Different Ruff versions | Pin the same version in `rev` and in CI |
| Commit fails, then succeeds on retry | A hook rewrote files | Stage the changes and commit again |
| CI reports formatting errors only | Hooks not installed locally | Run `pre-commit install` |
| Hook ignores your settings | Config not found from the file's directory | Keep `pyproject.toml` at the repository root |
Exercise
Why should the CI job use ruff format --check instead of ruff format, and what happens to the job when a file is not formatted?
Show answer
ruff format --check only reports files that would change, so the job never edits the checkout. Plain ruff format would rewrite files on the runner, and those changes would be discarded when the job ends. When a file is not formatted, the command exits with status 1 and the job fails.
Exercise
In .pre-commit-config.yaml, which hook should come first, ruff-check with --fix or ruff-format, and why?
Show answer
ruff-check should come first. Its fixes, such as sorting imports, can change how the code is laid out. Running ruff-format afterward makes sure the final file is formatted.