Skip to content

Continuous Integration

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

The Sourcery CLI requires a login in the CI for non open source projects (open source projects can skip this setup step). 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
# 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.

  1. Define a tag. For example: ci-checks

  2. Use the --enable option to review your changes only with the rules in ci-checks.

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" \
    --enable ci-checks \
    .

Review The Whole Repository

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

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