Skip to content

feat:给 CI 添加缓存从而加速 CI 流程 - #227

Merged
1lck merged 5 commits into
preview/0.3.0from
fix/issue-226
Aug 22, 2026
Merged

feat:给 CI 添加缓存从而加速 CI 流程#227
1lck merged 5 commits into
preview/0.3.0from
fix/issue-226

Conversation

@xiaoyumuxi

@xiaoyumuxi xiaoyumuxi commented Aug 22, 2026

Copy link
Copy Markdown
Collaborator

此 PR 的目标是利用 Github Action 的缓存机制来避免出现频繁的下载 rust、swift、ts 依赖还有相关的编译工作的耗时导致 CI 流程需要的时间过长,因此使用缓存将下载的包等依赖使用包名+SHA256 校验的方式来校验是否变更,如果没有变更的部分依赖等等就直接进行复用缓存,否则的话才会降级到普通的下载+编译的逻辑上去

@xiaoyumuxi
xiaoyumuxi requested a review from 1lck as a code owner August 22, 2026 04:01
@xiaoyumuxi xiaoyumuxi changed the title ci(windows): validate caches with safe fallbacks 给 CI 添加缓存从而加速 CI 流程 Aug 22, 2026
@xiaoyumuxi

Copy link
Copy Markdown
Collaborator Author

但是,因为 GitHub 的缓存机制是将缓存部分的数据打包保存,在下一次 CI 流程中再进行下载复用,它并没有实现编译器级别的增量编译。因此,Rust 部分和 Bun 部分的编译工作还是要重新执行的,但这已经大大减少了下载依赖的时间。后续应该还有其他的方案进行优化,但是要是想实现完全的和本地一样的增量编译应该是几乎不可能的(而且对这种产物需要进行哈希校验来着)

@xiaoyumuxi

Copy link
Copy Markdown
Collaborator Author

可以考虑给 Rust 加 sccache。它在 rustc 前面按源代码、编译器版本、参数等生成内容哈希,逐个复用编译结果,比缓存整个 target 目录细得多。
不过我不会直接建议打开 CARGO_INCREMENTAL=1:增量目录通常很大,可能出现“节省 2 分钟编译,却多花 3 分钟下载解压”的情况。
因此还是需要进行测试的,这个思路方案

@xiaoyumuxi

Copy link
Copy Markdown
Collaborator Author

Windows CI 缓存冷/热基准对比

两次测量使用同一个 PR merge ref 和同一个提交(385c9490af4afe4d9d32108c544f68e5674b2f93),主要变量只有仓库依赖缓存状态:

  • 仓库缓存冷启动:attempt 1
  • 仓库缓存热启动:attempt 2
  • 两次均完整通过,包括最终 Windows CI gate。

这里的“冷启动”是指四个仓库级缓存全部 miss:Cargo downloads、Cargo build outputs、Bun downloads 和 JDTLS downloads。setup-bun 自身的 Bun runtime cache 在两次运行中都已命中。

总览

项目 冷启动 热启动 差值
Classify changes 10s 11s +1s
Build and test Windows implementation 28m02s 14m35s -13m27s(缩短 48.0%,约 1.92×)
Windows CI gate 2s 2s
首个 job 开始 → gate 完成 28m20s 14m53s -13m27s(缩短 47.5%,约 1.90×)

Windows 主 job 逐步骤对比

步骤 冷启动 热启动 差值
Set up job 2s 2s
Check out source 12s 10s -2s
Set up Rust 31s 19s -12s
Set up Bun 3s 4s +1s
Configure isolated dependency caches 1s <1s ≈-1s
Resolve Rust cache identity <1s 1s ≈+1s
Restore Cargo downloads 1s(miss) 2s(exact hit) +1s
Restore Cargo build outputs <1s(miss) 38s(exact hit) ≈+38s
Restore Bun downloads 1s(miss) 21s(exact hit) +20s
Restore JDTLS downloads <1s(miss) 3s(exact hit) ≈+3s
Validate restored caches or fall back 4s 23s +19s
Build Windows Tauri application 17m34s 10m36s -6m58s (-39.7%)
Verify Windows boundaries 11s 11s
Test shared Rust Core 2m06s 52s -1m14s (-58.7%)
Test Windows Rust host 3m44s 42s -3m02s (-81.3%)
Post Restore JDTLS downloads 4s 3s -1s
Post Restore Bun downloads 1m03s <1s ≈-1m03s
Post Restore Cargo build outputs 2m16s 1s -2m15s
Post Restore Cargo downloads 4s 1s -3s
Post Set up Bun <1s <1s
Post Check out source 3s 3s
Complete job <1s <1s

