-
Notifications
You must be signed in to change notification settings - Fork 17
60 lines (52 loc) · 1.88 KB
/
pre-commit.yml
File metadata and controls
60 lines (52 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
name: Pre-commit Checks
# 触发条件:当有 PR 提交到 master 分支时自动运行
on:
pull_request:
branches: [ "master" ]
workflow_dispatch: # 添加这行支持手动触发
inputs:
branch:
description: 'Branch to test'
required: false
default: 'master'
type: string
jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
# 第一步:检出代码到 runner 环境
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ github.event.inputs.branch || github.ref }} # 支持指定分支
# 第二步:设置 Python 环境
# 使用 Python 3.11 确保与开发环境一致
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'
# 第三步:缓存 pre-commit hooks
# 避免每次都重新下载和安装 hooks,提高构建速度
- name: Cache pre-commit
uses: actions/cache@v3
with:
path: ~/.cache/pre-commit
key: pre-commit-${{ hashFiles('.pre-commit-config.yaml') }}
# 第四步:安装 pre-commit 工具
# 升级 pip 确保使用最新版本,然后安装 pre-commit
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pre-commit
# 第五步:安装项目依赖
# 支持 requirements.txt 和 pyproject.toml 两种依赖管理方式
# 以可编辑模式安装项目,确保 imports 正常工作
- name: Install project dependencies
run: |
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
if [ -f pyproject.toml ]; then pip install -e .; fi
# 第六步:运行所有 pre-commit hooks
# 包括代码格式检查、linting、单元测试等
# --all-files 确保检查所有文件,不仅仅是变更的文件
- name: Run pre-commit hooks
run: pre-commit run --all-files