Skip to content

Continuous Integration

You can use the Sourcery CLI in your CI system as well.

As generally, the Sourcery CLI requires a login in the CI as well. We recommend to:

  • Register a separate user for the CI.
  • Store your Sourcery token as a secret in your CI environment.
  • Log in to the Sourcery CLI using that token

When running Sourcery in a CI system, you probably want to use the --check flag to return exit code 1 if any issues are detected.

Review Only The PR's Code

For the majority of projects, the best usage of Sourcery in the CI is to review only the code of the current PR.

You can use the script below to detect Sourcery violations that are present in the current code but not in the main branch:

pip install sourcery-cli
# Store your Sourcery token as a secret in your CI environment.
sourcery login --token $SOURCERY_TOKEN
sourcery review --check --diff "git diff main" .

Instead of main, you can pick another branch as well. For example, in GitHub Actions you can use --diff="git diff ${{ github.event.pull_request.base.sha }}"

Enforce Only A Few High Priority Rules

If you want a less strict check in the CI, you can enforce only a small subset of your rules using the --enable option.

For example, this script enforces only the import rules from the Google Python Style Guide in your PR:

pip install sourcery-cli
# Store your Sourcery token as a secret in your CI environment.
sourcery login --token $SOURCERY_TOKEN
sourcery review \
    --check \
    --diff "git diff main" \
    --enable no-wildcard-imports \
    --enable no-relative-imports \
    .

Review The Whole Repository

You can also review the whole repository in the CI as well:

pip install sourcery-cli
# Store your Sourcery token as a secret in your CI environment.
sourcery login --token $SOURCERY_TOKEN
sourcery review --check .