GitHub Jobs API 的时间戳精度为 1 秒,所以 <1s 表示开始与结束时间落在同一秒,并非数学意义上的零耗时。

缓存恢复与完整性校验

热启动中的四项缓存全部为 exact primary-key hit;日志中每项均包含相同 key 的 Cache hit forCache restored from key,并在 post step 中明确显示 not saving cache

缓存 恢复大小 恢复耗时 校验结果
Cargo downloads 58.5 MiB 2s 438 archives,0 rejected
Cargo build outputs 1,560.3 MiB 38s Exact build-cache hit
Bun downloads 232.6 MiB 21s Bun 1.3.12 与 lock SHA-256 匹配;54,604 个文件通过校验
JDTLS downloads 46.8 MiB 3s 4 artifacts,0 rejected
总计 1.854 GiB 64s 无缓存降级或缓存相关 warning

缓存恢复与完整性校验从冷启动的 6s 增加到热启动的 1m27s;但 exact hit 避免了冷启动中 3m27s 的缓存上传。热启动的 post-cache 处理约 5s,新增上传为 0 bytes。

构建阶段内部拆分

内部阶段 冷启动 热启动 差值
Tauri build 前的 Bun/依赖准备 ≈4m06s ≈2m05s ≈-2m01s
Vite production build 8.22s 8.56s 基本不变
Cargo release profile 13m01s 8m12s -4m49s
Shared Core Cargo test profile 1m49s 32.47s ≈-1m17s
Windows host Cargo test profile 3m43s 39.45s ≈-3m04s

结论

经过完整性校验的仓库缓存将 Windows 主 job 几乎减半:28m02s → 14m35s。当前最大的剩余瓶颈是第一方 Rust release 编译/链接(热启动仍需 8m12s),而不是依赖下载或缓存上传。

@1lck
1lck merged commit b745eb8 into preview/0.3.0 Aug 22, 2026
19 checks passed

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

It introduces broad, cross-platform CI caching + fallback behavior changes that can subtly affect build correctness and reliability and should be validated by a human reviewer end-to-end.

Pull request overview

该 PR 旨在通过将依赖下载与构建产物迁移到仓库内的隔离缓存(.artifacts)并在恢复后进行校验,从而显著减少 CI 中重复下载/编译(Rust / SwiftPM / Bun / JDTLS)的耗时,并在校验失败时安全降级回“重新下载/重建”的路径。

Changes:

  • 新增 scripts/verify-download-cache.mjs 及配套验证脚本,在 CI 恢复缓存后按 SHA-256/清单校验并剔除不可信条目(含符号链接防护、Git bare repo 安全检查等)。
  • Windows CI/Release 引入 Cargo 下载缓存 + build outputs 缓存 + Bun/JDTLS 缓存,并增加失败后的清理与重试逻辑(含 invoke-*-with-cache-fallback 脚本)。
  • macOS CI/Release 用 composite action .github/actions/prepare-macos-dependency-cache 统一恢复与校验 SwiftPM/Cargo/JDTLS 的仓库级缓存,并在需要时执行 SwiftPM resolve 的 fallback。
