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/cachekeyed onhashFiles('**/yarn.lock'). - Run
yarn install --immutableso 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 --immutableGotchas
- 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
How to Cache Yarn Classic Dependencies in GitHub ActionsCache the Yarn 1.x global cache in GitHub Actions by resolving yarn cache dir and keying the cache on yarn.lo…
How to Cache the pnpm Store in GitHub ActionsCache the global pnpm content-addressable store in GitHub Actions by resolving the store path with pnpm store…