Skip to content

Commit 054ee63

Browse files
authored
docs(ci): elaborate ci caching docs (#2192)
1 parent e447e9a commit 054ee63

File tree

1 file changed

+54
-17
lines changed

1 file changed

+54
-17
lines changed

docs/ci.md

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ Playwright tests can be executed to run on your CI environments. To simplify thi
66
- [CI configurations](#ci-configurations)
77
* [GitHub Actions](#github-actions)
88
* [Docker](#docker)
9-
- [Tips](#tips)
109
* [Azure Pipelines](#azure-pipelines)
1110
* [Travis CI](#travis-ci)
12-
- [Tips](#tips-1)
1311
* [CircleCI](#circleci)
1412
* [AppVeyor](#appveyor)
15-
- [Debugging browser launches](#debugging-browser-launches)
1613
- [Caching browsers](#caching-browsers)
14+
- [Exception: `node_modules` are cached](#exception-nodemodules-are-cached)
15+
- [Directories to cache](#directories-to-cache)
16+
- [Debugging browser launches](#debugging-browser-launches)
1717
<!-- GEN:stop -->
1818

1919
Broadly, configuration on CI involves **ensuring system dependencies** are in place, **installing Playwright and browsers** (typically with `npm install`), and **running tests** (typically with `npm test`). Windows and macOS build agents do not require any additional system dependencies. Linux build agents can require additional dependencies, depending on the Linux distribution.
@@ -37,7 +37,7 @@ We run [our tests](/.github/workflows/tests.yml) on GitHub Actions, across a mat
3737
3838
We have a [pre-built Docker image](docker/README.md) which can either be used directly, or as a reference to update your existing Docker definitions.
3939
40-
#### Tips
40+
Suggested configuration
4141
1. By default, Docker runs a container with a `/dev/shm` shared memory space 64MB.
4242
This is [typically too small](https://github.com/c0b/chrome-in-docker/issues/1) for Chromium
4343
and will cause Chromium to crash when rendering large pages. To fix, run the container with
@@ -72,9 +72,14 @@ For Linux agents, refer to [our Docker setup](docker/README.md) to see additiona
7272

7373
We run our tests on Travis CI over a Linux agent (Ubuntu 18.04). Use our [Travis configuration](/.travis.yml) to see list of additional dependencies to be installed.
7474

75-
#### Tips
76-
- [User namespace cloning](http://man7.org/linux/man-pages/man7/user_namespaces.7.html) should be enabled to support proper sandboxing
77-
- [xvfb](https://en.wikipedia.org/wiki/Xvfb) should be launched in order to run Chromium in non-headless mode (e.g. to test Chrome Extensions)
75+
Suggested configuration
76+
1. [User namespace cloning](http://man7.org/linux/man-pages/man7/user_namespaces.7.html)
77+
should be enabled to support proper sandboxing
78+
1. [xvfb](https://en.wikipedia.org/wiki/Xvfb) should be launched in order to run
79+
Chromium in non-headless mode (e.g. to test Chrome Extensions)
80+
1. If your project does not have `package-lock.json`, Travis would be auto-caching
81+
`node_modules` directory. If you run `npm install` (instead of `npm ci`), it is
82+
possible that the browser binaries are not downloaded. Fix this with [these steps](#exception-nodemodules-are-cached) outlined below.
7883

7984
To sum up, your `.travis.yml` might look like this:
8085

@@ -84,6 +89,8 @@ dist: bionic
8489
addons:
8590
apt:
8691
packages:
92+
# This is required to run chromium
93+
- libgbm1
8794
# These are required to run webkit
8895
- libwoff1
8996
- libopus0
@@ -100,8 +107,8 @@ addons:
100107
- libnotify4
101108
- libxslt1.1
102109
- libvpx5
103-
# This is required to run chromium
104-
- libgbm1
110+
# For headful execution
111+
- xvfb
105112
106113
# allow headful tests
107114
before_install:
@@ -139,20 +146,50 @@ We run our tests on CircleCI, with our [pre-built Docker image](docker/README.md
139146

140147
We run our tests on Windows agents in AppVeyor. Use our [AppVeyor configuration](/.appveyor.yml) to create your own.
141148

142-
## Debugging browser launches
149+
## Caching browsers
143150

144-
Playwright supports the `DEBUG` environment variable to output debug logs during execution. Setting it to `pw:browser*` is helpful while debugging `Error: Failed to launch browser` errors.
151+
By default, Playwright downloads browser binaries when the Playwright NPM package
152+
is installed. The NPM packages have a `postinstall` hook that downloads the browser
153+
binaries. This behavior can be [customized with environment variables](installation.md).
145154

146-
```
147-
DEBUG=pw:browser* npm run test
148-
```
155+
Caching browsers on CI is **strictly optional**: The `postinstall` hooks should
156+
execute and download the browser binaries on every run.
149157

150-
## Caching browsers
158+
#### Exception: `node_modules` are cached
151159

152-
By default, Playwright installs browser binaries in the following directories. This behavior can be [customized with environment variables](installation.md).
160+
Most CI providers cache the [npm-cache](https://docs.npmjs.com/cli-commands/cache.html)
161+
directory (located at `$HOME/.npm`). If your CI pipelines caches the `node_modules`
162+
directory and you run `npm install` (instead of `npm ci`), the default configuration
163+
**will not work**. This is because the `npm install` step will find the NPM
164+
package on disk, and not execute the `postinstall` step.
165+
166+
> Travis CI automatically caches `node_modules` if your repo does not have a
167+
`package-lock.json` file.
168+
169+
This behavior can be fixed with one of the following approaches:
170+
1. Move to caching `$HOME/.npm` or the npm-cache directory. (This is the default
171+
behavior in most CI providers.)
172+
1. Set `PLAYWRIGHT_BROWSERS_PATH=0` as the environment variable before running
173+
`npm install`. This will download the browser binaries in the `node_modules`
174+
directory and cache them with the package code. See [installation docs](installation.md).
175+
1. Cache the browser binaries, with the steps below.
176+
177+
#### Directories to cache
178+
179+
With the default behavior, Playwright downloads the browser binaries in the following
180+
directories:
153181

154182
- `%USERPROFILE%\AppData\Local\ms-playwright` on Windows
155183
- `~/Library/Caches/ms-playwright` on MacOS
156184
- `~/.cache/ms-playwright` on Linux
157185

158-
These locations are not covered by typical CI configurations, which cache the project `node_modules` or the [npm-cache directory](https://docs.npmjs.com/cli-commands/cache.html). To cache the browser binaries between CI runs, cache this location in your CI configuration, against a hash of the Playwright version.
186+
To cache the browser downloads between CI runs, cache this location in your CI
187+
configuration, against a hash of the Playwright version.
188+
189+
## Debugging browser launches
190+
191+
Playwright supports the `DEBUG` environment variable to output debug logs during execution. Setting it to `pw:browser*` is helpful while debugging `Error: Failed to launch browser` errors.
192+
193+
```
194+
DEBUG=pw:browser* npm run test
195+
```

0 commit comments

Comments
 (0)