pre-commit Component
Run the same pre-commit hooks locally and in your GitLab pipeline. The component checks all tracked files by default and prints a diff when a hook changes a file.
Run hooks in a pipeline
-
Install pre-commit locally and add a
.pre-commit-config.yamlat the repository root. -
Add the component to
.gitlab-ci.yml:include:- component: $CI_SERVER_FQDN/xrow-public/ci-tools/common@stable- component: $CI_SERVER_FQDN/xrow-public/ci-tools/pre-commit@stable -
Check the hooks locally before pushing:
pre-commit run --all-files --show-diff-on-failure
The pipeline uses --all-files --show-diff-on-failure when opts is omitted. If a hook rewrites a tracked file, pre-commit exits with a failure and shows the changes in the job log. This can happen even when the rewritten file has no remaining lint errors. Apply the fix locally and commit it; the component does not commit or push changes.
Override the options
Set opts on the component to replace the entire default option string. For example, to check one file and still show a hook's changes:
include:
- component: $CI_SERVER_FQDN/xrow-public/ci-tools/common@stable
- component: $CI_SERVER_FQDN/xrow-public/ci-tools/pre-commit@stable
inputs:
opts: '--files path/to/file --show-diff-on-failure'
A custom opts value does not inherit --all-files or --show-diff-on-failure. Include --show-diff-on-failure explicitly if you want diff output with an override.
Inputs
| Name | Description | Default | Type |
| --- | --- | --- | --- |
| config-file | Path to the pre-commit configuration file | .pre-commit-config.yaml | string |
| needs | The jobs that this job depends on. | [] | array |
| opts | Options passed to pre-commit. Replaces the default; include --show-diff-on-failure in overrides to display changes. | --all-files --show-diff-on-failure | string |
| rules | The jobs that this job depends on. | [{"if":"$CI_PIPELINE_SOURCE == \"schedule\"","when":"never"},{"when":"on_success"}] | array |
| stage | The stage to run the job in | .pre | string |
| tags | The runners that this job depends on. | [] | array |