QA Interview Hub · Knowledge base · Quizzes · Українська

Cypress › CI, parallelization, reports

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

1. checkoutthe PR code on a clean machine2. install + cachenpm ci by lock file, Cypress binary3. start in backgroundnpm start, process lives until job end4. wait-on URLtests only after the server answers5. cypress runexit code = job result6. artifactsscreenshots if step 5 failed
Figure 1. Step 4 is the one most often forgotten: without it tests may start before the server listens on the port.

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 run with 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-artifact and cypress-io/github-action at v7) and read the action README.
  • Passwords in cypress.config.ts or in the repository instead of secrets.

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).

In the interview. Junior: I use the official cypress-io/github-action: it installs dependencies with a cache, starts the app, waits for it and runs the tests. I pass secrets from the repository secrets and save screenshots as artifacts on failure. Tests run on every pull request. Middle/Senior: I look at the reliability of the frame: wait-on instead of a race, minimal permissions, timeout-minutes, artifacts with the right condition, a pinned image or browser versions. I switch on Cloud recording separately, and it is unavailable for fork PRs because there are no secrets. What runs on a PR and what runs nightly I decide by feedback time.

More articles in this chapter