Integrations | Ruff (original) (raw)

GitHub Actions

GitHub Actions has everything you need to run Ruff out-of-the-box:

[](#%5F%5Fcodelineno-0-1)name: CI [](#%5F%5Fcodelineno-0-2)on: push [](#%5F%5Fcodelineno-0-3)jobs: [](#%5F%5Fcodelineno-0-4) build: [](#%5F%5Fcodelineno-0-5) runs-on: ubuntu-latest [](#%5F%5Fcodelineno-0-6) steps: [](#%5F%5Fcodelineno-0-7) - uses: actions/checkout@v4 [](#%5F%5Fcodelineno-0-8) - name: Install Python [](#%5F%5Fcodelineno-0-9) uses: actions/setup-python@v5 [](#%5F%5Fcodelineno-0-10) with: [](#%5F%5Fcodelineno-0-11) python-version: "3.11" [](#%5F%5Fcodelineno-0-12) - name: Install dependencies [](#%5F%5Fcodelineno-0-13) run: | [](#%5F%5Fcodelineno-0-14) python -m pip install --upgrade pip [](#%5F%5Fcodelineno-0-15) pip install ruff [](#%5F%5Fcodelineno-0-16) # Update output format to enable automatic inline annotations. [](#%5F%5Fcodelineno-0-17) - name: Run Ruff [](#%5F%5Fcodelineno-0-18) run: ruff check --output-format=github .

Ruff can also be used as a GitHub Action via ruff-action.

By default, ruff-action runs as a pass-fail test to ensure that a given repository doesn't contain any lint rule violations as per its configuration. However, under-the-hood, ruff-action installs and runs ruff directly, so it can be used to execute any supported ruff command (e.g., ruff check --fix).

ruff-action supports all GitHub-hosted runners, and can be used with any published Ruff version (i.e., any version available on PyPI).

To use ruff-action, create a file (e.g., .github/workflows/ruff.yml) inside your repository with:

[](#%5F%5Fcodelineno-1-1)name: Ruff [](#%5F%5Fcodelineno-1-2)on: [ push, pull_request ] [](#%5F%5Fcodelineno-1-3)jobs: [](#%5F%5Fcodelineno-1-4) ruff: [](#%5F%5Fcodelineno-1-5) runs-on: ubuntu-latest [](#%5F%5Fcodelineno-1-6) steps: [](#%5F%5Fcodelineno-1-7) - uses: actions/checkout@v4 [](#%5F%5Fcodelineno-1-8) - uses: astral-sh/ruff-action@v3

Alternatively, you can include ruff-action as a step in any other workflow file:

[](#%5F%5Fcodelineno-2-1) - uses: astral-sh/ruff-action@v3

ruff-action accepts optional configuration parameters via with:, including:

For example, to run ruff check --select B ./src using Ruff version 0.8.0:

[](#%5F%5Fcodelineno-3-1)- uses: astral-sh/ruff-action@v3 [](#%5F%5Fcodelineno-3-2) with: [](#%5F%5Fcodelineno-3-3) version: 0.8.0 [](#%5F%5Fcodelineno-3-4) args: check --select B [](#%5F%5Fcodelineno-3-5) src: "./src"

GitLab CI/CD

You can add the following configuration to .gitlab-ci.yml to run a ruff format in parallel with a ruff check compatible with GitLab's codequality report.

[](#%5F%5Fcodelineno-4-1).base_ruff: [](#%5F%5Fcodelineno-4-2) stage: build [](#%5F%5Fcodelineno-4-3) interruptible: true [](#%5F%5Fcodelineno-4-4) image: [](#%5F%5Fcodelineno-4-5) name: ghcr.io/astral-sh/ruff:0.11.8-alpine [](#%5F%5Fcodelineno-4-6) before_script: [](#%5F%5Fcodelineno-4-7) - cd $CI_PROJECT_DIR [](#%5F%5Fcodelineno-4-8) - ruff --version [](#%5F%5Fcodelineno-4-9) [](#%5F%5Fcodelineno-4-10)Ruff Check: [](#%5F%5Fcodelineno-4-11) extends: .base_ruff [](#%5F%5Fcodelineno-4-12) script: [](#%5F%5Fcodelineno-4-13) - ruff check --output-format=gitlab > code-quality-report.json [](#%5F%5Fcodelineno-4-14) artifacts: [](#%5F%5Fcodelineno-4-15) reports: [](#%5F%5Fcodelineno-4-16) codequality: $CI_PROJECT_DIR/code-quality-report.json [](#%5F%5Fcodelineno-4-17) [](#%5F%5Fcodelineno-4-18)Ruff Format: [](#%5F%5Fcodelineno-4-19) extends: .base_ruff [](#%5F%5Fcodelineno-4-20) script: [](#%5F%5Fcodelineno-4-21) - ruff format --diff

pre-commit

Ruff can be used as a pre-commit hook via ruff-pre-commit:

[](#%5F%5Fcodelineno-5-1)- repo: https://github.com/astral-sh/ruff-pre-commit [](#%5F%5Fcodelineno-5-2) # Ruff version. [](#%5F%5Fcodelineno-5-3) rev: v0.11.8 [](#%5F%5Fcodelineno-5-4) hooks: [](#%5F%5Fcodelineno-5-5) # Run the linter. [](#%5F%5Fcodelineno-5-6) - id: ruff [](#%5F%5Fcodelineno-5-7) # Run the formatter. [](#%5F%5Fcodelineno-5-8) - id: ruff-format

To enable lint fixes, add the --fix argument to the lint hook:

[](#%5F%5Fcodelineno-6-1)- repo: https://github.com/astral-sh/ruff-pre-commit [](#%5F%5Fcodelineno-6-2) # Ruff version. [](#%5F%5Fcodelineno-6-3) rev: v0.11.8 [](#%5F%5Fcodelineno-6-4) hooks: [](#%5F%5Fcodelineno-6-5) # Run the linter. [](#%5F%5Fcodelineno-6-6) - id: ruff [](#%5F%5Fcodelineno-6-7) args: [ --fix ] [](#%5F%5Fcodelineno-6-8) # Run the formatter. [](#%5F%5Fcodelineno-6-9) - id: ruff-format

To avoid running on Jupyter Notebooks, remove jupyter from the list of allowed filetypes:

[](#%5F%5Fcodelineno-7-1)- repo: https://github.com/astral-sh/ruff-pre-commit [](#%5F%5Fcodelineno-7-2) # Ruff version. [](#%5F%5Fcodelineno-7-3) rev: v0.11.8 [](#%5F%5Fcodelineno-7-4) hooks: [](#%5F%5Fcodelineno-7-5) # Run the linter. [](#%5F%5Fcodelineno-7-6) - id: ruff [](#%5F%5Fcodelineno-7-7) types_or: [ python, pyi ] [](#%5F%5Fcodelineno-7-8) args: [ --fix ] [](#%5F%5Fcodelineno-7-9) # Run the formatter. [](#%5F%5Fcodelineno-7-10) - id: ruff-format [](#%5F%5Fcodelineno-7-11) types_or: [ python, pyi ]

When running with --fix, Ruff's lint hook should be placed before Ruff's formatter hook, and_before_ Black, isort, and other formatting tools, as Ruff's fix behavior can output code changes that require reformatting.

When running without --fix, Ruff's formatter hook can be placed before or after Ruff's lint hook.

(As long as your Ruff configuration avoids any linter-formatter incompatibilities,ruff format should never introduce new lint errors, so it's safe to run Ruff's format hook after ruff check --fix.)

mdformat

mdformat is capable of formatting code blocks within Markdown. The mdformat-ruffplugin enables mdformat to format Python code blocks with Ruff.

Docker

Ruff provides a distroless Docker image including the ruff binary. The following tags are published:

In addition, ruff publishes the following images:

As with the distroless image, each image is published with ruff version tags asruff:{major}.{minor}.{patch}-{base} and ruff:{major}.{minor}-{base}, e.g., ruff:0.6.6-alpine.