Skip to content

Commit fa20b1a

Browse files
committed
Add Cache Playwright Dependencies Across Workflows as a GitHub Actions TIL
1 parent 00481bb commit fa20b1a

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

Diff for: README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
1010

1111
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
1212

13-
_1310 TILs and counting..._
13+
_1311 TILs and counting..._
1414

1515
---
1616

@@ -344,6 +344,7 @@ _1310 TILs and counting..._
344344

345345
### GitHub Actions
346346

347+
- [Cache Playwright Dependencies Across Workflows](github-actions/cache-playwright-dependencies-across-workflows.md)
347348
- [Capture An Output Value For Use In A Later Step](github-actions/capture-an-output-value-for-use-in-a-later-step.md)
348349
- [Reference An Encrypted Secret In An Action](github-actions/reference-an-encrypted-secret-in-an-action.md)
349350

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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

Comments
 (0)