|
1 | | -name: Rust |
| 1 | +name: Rust CI |
2 | 2 |
|
3 | 3 | on: |
4 | 4 | push: |
|
11 | 11 | CARGO_TERM_COLOR: always |
12 | 12 |
|
13 | 13 | jobs: |
14 | | - build: |
| 14 | + check-and-test: |
| 15 | + name: Check & Test |
15 | 16 | runs-on: ${{ matrix.os }} |
16 | 17 | strategy: |
17 | 18 | matrix: |
18 | 19 | os: [ubuntu-latest, windows-latest, macos-latest] |
19 | 20 |
|
20 | 21 | steps: |
21 | | - - uses: actions/checkout@v3 |
| 22 | + - name: Checkout repository |
| 23 | + uses: actions/checkout@v3 |
22 | 24 |
|
23 | | - # 新增:检查代码格式 |
24 | | - - name: fmt |
25 | | - uses: actions-rs/cargo@v1 |
| 25 | + # 安装 Rust 工具链,并确保 fmt 和 clippy 组件存在 |
| 26 | + - name: Install Rust toolchain |
| 27 | + uses: actions-rust-lang/setup-rust-toolchain@v1 |
26 | 28 | with: |
27 | | - command: fmt |
28 | | - # --check 参数让命令在发现未格式化的代码时失败,而不是直接修改文件 |
29 | | - args: -- --check |
| 29 | + components: clippy, fmt |
30 | 30 |
|
31 | | - # 新增:运行 Clippy lints |
32 | | - - name: clippy |
33 | | - uses: actions-rs/cargo@v1 |
| 31 | + # 新增:为 Cargo 设置缓存,极大提升后续运行速度 |
| 32 | + - name: Cache Cargo dependencies |
| 33 | + uses: actions/cache@v3 |
34 | 34 | with: |
35 | | - command: clippy |
36 | | - # --all-features 确保检查所有特性门控后的代码 |
37 | | - # -- -D warnings 将所有 Clippy 警告(warnings)视为错误(Deny),导致 CI 失败 |
38 | | - args: --all-features -- -D warnings |
| 35 | + # 缓存 target 目录和 cargo registry/git a索引 |
| 36 | + path: | |
| 37 | + ~/.cargo/bin/ |
| 38 | + ~/.cargo/registry/index/ |
| 39 | + ~/.cargo/registry/cache/ |
| 40 | + ~/.cargo/git/db/ |
| 41 | + target/ |
| 42 | + # 当 Cargo.lock 文件变化时,缓存失效 |
| 43 | + key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} |
| 44 | + restore-keys: | |
| 45 | + ${{ runner.os }}-cargo- |
39 | 46 |
|
40 | | - # 原有的步骤保持不变 |
41 | | - - name: Install tools |
42 | | - uses: actions-rs/cargo@v1 |
43 | | - with: |
44 | | - command: install |
45 | | - args: cargo-all-features |
46 | | - - name: Release build |
47 | | - uses: actions-rs/cargo@v1 |
48 | | - with: |
49 | | - command: build |
50 | | - args: --release --all-features |
| 47 | + # 1. 检查代码格式 |
| 48 | + - name: Check formatting |
| 49 | + run: cargo fmt --all -- --check |
| 50 | + |
| 51 | + # 2. 运行 Clippy |
| 52 | + - name: Run Clippy |
| 53 | + run: cargo clippy --all-targets --all-features -- -D warnings |
| 54 | + |
| 55 | + # 3. 运行测试 |
51 | 56 | - name: Run tests |
52 | | - uses: actions-rs/cargo@v1 |
53 | | - with: |
54 | | - command: test-all-features |
55 | | - args: --verbose |
56 | | - - name: Check if can be packaged |
57 | | - uses: actions-rs/cargo@v1 |
58 | | - with: |
59 | | - command: package |
| 57 | + run: cargo test --all-features --verbose |
| 58 | + |
| 59 | + # 4. 构建 Release 版本 |
| 60 | + - name: Build release |
| 61 | + run: cargo build --release --all-features |
60 | 62 |
|
| 63 | + # publish 任务保持不变,但可以依赖新的 check-and-test 任务 |
61 | 64 | # publish: |
62 | 65 | # runs-on: ubuntu-latest |
63 | | - # needs: [build] |
| 66 | + # needs: [check-and-test] # 依赖新的任务名 |
64 | 67 | # if: startsWith(github.ref, 'refs/tags/') |
65 | 68 |
|
66 | 69 | # steps: |
|
0 commit comments