优化:前端依赖 Docker Volume 持久化缓存 #705
Reference in New Issue
Block a user
Delete Branch "feat/ci-frontend-node-modules-cache"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
背景
frontend-lint 和 frontend-unit-test 两个 CI job 每次都在干净容器中执行
npm ci,node_modules 从零下载安装,耗时 2-5 分钟。已有国内镜像源 + 3 次重试,但缺少持久化缓存机制。方案
使用 Docker named volume 持久化 node_modules,按
apps/web/package-lock.json的 md5 hash 命名 volume。核心逻辑
md5sum apps/web/package-lock.json | cut -c1-12,volume 命名为ci-web-nm-<hash>/workspace/apps/web/node_modules,npm ci 会删除并重写 node_modules,用 volume 持久化是安全的修改文件
scripts/ci/step_frontend_install.sh- 重写核心逻辑(hash 计算、volume 检查/创建、缓存命中跳过、旧缓存清理)scripts/ci/step_frontend_run.sh- 加上 volume 挂载,运行时也能使用缓存的 node_modules兼容性保障
scripts/ci/目录,不涉及业务代码预期收益
🚀 预览环境已部署
代码审查结果 - PR #705
⚠️ 问题(1个需要修改)
npm ci重试 3 次均失败后,脚本执行exit 1退出,但此时已创建的 Docker Volume(包含不完整的node_modules)并未被删除。下一次 CI 运行时,由于package-lock.json未变动,脚本会检测到该 Volume 存在,误判为“缓存命中”从而跳过安装,直接使用损坏的依赖目录。trap捕获退出信号进行清理。💡 建议(1个可选)
md5sum命令存在可移植性风险。md5sum是 GNU coreutils 的工具,在 macOS 或某些精简的 Unix 环境(如 Alpine 基础镜像)中可能不存在(macOS 通常为md5)。虽然 CI 环境通常是 Linux,但脚本声明为#!/bin/sh,应考虑 POSIX 兼容性。sha256sum(通常更通用)或增加命令存在性检查。✅ 格式检查通过 | ❌ 逻辑审查需修改 | ✅ 建议关注性能
🤖 由 AI 代码审查机器人自动生成 | 2026-07-22 12:16:40 | 模型:
手动审批:CI门禁全绿,auto-approve超时未完成审批。
CI门禁全绿,手动审批通过。
🗑️ 预览环境已清理
PR #705 已关闭或合并,对应的预览环境已被清理。