File summaries
File Description
windows/tauri/rust-toolchain.toml 将 Windows Tauri Rust toolchain 切换到 stable,以匹配 CI 缓存与构建策略。
windows/tauri/package.json 调整 Bun packageManager 版本,使 CI 通过 bun-version-file 与仓库声明保持一致。
scripts/verify-download-cache.mjs 新增:统一的下载/依赖缓存校验与清单封存工具(Cargo/JDTLS/SwiftPM/Bun)。
scripts/validate-windows-build-caches.ps1 新增:Windows 侧缓存恢复结果检查 + 校验失败时清理并设置环境标志。
scripts/validate-macos-dependency-caches.sh 新增:macOS 侧缓存恢复结果检查 + 调用校验器并在失败时清理隔离缓存。
scripts/test-verify-download-cache.mjs 新增:校验器单元测试 + 断言工作流/脚本正确配置缓存隔离路径。
scripts/test-classify-ci-changes.sh 更新:将下载缓存校验器与 macOS composite action 纳入 CI 变更分类测试覆盖。
scripts/prepare-macos-dependencies.sh 新增:SwiftPM 依赖解析与缓存封存(写入 .lithe-integrity.json)并提供恢复后失败的重试降级。
scripts/prepare-jdtls.sh 更新:在缓存回退场景输出 GitHub Actions warning(更可观测)。
scripts/prepare-jdtls.ps1 更新:在缓存回退场景输出 GitHub Actions warning(更可观测)。
scripts/package-windows.ps1 更新:支持指定 Rust target、使用新的 Bun 安装脚本与 Tauri 构建 fallback,且校验 bundled extensions 资源完整性。
scripts/invoke-windows-tauri-build.ps1 新增:Tauri build 在缓存恢复后失败时清理 target 并重试一次的统一封装。
scripts/invoke-cargo-with-cache-fallback.ps1 新增:Cargo 命令在缓存恢复后失败时清理 target 并重试一次的统一封装。
scripts/install-windows-frontend-dependencies.ps1 新增:Windows 前端依赖安装使用隔离 Bun cache/tmp,失败时清理并重试,同时支持封存 Bun cache 清单。
scripts/classify-ci-changes.sh 更新:将新增脚本与 composite action 正确映射到 CI lanes(swift/plugins/windows/rust_core 等)。
scripts/build-windows.ps1 更新:改用新的 Bun 安装脚本与 Tauri 构建 fallback。
.github/workflows/release-windows.yml 更新:Windows Release 引入隔离缓存恢复、校验与 fallback,并改为调用打包脚本路径。
.github/workflows/release-preview-windows.yml 更新:Windows Preview Release 引入隔离缓存恢复、校验与 fallback。
.github/workflows/release-preview-macos.yml 更新:macOS Preview Release 使用新的 composite action 恢复/校验依赖缓存。
.github/workflows/release-macos.yml 更新:macOS Release 使用 composite action(并支持 restore-only)。
.github/workflows/ci-windows.yml 更新:Windows CI 引入隔离缓存恢复/校验与 fallback;新增运行校验器测试的步骤。
.github/workflows/ci-plugins.yml 更新:Plugins CI 使用 composite action 恢复/校验 SwiftPM 缓存。
.github/workflows/ci-macos.yml 更新:macOS CI 使用 composite action 恢复/校验 SwiftPM/Cargo 缓存。
.github/workflows/ci-database.yml 更新:Database CI 使用 composite action 恢复/校验 SwiftPM 缓存。
.github/actions/prepare-macos-dependency-cache/action.yml 新增:macOS 依赖缓存的统一 restore/validate/prepare composite action。
Review details
  • Files reviewed: 25/25 changed files
  • Comments generated: 2
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines 141 to +143
"node": ">=22.0.0"
},
"packageManager": "bun@1.3.14"
"packageManager": "bun@1.3.12"
Comment on lines +136 to +148
await fs.writeFile(fakeBun, "#!/bin/sh\nprintf '1.3.14\\n'\n", { mode: 0o700 });
await fs.mkdir(path.dirname(cachedPackage), { recursive: true });
await fs.writeFile(bunLock, "fixture-lock\n");
await fs.writeFile(cachedPackage, "export default 1;\n");
const bunEnvironment = { ...testEnvironment, PATH: `${fakeBin}${path.delimiter}${process.env.PATH}` };
const bunArguments = [
"--cargo-cache",
cargoCache,
"--cargo-lock",
cargoLock,
"--bun-version",
"1.3.14",
"--bun-lock",
@xiaoyumuxi xiaoyumuxi changed the title 给 CI 添加缓存从而加速 CI 流程 feat:给 CI 添加缓存从而加速 CI 流程 Aug 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants