Skip to content
TestLine Menu
TestLine
automation

Playwright tests in GitHub Actions and GitLab CI: step-by-step setup

A ready-made setup for GitHub Actions and GitLab CI, saving reports, keeping the URL and passwords in secrets, and splitting tests across several machines.

author
Artur Machowski, Senior QA Engineer
published
reading
7 min

Short answer

You run Playwright tests in CI in five steps: check out the code, install dependencies (npm ci), install browsers (npx playwright install --with-deps) or use the official Docker image, run the tests (npx playwright test) and save the report as an artifact, also when the tests fail. Keep the environment URL and passwords in CI secrets. Run the smoke test on every pull request and full regression every night, and once the suite grows, split it across several machines with the --shard option.

Playwright configuration for CI

First, playwright.config.ts, shared by both systems:

import { defineConfig } from "@playwright/test";

export default defineConfig({
  testDir: "./tests",
  retries: process.env.CI ? 2 : 0,
  workers: process.env.CI ? 2 : undefined,
  reporter: process.env.CI ? [["html", { open: "never" }], ["github"]] : "html",
  use: {
    baseURL: process.env.BASE_URL,
    trace: "on-first-retry",
    screenshot: "only-on-failure",
  },
});

BASE_URL comes from CI secrets, so you can run the same tests against staging and locally. The github reporter adds annotations next to failures in GitHub Actions. In GitLab CI keep just html.

GitHub Actions

The .github/workflows/playwright.yml file:

name: Playwright
on:
  pull_request:
  schedule:
    - cron: "0 2 * * *"

jobs:
  test:
    runs-on: ubuntu-latest
    timeout-minutes: 30
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: npm
      - run: npm ci
      - run: npx playwright install --with-deps
      - name: Smoke on pull request
        if: github.event_name == 'pull_request'
        run: npx playwright test --grep @smoke
        env:
          BASE_URL: ${{ secrets.STAGING_URL }}
          TEST_PASSWORD: ${{ secrets.TEST_PASSWORD }}
      - name: Full nightly regression
        if: github.event_name == 'schedule'
        run: npx playwright test
        env:
          BASE_URL: ${{ secrets.STAGING_URL }}
          TEST_PASSWORD: ${{ secrets.TEST_PASSWORD }}
      - uses: actions/upload-artifact@v4
        if: ${{ !cancelled() }}
        with:
          name: playwright-report
          path: playwright-report/
          retention-days: 14

You add the STAGING_URL and TEST_PASSWORD secrets in the repository settings, under Secrets and variables. The !cancelled() condition saves the report also when the tests fail.

GitLab CI

The .gitlab-ci.yml file:

stages: [test]

.playwright:
  image: mcr.microsoft.com/playwright:v1.55.0-noble
  stage: test
  before_script:
    - npm ci
  artifacts:
    when: always
    paths: [playwright-report/]
    expire_in: 14 days

smoke:
  extends: .playwright
  script:
    - npx playwright test --grep @smoke
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"

regression:
  extends: .playwright
  script:
    - npx playwright test
  rules:
    - if: $CI_PIPELINE_SOURCE == "schedule"

The image version must match the @playwright/test version in package.json, otherwise Playwright won’t find the browsers. You add BASE_URL and TEST_PASSWORD under Settings, CI/CD, Variables, as protected and masked variables. You set the nightly regression schedule under Build, Pipeline schedules.

Splitting across several machines

When full regression takes longer than 15 minutes or so, split it into parts. In GitHub Actions, with a matrix:

    strategy:
      fail-fast: false
      matrix:
        shard: [1/4, 2/4, 3/4, 4/4]
    steps:
      # ... the same steps as above
      - run: npx playwright test --shard=${{ matrix.shard }}

In GitLab CI, with parallel: 4 and --shard=$CI_NODE_INDEX/$CI_NODE_TOTAL. To get one combined report, use the blob reporter in each part and merge them with npx playwright merge-reports.

The most common problems

  • tests pass locally but fail in CI: a different resolution, a slower machine, missing data on staging; the trace from the retry usually shows the cause;
  • no report after failed tests: missing when: always or !cancelled();
  • passwords in the repository: always go through secrets and process.env;
  • full regression on every pull request: developers wait tens of minutes and start skipping it; run only the smoke test on PRs.

How to split tests into smoke and full regression is covered in regression in CI, and what to do with tests that pass one run and fail the next in flaky tests.

How I can help

As part of test automation I build a suite of 20-30 Playwright E2E tests together with the CI setup in GitHub Actions or GitLab CI, reports and a maintenance guide. That usually costs PLN 12,000-20,000 net and takes 3-5 weeks. Describe your project and CI system in the contact form.

Frequently asked questions

How do you run Playwright tests in GitHub Actions?

Add a .github/workflows/playwright.yml file with these steps: check out the code, install Node.js, npm ci, npx playwright install --with-deps and npx playwright test. Save the HTML report as an artifact, also when the tests fail.

How do you run Playwright tests in GitLab CI?

The simplest way is the official Docker image mcr.microsoft.com/playwright in the same version as Playwright in your project. In the job, run npm ci and npx playwright test, and save the report as an artifact with when: always.

Where should the environment URL and test passwords live?

In CI secrets: Secrets in GitHub Actions, or protected and masked variables in GitLab CI. Read them in the test code via process.env. Never put passwords in the repository or in a config file.

How do you speed up Playwright tests in CI?

Run tests in parallel and split them across several machines with the --shard option, e.g. 1/4, 2/4 and so on. Log in once via the API and save the session state instead of logging in through the form in every test.

How many retries should you set in CI?

Usually 1-2 in CI and 0 locally. Retries save you from a one-off glitch, but tests that pass only on retry need fixing, because the report marks them as flaky.

Got a release in a few weeks?

Describe your project in the form. I'll reply with questions about scope or an initial test plan.