pre-commit/action
Run your repo's pre-commit hooks in CI, with the hook environments cached.
What it does
pre-commit/action is a small composite action: it pip-installs pre-commit, restores a cache of hook environments, and runs pre-commit run --show-diff-on-failure --color=always --all-files.
It reuses the exact same hook configuration developers run locally, so CI and local checks cannot drift apart.
Usage
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.12'
- uses: pre-commit/action@v3.0.1Inputs
| Input | Description | Default | Required |
|---|---|---|---|
extra_args | Options to pass to pre-commit run. | --all-files | No |
Notes
The project publishes exact version tags (v3.0.1), not a floating major tag, reference the full version or a commit SHA.
The project describes this action as being in maintenance-only mode and recommends pre-commit.ci for richer behavior like auto-fixing PRs.
Run actions/setup-python first so the cache key (which includes the Python location) is stable and hooks get a predictable interpreter.
Common errors
- A failing run that ends with a diff is a hook that modified files (formatters like black or trailing-whitespace). Run
pre-commit run --all-fileslocally and commit the result. - A failure resolving a hook's Python version means the runner lacks that interpreter, add actions/setup-python with the version your hooks declare in
language_version.
Security and pinning
- Hooks execute arbitrary code from the repos pinned in .pre-commit-config.yaml, pin hook revs to tags or SHAs and review changes to that file like code.
Alternatives and related
Frequently asked questions
Should I use this action or pre-commit.ci?
How do I run hooks only on changed files in CI?
extra_args, e.g. --from-ref origin/main --to-ref HEAD, instead of the default --all-files. You need enough git history checked out for both refs.