Github操作不跨作业缓存node_modules

skw*_*wny 7 caching github-actions

我对 pull_request 有一个 Github 操作,并尝试在作业之间缓存 node_modules。我只能看到缓存在作业重新运行时工作,但在打开新 PR 时却看不到,即使我的 node_modules 没有任何更改。

这是设计使然吗?

每次它在新的 PR 中运行时,我都会在作业中看到以下输出:

Run actions/cache@v2
  with:
    path: ~/.npm
    key: Linux-build-cache-node-modules-83fc6365b85dd061416fccd5993d48c2003c388bb2184fd57f28d1041d9d261e
    restore-keys: Linux-build-cache-node-modules-
  Linux-build-
  Linux-
  
  env:
    cache-name: cache-node-modules
Cache not found for input keys: Linux-build-cache-node-modules-83fc6365b85dd061416fccd5993d48c2003c388bb2184fd57f28d1041d9d261e, Linux-build-cache-node-modules-, Linux-build-, Linux-

Run Code Online (Sandbox Code Playgroud)

然而,重新运行作业将享受缓存命中:

Run actions/cache@v2
Received 54525952 of 109978126 (49.6%), 51.8 MBs/sec
Received 109978126 of 109978126 (100.0%), 67.8 MBs/sec
Cache Size: ~105 MB (109978126 B)
/usr/bin/tar --use-compress-program zstd -d -xf /home/runner/work/_temp/0bdf436d-9d4d-4397-b8ed-5910ff503949/cache.tzst -P -C /home/runner/work/argutopia/argutopia
Cache restored successfully
Cache restored from key: Linux-build-cache-node-modules-83fc6365b85dd061416fccd5993d48c2003c388bb2184fd57f28d1041d9d261e
Run Code Online (Sandbox Code Playgroud)

这是我的action yml供参考:

name: PR CI
on:
  pull_request:
    branches: [main, develop]
jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [14.x]
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v1
        with:
          node-version: ${{ matrix.node-version }}

      - name: Cache node modules
        uses: actions/cache@v2
        id: cache
        env:
          cache-name: cache-node-modules
        with:
          path: ~/.npm
          key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-build-${{ env.cache-name }}-
            ${{ runner.os }}-build-
            ${{ runner.os }}-

      - name: Install Dependencies
        if: steps.cache.outputs.cache-hit != 'true'
        run: |
          echo '::debug::deps cache miss - installing deps'
          npm install
Run Code Online (Sandbox Code Playgroud)

小智 2

缓存~/.npm不允许您跳过运行npm install。它将在本机上保存全局 npm 缓存,以便运行时npm install不会产生太多下载时间,但您仍然需要node_modules为项目构建本地缓存。

如果您希望能够跳过安装,您也可以缓存项目node_modules文件夹,并在缓存命中时跳过安装,尽管这可能会带来一些问题。