Skip to content

Commit 2fb8484

Browse files
committed
update workflows
1 parent a661110 commit 2fb8484

File tree

1 file changed

+39
-36
lines changed

1 file changed

+39
-36
lines changed

.github/workflows/rust.yml

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Rust
1+
name: Rust CI
22

33
on:
44
push:
@@ -11,56 +11,59 @@ env:
1111
CARGO_TERM_COLOR: always
1212

1313
jobs:
14-
build:
14+
check-and-test:
15+
name: Check & Test
1516
runs-on: ${{ matrix.os }}
1617
strategy:
1718
matrix:
1819
os: [ubuntu-latest, windows-latest, macos-latest]
1920

2021
steps:
21-
- uses: actions/checkout@v3
22+
- name: Checkout repository
23+
uses: actions/checkout@v3
2224

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
2628
with:
27-
command: fmt
28-
# --check 参数让命令在发现未格式化的代码时失败,而不是直接修改文件
29-
args: -- --check
29+
components: clippy, fmt
3030

31-
# 新增:运行 Clippy lints
32-
- name: clippy
33-
uses: actions-rs/cargo@v1
31+
# 新增:为 Cargo 设置缓存,极大提升后续运行速度
32+
- name: Cache Cargo dependencies
33+
uses: actions/cache@v3
3434
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-
3946
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. 运行测试
5156
- 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
6062

63+
# publish 任务保持不变,但可以依赖新的 check-and-test 任务
6164
# publish:
6265
# runs-on: ubuntu-latest
63-
# needs: [build]
66+
# needs: [check-and-test] # 依赖新的任务名
6467
# if: startsWith(github.ref, 'refs/tags/')
6568

6669
# steps:

0 commit comments

Comments
 (0)