Skip to content
Latchkey

How to Cache Yarn Berry with PnP in GitHub Actions

For Yarn Berry, cache the in-repo .yarn/cache of zip archives and run yarn install --immutable.

Yarn Berry (2+) stores dependency zips in .yarn/cache and, under Plug-n-Play, resolves them without a node_modules tree. Cache .yarn/cache keyed on yarn.lock so installs are an offline unpack. If the cache is committed (zero-install), you may not need this at all.

Steps

  • Enable Berry with Corepack (corepack enable).
  • Cache .yarn/cache keyed on hashFiles('**/yarn.lock').
  • Run yarn install --immutable so CI fails on lockfile drift.

Workflow

.github/workflows/ci.yml
steps:
  - uses: actions/checkout@v4
  - uses: actions/setup-node@v4
    with:
      node-version: 20
  - run: corepack enable
  - uses: actions/cache@v4
    with:
      path: .yarn/cache
      key: yarn-berry-${{ runner.os }}-${{ hashFiles('**/yarn.lock') }}
      restore-keys: |
        yarn-berry-${{ runner.os }}-
  - run: yarn install --immutable

Gotchas

  • If you commit .yarn/cache (zero-install), installs are already offline and a CI cache is redundant.
  • Do not cache .pnp.cjs; it is generated from the lockfile and regenerates cheaply.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →