How to Cache Playwright Browser Binaries in GitHub Actions
Cache the browser download directory keyed on the Playwright version, then install browsers only on a cache miss.
Restore ~/.cache/ms-playwright with a key built from the installed Playwright version, and run install --with-deps only when the cache does not hit.
Workflow
.github/workflows/ci.yml
jobs:
e2e:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with: { node-version: 20, cache: npm }
- run: npm ci
- id: pw
run: echo "version=$(npx playwright --version | cut -d' ' -f2)" >> "$GITHUB_OUTPUT"
- uses: actions/cache@v4
id: cache
with:
path: ~/.cache/ms-playwright
key: playwright-${{ steps.pw.outputs.version }}
- if: steps.cache.outputs.cache-hit != 'true'
run: npx playwright install --with-deps
- if: steps.cache.outputs.cache-hit == 'true'
run: npx playwright install-deps
- run: npx playwright testGotchas
- A cache hit restores the browsers but not the OS libraries, so still run
install-depson a hit. - Keying on the version means a Playwright upgrade misses the cache and reinstalls cleanly.
Related guides
How to Run Playwright Tests in CI on GitHub ActionsRun Playwright end-to-end tests on GitHub Actions by installing browsers with their OS dependencies, running…
How to Run Only Affected E2E Tests in GitHub ActionsSkip unaffected E2E specs in GitHub Actions using Playwright --only-changed against the base branch, running…