集成
GitHub Actions
GitHub Actions 具备开箱即用地运行 Ruff 的所有必要条件
name: CI
on: push
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Python
uses: actions/setup-python@v5
with:
python-version: "3.11"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install ruff
# Update output format to enable automatic inline annotations.
- name: Run Ruff
run: ruff check --output-format=github .
Ruff 也可以通过 ruff-action 作为 GitHub Action 使用。
默认情况下,ruff-action 作为通过/失败测试运行,以确保给定的存储库不包含任何违反 lint 规则的内容,如其配置所示。但是,在底层,ruff-action 直接安装并运行 ruff,因此它可以用于执行任何受支持的 ruff 命令(例如,ruff check --fix)。
ruff-action 支持所有 GitHub 托管的 runners,并且可以与任何已发布的 Ruff 版本(即,PyPI 上提供的任何版本)一起使用。
要使用 ruff-action,请在您的存储库中创建一个文件(例如,.github/workflows/ruff.yml),其中包含
name: Ruff
on: [ push, pull_request ]
jobs:
ruff:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: astral-sh/ruff-action@v3
或者,您可以将 ruff-action 作为步骤包含在任何其他 workflow 文件中
ruff-action 通过 with: 接受可选的配置参数,包括
version: 要安装的 Ruff 版本(默认值:latest)。args: 传递给 Ruff 的命令行参数(默认值:"check")。src: 传递给 Ruff 的源路径(默认值:[".", "src"])。
例如,要使用 Ruff 版本 0.8.0 运行 ruff check --select B ./src
GitLab CI/CD
您可以将以下配置添加到 .gitlab-ci.yml,以与兼容 GitLab 代码质量报告的 ruff check 并行运行 ruff format。
.base_ruff:
stage: build
interruptible: true
image:
name: ghcr.io/astral-sh/ruff:0.12.4-alpine
before_script:
- cd $CI_PROJECT_DIR
- ruff --version
Ruff Check:
extends: .base_ruff
script:
- ruff check --output-format=gitlab > code-quality-report.json
artifacts:
reports:
codequality: $CI_PROJECT_DIR/code-quality-report.json
Ruff Format:
extends: .base_ruff
script:
- ruff format --diff
pre-commit
Ruff 可以通过 ruff-pre-commit 用作 pre-commit hook
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.12.4
hooks:
# Run the linter.
- id: ruff
# Run the formatter.
- id: ruff-format
要启用 lint 修复,请将 --fix 参数添加到 lint hook
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.12.4
hooks:
# Run the linter.
- id: ruff
args: [ --fix ]
# Run the formatter.
- id: ruff-format
要避免在 Jupyter Notebook 上运行,请从允许的文件类型列表中删除 jupyter
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.12.4
hooks:
# Run the linter.
- id: ruff
types_or: [ python, pyi ]
args: [ --fix ]
# Run the formatter.
- id: ruff-format
types_or: [ python, pyi ]
使用 --fix 运行时,Ruff 的 lint hook 应放置在 Ruff 的格式化 hook 之前,以及 Black、isort 和其他格式化工具之前,因为 Ruff 的修复行为可能会输出需要重新格式化的代码更改。
在不使用 --fix 运行时,Ruff 的格式化 hook 可以放置在 Ruff 的 lint hook 之前或之后。
(只要您的 Ruff 配置避免任何linter-formatter 不兼容性,ruff format 永远不应引入新的 lint 错误,因此在 ruff check --fix 之后 运行 Ruff 的格式化 hook 是安全的。)
mdformat
mdformat 能够格式化 Markdown 中的代码块。mdformat-ruff 插件使 mdformat 能够使用 Ruff 格式化 Python 代码块。
Docker
Ruff 提供了一个包含 ruff 二进制文件的 distroless Docker 镜像。 发布了以下标签
ruff:latestruff:{major}.{minor}.{patch},例如,ruff:0.6.6ruff:{major}.{minor},例如,ruff:0.6(最新的 patch 版本)
此外,ruff 发布了以下镜像
- 基于
alpine:3.20 ruff:alpineruff:alpine3.20- 基于
debian:bookworm-slim ruff:debian-slimruff:bookworm-slim- 基于
buildpack-deps:bookworm ruff:debianruff:bookworm
与 distroless 镜像一样,每个镜像都使用 ruff 版本标签发布为 ruff:{major}.{minor}.{patch}-{base} 和 ruff:{major}.{minor}-{base},例如,ruff:0.6.6-alpine。