Skip to main content

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​

  1. Install pre-commit locally and add a .pre-commit-config.yaml at the repository root.

  2. 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
  3. 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​

NameDescriptionDefaultType
------------
config-filePath to the pre-commit configuration file.pre-commit-config.yamlstring
needsThe jobs that this job depends on.[]array
optsOptions passed to pre-commit. Replaces the default; include --show-diff-on-failure in overrides to display changes.--all-files --show-diff-on-failurestring
rulesThe jobs that this job depends on.[{"if":"$CI_PIPELINE_SOURCE == \"schedule\"","when":"never"},{"when":"on_success"}]array
stageThe stage to run the job in.prestring
tagsThe runners that this job depends on.[]array