feat: 更新 npm-install GitHub Action,移除 lockfile-name 输入参数,添加 cache-hash 参数以支持自定义缓存 hash,优化锁文件处理逻辑,确保在未提供 hash 时使用 package.json 作为 fallback,提升缓存管理的灵活性和可靠性,同时更新文档以反映新功能和使用示例。

This commit is contained in:
Lyda
2025-08-21 10:34:47 +08:00
parent 75b194bdfb
commit 3f3997c5dc
3 changed files with 181 additions and 45 deletions

View File

@@ -5,8 +5,9 @@
## ✨ 特性
- 🚀 **智能缓存**: 自动缓存 node_modules避免重复安装
- 📦 **多包管理器支持**: 支持 npm、pnpm、yarn
- 📦 **多包管理器支持**: 支持 npm、pnpm、yarn,自动检测标准锁文件
- 🎯 **灵活配置**: 可自定义缓存前缀、安装命令等
- 🔑 **精确 hash 控制**: 支持自定义缓存 hash确保依赖变化时缓存失效
- 🔄 **Git 集成**: 可选的 git stash 功能
- 📊 **详细输出**: 提供缓存命中状态和使用的缓存 key
@@ -15,12 +16,12 @@
| 参数名 | 描述 | 是否必需 | 默认值 |
| ------------------- | ---------------------------- | -------- | -------------- |
| `package-manager` | 包管理器类型 (npm/pnpm/yarn) | 否 | `npm` |
| `lockfile-name` | lock 文件名称 | 否 | 自动检测 |
| `cache-prefix` | 缓存前缀名称 | 否 | `modules` |
| `node-modules-path` | node_modules 目录路径 | 否 | `node_modules` |
| `force-install` | 是否强制安装 | 否 | `false` |
| `enable-git-stash` | 安装后是否执行 git stash | 否 | `false` |
| `install-command` | 自定义安装命令(覆盖默认) | 否 | `''` |
| `cache-hash` | 缓存 hash 值(**推荐使用** | 否 | 自动计算 |
## 📤 输出参数
@@ -29,6 +30,25 @@
| `cache-hit` | 是否命中缓存 (true/false) |
| `cache-key` | 使用的缓存 key |
## 💡 重要提示
**强烈推荐使用 `cache-hash` 参数!**
使用 `cache-hash` 参数有以下优势:
-**精确控制**: 让你完全控制缓存失效的条件
-**环境兼容**: 避免在不同容器环境中 hash 计算不一致的问题
-**灵活组合**: 可以组合多个文件的 hash`package.json` + `package-lock.json` + `.nvmrc`
-**调试友好**: 在 workflow 中可以清楚看到使用的 hash 值
```yaml
# 推荐用法
with:
cache-hash: ${{ hashFiles('package-lock.json') }}
```
如果不提供 `cache-hash`action 会自动使用 `package.json` 作为 fallback但不如手动指定精确。
## 🚀 使用方法
### 基础用法 (npm)
@@ -78,6 +98,31 @@
install-command: "npm ci --only=production"
```
### 使用自定义缓存 hash推荐用法
```yaml
# 推荐手动指定缓存hash确保精确的缓存管理
- name: 安装npm依赖
uses: actions/xgj/npm-install@v1
with:
package-manager: "npm"
cache-hash: ${{ hashFiles('package-lock.json') }}
# pnpm项目
- name: 安装pnpm依赖
uses: actions/xgj/npm-install@v1
with:
package-manager: "pnpm"
cache-hash: ${{ hashFiles('pnpm-lock.yaml') }}
# 多文件hash组合
- name: 安装依赖多文件hash
uses: actions/xgj/npm-install@v1
with:
package-manager: "npm"
cache-hash: ${{ hashFiles('package.json', 'package-lock.json', '.nvmrc') }}
```
### 检查缓存状态
```yaml