|
| 1 | +# Cache Playwright Dependencies Across Workflows |
| 2 | + |
| 3 | +With the help of `actions/cache@v3`, I can cache the dependency install and |
| 4 | +setup involved with using Playwright in GitHub Actions. That setup, in my |
| 5 | +experience, typically takes ~45s. When it is already cached, it is able to skip |
| 6 | +that step entirely greatly reducing the overall run time of the script. |
| 7 | + |
| 8 | +First, I need to define a cache (`playwright-cache`). Second, I need to only |
| 9 | +install the Playwright dependencies when that cache isn't available (`cache-hit |
| 10 | +!= 'true'`). |
| 11 | + |
| 12 | +Here is a striped down workflow demonstrating that. |
| 13 | + |
| 14 | +```yaml |
| 15 | +name: Playwright Script |
| 16 | +on: |
| 17 | + workflow_dispatch: |
| 18 | +jobs: |
| 19 | + Cached-Playwright-Script: |
| 20 | + runs-on: ubuntu-latest |
| 21 | + steps: |
| 22 | + - name: Check out repository code |
| 23 | + uses: actions/checkout@v3 |
| 24 | + - uses: actions/cache@v3 |
| 25 | + id: playwright-cache |
| 26 | + with: |
| 27 | + path: | |
| 28 | + ~/.cache/ms-playwright |
| 29 | + key: ${{ runner.os }}-playwright-${{ hashFiles('**/package-lock.json') }} |
| 30 | + - name: Install dependencies |
| 31 | + run: npm ci |
| 32 | + - name: Install playwright deps |
| 33 | + run: npx playwright install --with-deps chromium |
| 34 | + if: steps.playwright-cache.outputs.cache-hit != 'true' |
| 35 | + - run: node playwright-script.js |
| 36 | +``` |
| 37 | +
|
| 38 | +If I add the caching step and the cache-conditional `playwright install` steps |
| 39 | +to another workflow in this project, the cache will be available to both of |
| 40 | +them. That means they both benefit from the savings of that work having already |
| 41 | +been cached. |
| 42 | + |
| 43 | +[source](https://justin.poehnelt.com/posts/caching-playwright-in-github-actions/) |
0 commit comments