How to Run a pre-commit Check Script in GitHub Actions
Running pre-commit run --all-files in CI enforces the same hooks locally and in the pipeline, so nothing slips through unhooked machines.
Install pre-commit, then run it against every file. The hooks are defined once in .pre-commit-config.yaml, so CI and local checks cannot drift apart.
Steps
- Keep hook definitions in
.pre-commit-config.yaml. - Set up Python and
pip install pre-commit. - Run
pre-commit run --all-filesas the gate.
Workflow
.github/workflows/ci.yml
jobs:
checks:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.12'
- run: pip install pre-commit
- run: pre-commit run --all-filesGotchas
- Cache
~/.cache/pre-commitkeyed on the config hash so hook environments are not rebuilt every run. - Some hooks reformat files; in CI they fail the job to signal the change rather than committing it.
Related guides
How to Lint Shell Scripts With ShellCheck in GitHub ActionsCatch shell script bugs in GitHub Actions with ShellCheck, which is preinstalled on Ubuntu runners, failing t…
How to Factor Common Steps Into a Composite Action in GitHub ActionsMove repeated setup steps into a local composite action in GitHub Actions with runs.using composite, then cal…