feat(ruff): add Ruff configuration for code linting and formatting #6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Ruff Linter | |
on: [ push, pull_request ] | |
jobs: | |
ruff: | |
name: Run Ruff Linter | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Setup Python | |
uses: actions/setup-python@v4 | |
with: | |
python-version: '3.x' # 兼容所有 Python 版本 | |
- name: Cache Ruff | |
uses: actions/cache@v3 | |
with: | |
path: ~/.cache/ruff | |
key: ruff-${{ runner.os }}-${{ github.sha }} | |
restore-keys: | | |
ruff-${{ runner.os }}- | |
- name: Run Ruff Linter | |
uses: astral-sh/ruff-action@v3 | |
with: | |
args: check . # 执行 lint 代码检查 | |
# 尝试自动修复 | |
- name: Fix Ruff Issues (Optional) | |
run: ruff check . --fix | |
# 如果检测失败,则终止 CI | |
- name: Fail if Lint Errors Exist | |
run: | | |
if [[ $(ruff check . --output-format=json | jq '. | length') -gt 0 ]]; then | |
exit 1 | |
fi |