Skip to content
Latchkey

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 test

Gotchas

  • A cache hit restores the browsers but not the OS libraries, so still run install-deps on a hit.
  • Keying on the version means a Playwright upgrade misses the cache and reinstalls cleanly.

Related guides

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