Windows环境下GitHub Actions用actions/cache@v2缓存NPM依赖失效
问题排查结果
缓存体积异常小的核心原因是Windows环境下npm缓存路径配置不匹配,你配置的~/.npm是Linux/macOS系统的npm默认缓存路径,Windows系统npm默认缓存路径为~\AppData\Roaming\npm-cache,你配置的路径在Windows runner中不存在或仅包含空文件,因此缓存仅能命中到极少内容。
另外你的原YAML配置存在语法缩进错误:runs-on: windows-latest 层级错误,没有对齐到build_on_win子级,可能导致工作流解析异常。

修复方案
方案1:修正缓存路径适配Windows
升级缓存action到兼容性更好的v3版本,同时配置多系统兼容的缓存路径:
name: Build on: push jobs: build_on_win: runs-on: windows-latest if: "!contains(github.event.head_commit.message, 'skip-publish')" steps: - uses: actions/checkout@v2 - uses: actions/setup-node@master with: node-version: 15 - name: Cache NPM dependencies uses: actions/cache@v3 with: path: | ~/.npm ~\AppData\Roaming\npm-cache key: ${{ runner.OS }}-npm-cache-${{ hashFiles('**/package-lock.json') }} restore-keys: | ${{ runner.OS }}-npm-cache- - name: install dependencies run: npm install # 其余逻辑
方案2:使用setup-node内置缓存能力(更推荐)
actions/setup-node从v2版本开始内置npm/yarn/pnpm缓存支持,自动适配不同系统的缓存路径,无需单独配置actions/cache,配置更简洁:
name: Build on: push jobs: build_on_win: runs-on: windows-latest if: "!contains(github.event.head_commit.message, 'skip-publish')" steps: - uses: actions/checkout@v2 - uses: actions/setup-node@master with: node-version: 15 cache: 'npm' # 开启内置npm缓存,自动适配系统路径 - name: install dependencies run: npm install # 其余逻辑
内容的提问来源于stack exchange,提问作者Ajit Kumar
相关产品推荐
相关产品推荐

