refactor: 移除 node_modules 缓存模式,仅保留 pnpm store 缓存策略

This commit is contained in:
Lyda
2025-10-11 19:25:35 +08:00
parent 4495633411
commit acea3c86c1
2 changed files with 31 additions and 59 deletions

View File

@@ -1,11 +1,11 @@
# pnpm 依赖安装与缓存 Action
专为 pnpm 项目设计的 GitHub Action通过缓存 `node_modules` pnpm store在 CI/CD 中实现快速复用,二次执行可在 10 秒内完成安装。
专为 pnpm 项目设计的 GitHub Action通过缓存 pnpm store在 CI/CD 中实现快速复用,二次执行可在 10 秒内完成安装。
## ✨ 特性
- **专注 pnpm**默认缓存 `node_modules`,命中后直接复用,无需重新链接
- **双缓存模式**:可在 `node_modules` / `store` 之间切换,满足不同目录约束
- **专注 pnpm**:缓存 pnpm store命中后仅执行快速链接
- **离线友好**:自动根据缓存命中追加 `--offline`/`--prefer-offline`
- **锁文件准确性**:推荐传入 `cache-hash`(如 `hashFiles('pnpm-lock.yaml')`)确保缓存精准失效
- **自定义安装**:支持附加参数或完全覆盖安装命令,保留 `force-install` 选项
- **环境整洁**:自动设置 `PNPM_STORE_DIR`,可选清理项目根 `.pnpm-store`
@@ -14,9 +14,8 @@
| 参数名 | 描述 | 必需 | 默认值 |
| --- | --- | --- | --- |
| `cache-mode` | 缓存模式:`node_modules` / `store` | 否 | `node_modules` |
| `cache-prefix` | 缓存 key 前缀 | 否 | `modules` |
| `node-modules-path` | `node_modules` 目录路径(仅 `cache-mode=node_modules` 时生效) | 否 | `node_modules` |
| `store-path` | 自定义 pnpm store 路径(默认使用 runner 临时目录) | 否 | `''` |
| `force-install` | 是否强制安装(追加 `--force` | 否 | `false` |
| `install-command` | 自定义安装命令,覆盖默认的 `pnpm install` | 否 | `''` |
| `install-args` | 附加参数(仅默认命令时生效) | 否 | `''` |
@@ -40,27 +39,20 @@
cache-hash: ${{ hashFiles('pnpm-lock.yaml') }}
```
- 默认缓存 `node_modules`命中后直接跳过安装步骤,满足 10 秒内完成的目标
- 默认缓存 pnpm store命中后执行 `pnpm install --offline --frozen-lockfile`通常在 10 秒内完成链接
- 首次执行或 lock 文件变更时执行 `pnpm install --prefer-offline --frozen-lockfile`,完成后写入缓存。
## 🔁 缓存模式
## 🔁 缓存路径
### `cache-mode: node_modules`(默认)
- **适用场景**:希望二次执行直接复用产物、编译型依赖较多。
- **行为**:缓存 `node_modules` 指定目录。命中缓存后跳过安装步骤。
- **额外设定**Action 会自动将 `pnpm node-linker` 设置为 `hoisted`,避免使用 symlink生成更接近 npm 的目录结构。
### `cache-mode: store`
默认缓存路径为 `${{ runner.temp }}/.pnpm-store`。如需与团队现有目录保持一致,可通过 `store-path` 自定义:
```yaml
with:
cache-mode: store
store-path: ~/.cache/pnpm-store
cache-hash: ${{ hashFiles('pnpm-lock.yaml') }}
```
- **适用场景**:项目对 `node_modules` 目录结构有额外处理,或需要保持 `node_modules` 在工作目录内新生成
- **行为**:缓存 `${{ runner.temp }}/.pnpm-store`。命中缓存后执行 `pnpm install --offline --frozen-lockfile`,仅进行符号链接操作。
> 提示:如自定义路径,需确保同一 Runner 不会被多个并发作业共享该目录,以免产生竞争条件
## ⚙️ 进阶配置