Kaizen Today I Learned by Ville Säävuori

Running Playwright in GitLab CI

Had some trouble getting Playwright to run properly on GitLab CI so decided to document my learnings here.

Playwright CI documentation was helpful for tracking and tackling the problems:

  • Setting DEBUG=pw:browser* environment variable will output debug logs during the browser install which is really helpful.
  • Most issues seem to relate to memory handling and consumption. Adding --disable-dev-shm-usage flag for Chromium should fix issues with environments like Docker with no access or limited size /dev/shm. Launch the broser like this:
    const browser = await playwright.chromium.launch({
      args: ['--disable-dev-shm-usage']
    });
    
  • Caching is also a common issue in CI environments. Setting PLAYWRIGHT_BROWSERS_PATH=0 environment variable makes Playwright store the browser binaries inside node_modules which resolves most issues.
  • Microsoft also offers pre-build Docker images which should be an easy way to run inside GitLab CI.

Use only one browser

By default Playwright will install Chromium, Firefox and Webkit. If you don’t need all of these, there are specific versions of the package as well. As far as I can tell the only difference between the full package is the number of included browser engines.

Force install browsers

To force broser install, run:

npx playwright-cli install

Running in Gitlab CI

After trying out multiple different things, I ended up switching the runner to use mcr.microsoft.com/playwright:bionic Docker image which is not optimal as I run several other scripts in the same pipeline and wanted to optimize the install into one simple and lightweight step. This extended the pipeline runtime by about two minutes, but it works now.

I did leave the install script in which still produces an error (that it doesn’t do locally):

$ npx playwright-cli install
(node:62) UnhandledPromiseRejectionWarning: Error: EACCES: permission denied, mkdir '/builds/uninen/personal-data/.npm/_npx/20/lib/node_modules/playwright-cli/node_modules/playwright/.local-browsers'

I might revisit this sometime in the future when things mature a bit (and my nerves get a bit longer). It works, that’s enough for me for now.

Running in Vercel

Creating the browser instance with --disable-dev-shm-usage flag and adding the PLAYWRIGHT_BROWSERS_PATH=0 environment variable is all you need for Vercel.

Tagged with , , , ,

Published . Last modified .