1- name : Run tests
1+ name : Run tests & Code Quality Checks
22
3- on : [push]
3+ on :
4+ push :
5+ branches :
6+ - main # 在代码合并到 main 分支后,仍然运行一次完整的 CI
7+ pull_request :
8+ branches :
9+ - main # 在针对 main 分支的 PR 提出/更新时运行 CI
410
511env :
612 CARGO_TERM_COLOR : always
7- RUST_TOOLCHAIN : nightly-2025-01-13
8-
9- QEMU_VERSION : qemu-9.2.1
10- QEMU_BUILD_PATH : ${{ github.workspace }}/qemu-build-cache
13+ RUST_TOOLCHAIN : nightly-2025-01-13
14+
15+ QEMU_VERSION : qemu-9.2.1
16+ QEMU_BUILD_PATH : ${{ github.workspace }}/qemu-build-cache
1117
1218jobs :
1319 run-tests :
@@ -16,29 +22,55 @@ jobs:
1622 - name : ⬇️ Checkout code
1723 uses : actions/checkout@v4
1824
19- # --- 1. 缓存 Cargo 依赖项 ---
20- - name : 📦 Cache Cargo Dependencies & Build Artifacts
21- uses : actions/cache@v4
25+ # --- 1. 缓存 Cargo 依赖项 (使用 Swatinem/rust-cache) ---
26+ # ⚠️ 替换了原有的 actions/cache@v4,更高效地管理 Cargo 缓存
27+ - name : 📦 Setup Rust Cache
28+ uses : Swatinem/rust-cache@v2
2229 with :
23- path : |
24- ~/.cargo/registry
25- ~/.cargo/git
26- target
27- key : ${{ runner.os }}-cargo-${{ env.RUST_TOOLCHAIN }}-${{ hashFiles('**/Cargo.lock') }}
28- restore-keys : |
29- ${{ runner.os }}-cargo-${{ env.RUST_TOOLCHAIN }}-
30+ # 仅需传递 key 相关的变量,该 Action 会自动处理路径
31+ key : ${{ runner.os }}-${{ env.RUST_TOOLCHAIN }}-${{ hashFiles('**/Cargo.lock') }}
3032
3133 # --- 2. 安装 Rust Toolchain 和 Target ---
3234 - name : 🛠️ Setup Rust Toolchain and Target
3335 uses : dtolnay/rust-toolchain@master
3436 with :
35- toolchain : ${{ env.RUST_TOOLCHAIN }}
37+ toolchain : ${{ env.RUST_TOOLCHAIN }}
3638 targets : riscv64gc-unknown-none-elf
39+ # 确保安装了 rustfmt 和 clippy
3740 components : rust-src, rustfmt, clippy, llvm-tools-preview
3841
42+ # --- 2.1 代码质量检查 (优先执行) ---
43+
44+ - name : 🔍 Cargo Check (快速验证编译)
45+ working-directory : os
46+ run : cargo check --target riscv64gc-unknown-none-elf
47+
48+ - name : 📏 Run Code Formatting Check
49+ working-directory : os
50+ # 使用 --check 模式,如果代码未格式化,则 CI 失败
51+ run : cargo fmt --all -- --check
52+
53+ - name : 🔬 Run Clippy Lint Check
54+ working-directory : os
55+ # 使用 -D warnings 强制将所有警告升级为错误,确保 PR 必须解决所有代码异味
56+ run : cargo clippy --target riscv64gc-unknown-none-elf -- -D warnings
57+
58+ # --- 2.2 安装辅助工具 ---
59+
3960 - name : Install cargo-binutils
4061 run : cargo install cargo-binutils
4162
63+ # --- 2.5 缓存 Apt 包 (优化 QEMU 依赖安装时间) ---
64+ - name : 📦 Cache APT Dependencies
65+ uses : actions/cache@v4
66+ with :
67+ path : |
68+ /var/cache/apt/archives
69+ /var/lib/apt/lists
70+ key : ${{ runner.os }}-apt-qemu-deps-v1
71+ restore-keys : |
72+ ${{ runner.os }}-apt-qemu-deps-
73+
4274 # --- 3. QEMU 编译和缓存 ---
4375 - name : 🧱 Install QEMU Dependencies
4476 run : |
0 commit comments