How do you run Cypress in GitHub Actions?
Cypress in GitHub Actions runs through the official cypress-io/github-action, which takes care of the whole run scaffold. It installs dependencies, caches the binary, starts the app (start), waits for the URL (wait-on) and runs cypress run. Without waiting, the first cy.visit() sometimes fails with a connection error because the background server is not listening yet. Secrets come from secrets, and screenshots are saved with upload-artifact using if: failure().
Cypress · CI
Green locally, but in CI the first test fails with connection refused. What is missing?
Locally a developer already has dependencies, the Cypress binary and a running app. A clean CI machine has none of it: the code, the dependencies, the Cypress binary, the server and the order they start in all have to be described. This lesson shows how the official cypress-io/github-action covers this, what it does step by step and what stays your job: secrets, artifacts, permissions.
How it works and why. The action merges what you would otherwise assemble from five commands: it installs dependencies with the command it picks by lock file (npm ci, yarn --frozen-lockfile or pnpm install --frozen-lockfile), caches the Cypress binary, starts the app in the background (start), waits for the URL (wait-on) and calls cypress run. The Cypress exit code becomes the step result, so the job goes red when tests fail. Recording to Cypress Cloud and parallelisation are switched on by the record and parallel fields of the same action.
What happens on a clean machine
Example: from "works on my machine" to a stable workflow. A typical first attempt just repeats the local commands:
# Before: server in the background, tests start at once
- run: npm ci
- run: npm start &
- run: npx cypress run
The background process only starts. Nothing promises that the server already listens on the port when the next step begins, so the first cy.visit() sometimes fails with a connection error and a re-run passes. That "random" behaviour is the sign of a race. The working version is the official action plus an artifacts step:
name: e2e
on: [pull_request]
permissions:
contents: read
jobs:
cypress:
runs-on: ubuntu-latest
timeout-minutes: 20
steps:
- uses: actions/checkout@v7
- uses: cypress-io/github-action@v7
with:
browser: chrome
start: npm start
wait-on: 'http://localhost:3000'
env:
CYPRESS_USER_PASSWORD: ${{ secrets.USER_PASSWORD }}
- uses: actions/upload-artifact@v7
if: failure()
with:
name: cypress-screenshots
path: cypress/screenshots
if-no-files-found: ignore
What matters here. wait-on blocks the step until the URL answers, so there is no race. Variables with the CYPRESS_ prefix become Cypress environment values (CYPRESS_USER_PASSWORD is read in a test with cy.env(['USER_PASSWORD']); Cypress.env() was removed in Cypress 16). A GitHub secret is masked in logs, but it is available in plain form to the test code, so use a test account with minimal rights. The upload-artifact step has if: failure(): without it the step is skipped after the previous step fails, because success() is the default. The permissions block leaves the token read-only, and timeout-minutes stops a hung run from eating the minutes quota.
Limits and alternatives. The action does not choose a test strategy for you: what runs on a PR and what runs nightly is your decision (spec, tags, separate workflows). Recording to Cypress Cloud is switched on like this:
- uses: cypress-io/github-action@v7
with:
record: true
env:
CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }}
Secrets are not passed to workflows triggered from forks through pull_request, so the record key is empty there and a job with record: true on a fork PR fails. For external contributors make a separate job without recording. If you need specific browser or OS versions, use a container instead of the runner's built-in Chrome (see the lesson on Docker images). The same scenario in Playwright needs no dedicated action:
- run: npm ci
- run: npx playwright install --with-deps
- run: npx playwright test
- uses: actions/upload-artifact@v7
if: ${{ !cancelled() }}
with:
name: playwright-report
path: playwright-report
The difference is not about a "better" tool: Playwright installs its own browsers with the install command, while Cypress takes its binary from the cache, so the whole "frame" lives in the action itself.
Typical mistakes.
npm start & npx cypress runwith no waiting: the first tests fail with a connection error and a re-run "heals" it. The cause is a race, not the tests.- An artifacts step without
if: failure()or!cancelled(): it does not run after the tests fail, so there are no screenshots exactly when you need them. - Old major versions of actions copied from old examples. Take the current ones (
checkout,upload-artifactandcypress-io/github-actionat v7) and read the action README. - Passwords in
cypress.config.tsor in the repository instead ofsecrets.
Check yourself
The workflow starts the server as npm start & and then npx cypress run. The first test fails, a re-run is green. What is it and how do you fix it?
Answer
It is a race: the background process has started but the port is not yet listening when Cypress makes its first request. Fix: start with wait-on in cypress-io/github-action, or start-server-and-test, so tests begin only after the URL answers.
An upload-artifact step stands after the tests with no condition. The tests failed and there are no screenshots in the artifacts. Why?
Answer
By default a step has the condition success() and is skipped when the previous step failed. Add if: failure() (only on failure) or if: ${{ !cancelled() }} (always, except cancellation).