cache は node_modules を取らない

setup-node の cache はグローバルストアを取る。node_modules を取りたいなら actions/cache を自前で。

  • #GitHub Actions
  • #Node.js

actions/setup-nodecache は npm の .npm、Yarn のキャッシュ、pnpm のストアを取る。node_modules ではない。

yaml
- uses: pnpm/action-setup@v4
  with:
    run_install: false
- uses: actions/setup-node@v4
  with:
    node-version-file: package.json
    cache: pnpm
- run: pnpm install

グローバルストア向き: pnpm や Yarn v2 以降(ハードリンク)、複数 Node バージョンでのテスト。 node_modules 向きかもしれない: npm や Yarn v1(コピー)。

node_modules を自前で取るなら、key に OS / arch / Node バージョン / lockfile ハッシュを入れる。曖昧だと不整合、厳しすぎるとキャッシュが肥大化する。

yaml
- uses: actions/setup-node@v4
  id: node
  with:
    node-version: 20
- uses: actions/cache@v4
  id: cache
  with:
    key: ${{ runner.arch }}-${{ runner.os }}-node-${{ steps.node.outputs.node-version }}-yarn-${{ hashFiles('**/yarn.lock') }}
    path: node_modules
- run: yarn install --frozen-lockfile
  if: steps.cache.outputs.cache-hit != 'true'
編集