在 GitHub Actions 中缓存 node_modules

Ten*_*eff 25 github yarnpkg github-actions

我有一个 Yarn monorepo(工作区),有 2 个包:后端(Node.js/TypeScript)和前端(React/Typescript)。

\n

/package.json(已修剪)

\n
{\n  "workspaces": [\n    "backend",\n    "frontend"\n  ],\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我正在尝试添加与 GitHub Actions 的持续集成,并尝试使用它actions/cache@v2来缓存 Yarn 缓存目录和所有项目'node_modules目录

\n

.github/workflows/CI.yml(已修剪)

\n
    steps:\n      - uses: actions/checkout@v2\n\n      - name: Get yarn cache directory path\n        id: yarn-cache-dir-path\n        run: |\n          echo "::set-output name=dir::$(yarn cache dir)"\n          echo "::set-output name=version::$(yarn -v)"\n\n      - name: Use Node.js ${{ matrix.node-version }}\n        uses: actions/setup-node@v2\n        with:\n          node-version: ${{ matrix.node-version }}\n\n      - uses: actions/cache@v2\n        with:\n          path: |\n            ${{ steps.yarn-cache-dir-path.outputs.dir }}\n            '**/node_modules'\n            '**/.eslintcache'\n          key: ${{ runner.os }}-yarn-${{ steps.yarn-cache-dir-path.outputs.version }}-${{ hashFiles('**/yarn.lock') }}\n\n      - name: Install packages\n        run: yarn install --frozen-lockfile\n
Run Code Online (Sandbox Code Playgroud)\n

我收到缓存已存储并在连续运行中重新使用:

\n
key: Linux-yarn-1.22.10-143fef95c7228810cf502305eff3be1cbc468dc8a3e0b153a4311c0250aaef6f\nReceived 158645465 of 175422681 (90.4%), 151.3 MBs/sec\nReceived 175422681 of 175422681 (100.0%), 138.1 MBs/sec\nCache Size: ~167 MB (175422681 B)\n/usr/bin/tar --use-compress-program zstd -d -xf /home/runner/work/_temp/08363700-9a23-447e-a80e-6f3dbec6068f/cache.tzst -P -C /home/runner/work/path\nCache restored successfully\nCache restored from key: Linux-yarn-1.22.10-143fef95c7228810cf502305eff3be1cbc468dc8a3e0b153a4311c0250aaef6f\n
Run Code Online (Sandbox Code Playgroud)\n

yarn仍尝试解决依赖关系:

\n
yarn install --frozen-lockfile\nshell: /usr/bin/bash -e {0}\nyarn install v1.22.10\n[1/4] Resolving packages...\n[2/4] Fetching packages...\ninfo fsevents@2.3.2: The platform "linux" is incompatible with this module.\ninfo "fsevents@2.3.2" is an optional dependency and failed compatibility check. Excluding it from installation.\ninfo fsevents@2.3.1: The platform "linux" is incompatible with this module.\ninfo "fsevents@2.3.1" is an optional dependency and failed compatibility check. Excluding it from installation.\ninfo fsevents@1.2.13: The platform "linux" is incompatible with this module.\ninfo "fsevents@1.2.13" is an optional dependency and failed compatibility check. Excluding it from installation.\n[3/4] Linking dependencies...\n.....\n[4/4] Building fresh packages...\nDone in 40.07s.\n
Run Code Online (Sandbox Code Playgroud)\n

我的期望是它应该像在我的本地计算机上一样工作:

\n
$ yarn --frozen-lockfile\nyarn install v1.22.10\n[1/4]   Resolving packages...\nsuccess Already up-to-date.\n\xe2\x9c\xa8  Done in 0.72s.\n
Run Code Online (Sandbox Code Playgroud)\n

我能否以某种方式改进我的配置以满足预期结果或预期的 GitHub Actions 行为?

\n
\n

更新:尝试使用以下路径时:

\n
          path: |\n            '**/node_modules'\n            '**/.eslintcache'\n
Run Code Online (Sandbox Code Playgroud)\n

或者:

\n
          path: |\n            'node_modules'\n            '*/node_modules'\n            '**/.eslintcache'\n
Run Code Online (Sandbox Code Playgroud)\n

缓存大小为 22 B。可能不匹配任何node_modules匹配

\n

Ten*_*eff 24

经过大量的试验和错误,从s 中删除引号path似乎已经解决了问题。并且缓存的大小几乎增加了一倍

  - uses: actions/cache@v2
    id: yarn-cache
    with:
      path: |
        **/node_modules
        **/.eslintcache
        ${{ steps.yarn-cache-dir-path.outputs.dir }}

      key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
      restore-keys: |
        ${{ runner.os }}-yarn-
Run Code Online (Sandbox Code Playgroud)

Run actions/cache@v2

Received 213909504 of 305669200 (70.0%), 203.6 MBs/sec
Received 305669200 of 305669200 (100.0%), 185.6 MBs/sec
Cache Size: ~292 MB (305669200 B)
/usr/bin/tar --use-compress-program zstd -d -xf /home/runner/work/_temp/2e2d2a1d-04d7-44c3-829e-ec4e8faf394b/cache.tzst -P -C /home/runner/work/path
Cache restored successfully
Cache restored from key: Linux-yarn-143fef95c7228810cf502305eff3be1cbc468dc8a3e0b153a4311c0250aaef6f
Run Code Online (Sandbox Code Playgroud)

Run yarn install --frozen-lockfile

yarn install v1.22.10
[1/4] Resolving packages...
success Already up-to-date.
Done in 0.96s.
Run Code Online (Sandbox Code Playgroud)

  • @Emre 我的目标是快速运行构建。如果您可以在答案中描述缓存 node_modules 的缺点并比较构建速度,我会接受它 (7认同)
  • 该帖子中有 3 个人表示不建议缓存 node_modules,但没有解释原因。此时此刻,他们让我产生了怀疑。有人可以解释是否存在问题或风险吗? (3认同)
  • 你说得对,似乎没有更好的办法。 (2认同)