Using Sourcery in Your CI or as a Pre-commit Hook

You can use Sourcery in your CI or as a pre-commit hook to:

  • ensure that only code following your best practices gets deployed to production
  • set high quality standards for your pull requests without getting noise from issues in your legacy code
  • finetune your quality gates to ensure your most important best practices are always followed
  • review your code before you send it to a human reviewer

Sourcery CI

The recommended setup for a pre-commit hook is:


  - repo:
    rev: v1.21.0
      - id: sourcery
        # The best way to use Sourcery in a pre-commit hook:
        # \* review only changed lines:
        # \* omit the summary
        args: \[--diff=git diff HEAD, --no-summary\]

To use Sourcery in CI you can run a script like:

An example script to set up Sourcery in your CI to review only the code in your current PR:

pip install sourcery

Store your Sourcery token as a secret in your CI environment.

sourcery login --token $SOURCERY_TOKEN sourcery review --check --diff "git diff
main" .

For more details and configuration options, check out the Getting Started with CI & pre-commit guides.