A workflow skeleton for Playwright in GitHub Actions
A Playwright workflow is YAML in .github/workflows/ with triggers and jobs that run in parallel by default, each on its own runner. A job is made of steps. A typical skeleton is checkout, Node, npm ci, browsers, tests and a report upload with the !cancelled() condition so it is saved even after a failure. timeout-minutes and minimal permissions are added.
CI/CD · GitHub Actions
Tests are green locally and red in Actions, with no report. What is a workflow made of?
A QA Engineer adds the first Playwright workflow to a repository. The tests fail, and the report button is empty: the artifact upload step did not run, because it came after a red step. A small detail like this if condition can cost an hour until you know how Actions decides what to run. This lesson walks through the skeleton of a workflow and its usual traps.
How it works and why. A workflow is a YAML file in .github/workflows/. It consists of jobs, which run in parallel by default, each on its own runner (a clean virtual machine). A job is a sequence of steps on one machine with a shared file system. A step is either run: (a shell command) or uses: (a ready-made action). Data does not flow between jobs on its own: that takes artifacts, a cache or outputs.
Workflow structure
Figure 1. Parallelism exists between jobs, not between steps.
Triggers (on:): pull_request (a PR), push with a branch filter, schedule (cron) and workflow_dispatch (a manual run). Cron runs in UTC by default, such runs start only from the default branch and may be late at peak hours. Another zone is set with a timezone key holding an IANA name.
A step after failing tests
Wrong
The default condition
A step has the success() condition: after a red test step it is skipped, and the report you need most is missing.
Right
if: !cancelled()
The step runs after failing tests, but not after a run was cancelled by hand or by concurrency.
Figure 2. The condition on the report upload step decides whether you see the cause of a failure.
Example: a working skeleton. Everything needed for a first Playwright run:
What matters here. timeout-minutes protects against hangs (by default a job may run up to 360 minutes). permissions: contents: read limits the GITHUB_TOKEN to a minimum. npm ci installs dependencies strictly from the lock file. cache: npm caches the global npm cache. playwright install --with-deps installs a browser with system libraries. Playwright's CI config (workers, retries) is in the lesson “CI: sharding, Docker and fast reruns”.
A nightly run on Kyiv time:
on:
schedule:
# every day at 02:00 Kyiv time
- cron: '0 2 * * *'timezone: 'Europe/Kyiv'
Limits and alternatives. Actions can be pinned by a tag (@v7) or by a full commit SHA: a SHA protects against a moved tag, a tag is easier to update. Action versions change, so check them on the releases page before use and update them along with dependencies. For constant runs on your own hardware there are self-hosted runners, but then the team keeps the environment clean. Sharding and matrix are in the next lesson: “Cache, matrix, concurrency and sharding”.
Typical mistakes.
No timeout-minutes: a hung test holds a runner for six hours.
A report upload without !cancelled(): no report after a failure.
Token permissions wider than needed: permissions not set.
Cron expected in local time while it runs in UTC.
Expecting one job to see another job's files: artifacts are needed.
Check yourself
The tests failed, the npx playwright test step is red and the next upload-artifact is "skipped". Why, and how to fix it?
Answer
A step's default condition is success(): after a failed step the next ones are skipped. Add if: ${{ !cancelled() }} so the upload runs after failing tests.
Will a second job see files created by the first?
Answer
No: each job runs on its own runner. Data is passed through artifacts, a cache or outputs, and order is set with needs.
Related lessons: “Cache, matrix, concurrency and sharding”, “Secrets, service containers, GitLab CI and Jenkins”, “What to run on a PR, on merge and nightly”.
In an interview.Junior: A workflow is YAML with triggers and jobs, and a job is made of steps. For Playwright I do checkout, set up Node, run npm ci, install browsers, run the tests and upload the report. Middle/Senior: Jobs run on separate runners, so data moves between them through artifacts. I set timeout-minutes and minimal permissions, upload the report with !cancelled(), and remember that cron runs in UTC unless timezone is set. I check action versions on the releases page.
Cache, matrix, concurrency and sharding
The npm cache shortens install, a matrix multiplies a job by parameters, sharding splits e2e across those jobs, and concurrency cancels outdated PR runs. The cache is tied to the lock file, shards run with --shard and a separate job merges their blob reports, with unique artifact names. Cancellation is not enabled on main.
CI/CD · GitHub Actions
Four shards gave x2.5, not x4, and every push runs a stale run. Why?
A team split e2e into four shards and hoped for a four-fold speedup. A run dropped from 20 minutes to eight, not five: one shard got the heavy files and install in every job added its own two minutes. On top of that the author pushes three commits in a row, and three full runs compete for runners. This lesson is about the cache, matrix, concurrency and sharding that fix it.
How it works and why.Dependency cache.actions/setup-node with cache: npm caches the global npm cache (~/.npm) under a key derived from package-lock.json. npm ci still runs, but takes packages from disk. Caching node_modules itself is usually not advised: npm ci deletes and recreates it. For custom caches there is actions/cache: the key must change with the lock file, otherwise the cache goes stale.
Playwright browsers live in ~/.cache/ms-playwright (on Linux). The Playwright docs advise against caching them: restoring the cache takes about as long as downloading, and system libraries sit outside that directory and are not restored by the cache. If a cache is still wanted, the key must change with the Playwright version (in the example through the hash of package-lock.json, which records it), and after a cache hit the libraries are installed separately.
Matrix. One job multiplied by parameters: browsers, Node versions, shard numbers. Each combination is a separate job on a separate runner. By default fail-fast: true: when one combination fails, the rest, running or queued, are cancelled. For a full picture set fail-fast: false. max-parallel caps parallel jobs, and the limit of concurrent jobs depends on the account plan.
Shards and report merge
Figure 1. Without the merge you have four separate reports instead of one.
Example: sharding with a report merge. Each shard calls --shard=i/N and leaves a blob report. For this, set the blob reporter in the Playwright config on CI. Artifact names within a run must be unique, so the shard number is part of the name.
Evenness depends on how Playwright distributes tests: by default sharding goes by file, and with fullyParallel: true by individual test, which evens the load. More in the lesson “Parallelism, workers and sharding”.
Concurrency. So that a new push to a PR cancels a stale run, set a group:
On main cancelling is usually turned off: the result for merged commits should not be lost.
Limits and alternatives. A cache does not speed up a run if restore and save take longer than install: on small projects the gain is minimal. Caches in a repository have a size limit and the oldest are evicted. Shards raise minute costs: four shards of eight minutes each is 32 billable minutes. Large suites have dynamic balancing in third-party services, but to start fullyParallel is enough.
Typical mistakes.
The same artifact name in all shards: the run fails on a conflict.
Caching node_modules instead of the npm cache.
A cache key without a lock file hash.
Concurrency on main with cancel-in-progress: results get lost.
Blob reports not merged, so the full picture is missing.
Check yourself
Why is install-deps still needed after a cache hit for Playwright browsers?
Answer
The cache restores only the directory with browsers. System libraries (fonts, shared libs) live in the OS and are not kept between runs, so they are installed separately.
Why must a shard's artifact name contain its number?
Answer
Artifact names within a run must be unique: the same name causes a conflict. With a number in the name every shard leaves its own blob report, and they are merged later.
Related lessons: “A workflow skeleton for Playwright in GitHub Actions”, “How to keep PR feedback within 10 minutes”, “Reports and artifacts: what to keep and how to share”.
In an interview.Junior: The npm cache shortens install, a matrix runs one job with different parameters in parallel, and concurrency cancels stale PR runs. Middle/Senior: I shard e2e through a matrix with --shard, turn on fullyParallel for evenness and merge blob reports in a separate job. I tie the cache key to the lock file, make artifact names unique, turn fail-fast off where a full picture is needed, and do not enable cancelling on main. I also remember minute cost: shards are not free.
Secrets, service containers, GitLab CI and Jenkins
Workflows read secrets through secrets, and the pull_request event does not pass them to PRs from forks. pull_request_target runs in the context of the base repository's default branch with access to secrets, so running PR code from it is dangerous. Untrusted values go through env, and a database is added as a service container with a healthcheck. GitLab CI and Jenkins have equivalents of these, with different syntax and details.
CI/CD · GitHub Actions
In a fork PR a test fails on "no token", and a PR title runs as a command. What is wrong?
API tests need a token and a database. On a colleague's PR everything is green, while on a fork PR tests fail: there is no token in the run. Someone suggests "just turn on pull_request_target", because then the secrets show up. That advice opens a door to secret theft. This lesson is about secrets, service containers and how the same ideas look in GitLab CI and Jenkins.
How it works and why.Secrets are stored in Settings → Secrets and variables (repository, environment or organization level) and read as ${{ secrets.API_TOKEN }}. They are masked in logs, but the mask looks for the exact string, so a modified or split secret can leak. Non-sensitive settings (an environment URL, flags) are vars, not secrets. For PRs from forks the pull_request event gets no secrets and a read-only token: that protects against theft. The pull_request_target event runs in the context of the base branch with access to secrets, so checking out and running foreign code from it is dangerous.
The second trap: an expression ${{ … }} is substituted into the command text before the shell sees it. If it holds text the PR author controls (a title, a branch), the title can contain "; curl … and it will run as code. So untrusted values are passed through env and read as a shell variable.
A value in a command
Wrong
An expression in run text
The PR title is pasted into the command line and can become part of the script. That is an injection.
Right
The value through env
The value sits in an environment variable and is read as a quoted $PR_TITLE: it is data, not code.
Figure 1. Secrets, too, are better passed through env only to the step that needs them.
The --health-* options make the job wait until the database is actually ready, not just started. If the job runs on the runner, the service is reached through localhost and a mapped port. If the job itself runs in a container (container:), the service address is its name (postgres) and the internal port. The same principle with docker compose is in the lesson “docker compose for e2e: app + db + tests”.
Other systems: the same ideas. Syntax differs, not the approach: triggers, cache, parallelism, artifacts, secrets and service containers.
GitLab CI: a .gitlab-ci.yml file, stages, parallel: 4 for shards and parallel:matrix for parameter combinations, cache:, artifacts: when: always, services:, CI/CD variables.
Jenkins: a Jenkinsfile with stages and parallel, a post { always { ... } } block for reports, agents instead of runners, credentials instead of secrets.
# .gitlab-ci.ymlstages: [test]
e2e:
stage: test
image: mcr.microsoft.com/playwright:v1.65.1-noble
parallel: 4script:
- npm ci
- npx playwright test --shard=$CI_NODE_INDEX/$CI_NODE_TOTAL
artifacts:
when: always
paths: [playwright-report/]
Limits and alternatives. For cloud access, short-lived tokens through OIDC are better than long-lived keys in secrets. Environment secrets can be guarded by a required approval before a run. With the Playwright Docker image on GitLab, the tag version is bumped together with @playwright/test. Most tests need no real secrets: for API tests on a PR a test token of an ephemeral environment is often enough.
Typical mistakes.
pull_request_target with a checkout of fork code: secrets in the hands of foreign code.
A PR title or branch directly in run.
A secret in the env of a whole job instead of one step.
A service without a healthcheck: tests start before the database is ready.
Sensitive and non-sensitive values mixed in secrets.
Check yourself
Why are secrets unavailable in a fork PR, and why is pull_request_target not a safe cure?
Answer
Code in a fork PR is foreign and with secrets it could exfiltrate them. So pull_request does not pass them. pull_request_target gives secrets but runs in the base branch context: if it checks out and runs PR code, foreign code gets access to secrets.
A job runs on the runner and the database sits in a service container. What is the database address in DATABASE_URL?
Answer
localhost and the mapped port from ports. If the job itself ran in a container, the address would be the service name with the internal port.
Related lessons: “A workflow skeleton for Playwright in GitHub Actions”, “Cache, matrix, concurrency and sharding”, “docker compose for e2e: app + db + tests”.
In an interview.Junior: Secrets are stored in repository settings and read through secrets in a workflow, and masked in logs. For a database in tests I use a service container. Middle/Senior: I pass secrets through env only to the step that needs them and never put untrusted values straight into run. I do not use pull_request_target with foreign code and replace long-lived keys with OIDC. I add a healthcheck to the service container. The ideas carry over between GitHub Actions, GitLab CI and Jenkins, only the syntax changes.