Cypress 25 ๐ฒ CI Integration โ GitHub Actions and headless execution
Cypress is designed to run in CI from the start, but the default behavior of the cypress run command is tuned for a developer’s terminal, not a headless CI container. The cypress run command is the headless form, and the cypress open command is the interactive form, and a CI pipeline uses the first. The headless form starts an Electron or Chrome instance without a visible browser, records the video of the run, captures screenshots on failure, and exits with a non-zero code when a test fails, which is what the CI system reads.
The reason the topic matters is that a Cypress pipeline fails in ways a local run does not. The container lacks a display, so the browser needs the --no-sandbox flag and the Xvfb virtual framebuffer. The CI machine has fewer resources than a laptop, so the --parallel flag and the record key are what spread the tests across machines. The CI run also needs the dependency cache, because the Cypress binary is a large download and the cypress-io/github-action caches it.
This chapter covers the cypress run command, the GitHub Actions workflow that wraps it, the cypress-io/github-action step, the parallelization across machines, and the artifacts (screenshots, videos, and the test report) the pipeline uploads. The sections that follow walk through the headless run, the workflow file, the action step, and the failure artifacts.
Key point: CI integration is the cypress run command inside a GitHub Actions workflow, wrapped by the cypress-io/github-action step that handles the binary cache and the display; the --parallel flag with a record key spreads the specs across machines, and the screenshots and videos are uploaded as artifacts when a test fails.
Why CI integration exists
The headless browser problem. The cypress open command launches a visible browser window, and a CI container has no display. The cypress run command launches the browser headless, and the GitHub Actions runner provides the Xvfb virtual framebuffer that the Electron and Chrome instances need. Without the framebuffer, the browser starts and immediately crashes with a display error.
The binary download problem. The Cypress binary is a large platform-specific download, and a fresh CI container downloads it on every run. The cypress-io/github-action step caches the binary in the GitHub Actions cache, keyed by the Cypress version and the lockfile hash. The cache turns the download from a minute into a second on a warm cache.
The test duration problem. A large Cypress suite takes too long for a single machine, and the CI pipeline’s wall-clock time is the sum of the test durations. The --parallel flag with a record key splits the specs across the machines in the same GitHub Actions matrix, and the Cypress Cloud dashboard tracks the split so each spec runs exactly once.
The failure diagnosis problem. A test that fails in CI fails with no one watching the terminal. The screenshots on failure and the videos of the run are the only record, and the workflow uploads them as artifacts. Without the upload, the failure is a stack trace with no visual context, and the diagnosis is a guess.
The exit-code contract. The CI system reads the exit code of the cypress run command, and a non-zero code fails the job. The command returns a non-zero code when any test fails, when the browser crashes, or when the spec file is not found. The contract is what makes the pipeline a gate rather than a report.
The secrets problem. The record key and the test credentials are secrets, and the GitHub Actions workflow injects them as environment variables from the repository secrets. The CYPRESS_RECORD_KEY is the record key, and the test code reads the credentials from Cypress.env(), which never appears in the repository.
a. The cypress run command and the headless browser
The cypress run command is the headless form, and it is the command a CI pipeline runs. The command starts the browser without a visible window, runs the spec files in order, and exits with a code the CI system reads.
# Run all specs headless in Electron (the default browser)
npx cypress run
# Run a specific spec file
npx cypress run --spec "cypress/e2e/login.cy.ts"
# Run in Chrome with a visible browser in a container (requires Xvfb)
npx cypress run --browser chrome --headless
# Run with a record key and parallel mode (Cypress Cloud)
npx cypress run --record --key $CYPRESS_RECORD_KEY --parallel
The --spec flag narrows the run to the matching files, and the glob is what a CI job uses to split a suite by directory. The --browser flag selects the browser, and Electron is the default because it is bundled with the Cypress binary. The --record flag sends the results to Cypress Cloud, and the --key flag supplies the record key. The --parallel flag requires both the record key and the Cloud project, because the Cloud dashboard is what coordinates the machines.
The browser launch needs the sandbox flag in a container. The Chrome and Electron sandbox requires kernel privileges a Docker container does not grant, so the container runs the browser with --no-sandbox.
// cypress.config.ts
import { defineConfig } from "cypress";
export default defineConfig({
e2e: {
baseUrl: "http://localhost:3000",
video: true, // record a video of every run
screenshotOnRunFailure: true, // capture a screenshot on failure
retries: {
runMode: 2, // retry a failing test twice in `cypress run`
openMode: 0, // no retries in `cypress open`
},
},
});
The video option records the run, and the runMode retries the flaky test in CI. The retries.runMode is the CI-only retry count, and the retries.openMode is the local count. The separation is deliberate: the local run should show the failure immediately, and the CI run should retry the flaky test before failing the pipeline.
b. The GitHub Actions workflow
The GitHub Actions workflow is a YAML file in .github/workflows/, and it defines the job that checks out the repository, installs the dependencies, starts the application, and runs Cypress.
# .github/workflows/cypress.yml
name: Cypress E2E
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
cypress-run:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: 20
- name: Install dependencies
run: npm ci
- name: Start the application
run: npm run start:ci &
- name: Wait for the application
run: npx wait-on http://localhost:3000
- name: Run Cypress
uses: cypress-io/github-action@v6
with:
start: npm run start:ci
wait-on: "http://localhost:3000"
The workflow triggers on a push to main and on a pull request. The actions/checkout step clones the repository, and the actions/setup-node step installs the Node version the project needs. The npm ci step installs the exact lockfile dependencies. The wait-on step blocks until the application responds, because the Cypress run must not start before the server is ready.
The cypress-io/github-action step is the official action, and it handles the binary install, the cache, and the display. The start input launches the application, and the wait-on input blocks until the URL responds. The action replaces the manual npm run start:ci & and wait-on steps, so the shorter form is the recommendation.
# The shorter form with the official action
- name: Run Cypress
uses: cypress-io/github-action@v6
with:
start: npm run start:ci
wait-on: "http://localhost:3000"
browser: chrome
record: true
env:
CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }}
The browser input selects Chrome, and the record input enables the Cloud recording. The CYPRESS_RECORD_KEY is the secret the action reads from the environment. The start input is the command that boots the application, and the action runs it in the background and waits for the wait-on URL.
c. Parallelization and the failure artifacts
The --parallel flag splits the specs across the machines, and the split is coordinated by Cypress Cloud. Each machine runs a subset of the specs, and the Cloud dashboard tracks which spec ran on which machine so no spec runs twice.
# .github/workflows/cypress.yml (parallel form)
jobs:
cypress-run:
runs-on: ubuntu-latest
strategy:
fail-fast: false # keep other machines running on failure
matrix:
containers: [1, 2, 3] # three machines
steps:
- uses: actions/checkout@v4
- uses: cypress-io/github-action@v6
with:
start: npm run start:ci
wait-on: "http://localhost:3000"
record: true
parallel: true
env:
CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }}
The matrix.containers is the number of machines, and each machine runs the action with parallel: true. The fail-fast: false keeps the other machines running when one fails, so the full failure set is visible. The Cloud dashboard balances the specs by their recorded duration, so the machines finish at approximately the same time.
The failure artifacts are the screenshots and the videos. The cypress-io/github-action uploads them automatically when the upload-artifact step follows, and the artifacts are what the developer downloads to diagnose the failure.
- name: Upload screenshots
if: failure()
uses: actions/upload-artifact@v4
with:
name: cypress-screenshots
path: cypress/screenshots
- name: Upload videos
if: always()
uses: actions/upload-artifact@v4
with:
name: cypress-videos
path: cypress/videos
The if: failure() condition uploads the screenshots only when the job fails, and the if: always() condition uploads the videos regardless. The screenshots are the failure record, and the videos are the full run record. The path is the default output directory, and the name is the artifact label in the GitHub Actions UI.
The test report is the other artifact. The mochawesome reporter writes a JSON and HTML report, and the workflow uploads it for the dashboard.
// cypress.config.ts (reporter form)
import { defineConfig } from "cypress";
export default defineConfig({
reporter: "mochawesome",
reporterOptions: {
reportDir: "cypress/reports", // output directory
overwrite: false, // keep each spec's report separate
html: true, // generate the HTML report
json: true, // generate the JSON report
},
e2e: {
baseUrl: "http://localhost:3000",
},
});
The reporter is the Mocha reporter Cypress wraps, and the mochawesome reporter writes a file per spec. The overwrite: false keeps each spec’s report, and the html: true generates the human-readable report. The CI workflow uploads the cypress/reports directory as the test report artifact.
Complete Example Session
# ============================================
# PART 1: THE WORKFLOW TRIGGER
# ============================================
name: Cypress E2E
on:
push:
branches: [main]
pull_request:
branches: [main]
# ============================================
# PART 2: THE JOB AND THE RUNNER
# ============================================
jobs:
cypress-run:
runs-on: ubuntu-latest
# ============================================
# PART 3: THE CHECKOUT AND NODE
# ============================================
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: 20
# ============================================
# PART 4: THE DEPENDENCY CACHE
# ============================================
- name: Install dependencies
run: npm ci
# ============================================
# PART 5: THE CYPRESS ACTION STEP
# ============================================
- name: Run Cypress
uses: cypress-io/github-action@v6
with:
start: npm run start:ci
wait-on: "http://localhost:3000"
browser: chrome
# ============================================
# PART 6: THE RECORD SECRET
# ============================================
env:
CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }}
# ============================================
# PART 7: THE SCREENSHOT ARTIFACT
# ============================================
- name: Upload screenshots
if: failure()
uses: actions/upload-artifact@v4
with:
name: cypress-screenshots
path: cypress/screenshots
# ============================================
# PART 8: THE VIDEO ARTIFACT
# ============================================
- name: Upload videos
if: always()
uses: actions/upload-artifact@v4
with:
name: cypress-videos
path: cypress/videos
# ============================================
# PART 9: THE PARALLEL MATRIX
# ============================================
strategy:
fail-fast: false
matrix:
containers: [1, 2, 3]
# ============================================
# PART 10: THE PARALLEL ACTION STEP
# ============================================
- name: Run Cypress in parallel
uses: cypress-io/github-action@v6
with:
start: npm run start:ci
wait-on: "http://localhost:3000"
record: true
parallel: true
env:
CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }}
The ten parts covered the trigger, the runner, the checkout, the dependency install, the action step, the record secret, the screenshot artifact, the video artifact, the parallel matrix, and the parallel action step.
Quick Reference
The cypress run Flags
| Flag | Effect |
|---|---|
--spec | Run the matching spec files |
--browser | Select the browser |
--record | Send the results to Cypress Cloud |
--key | Supply the record key |
--parallel | Split the specs across machines |
The Action Inputs
| Input | Effect |
|---|---|
start | Boot the application |
wait-on | Block until the URL responds |
browser | Select the browser |
record | Enable the Cloud recording |
parallel | Split the specs across machines |
The Artifact Types
| Artifact | Path | Upload Condition |
|---|---|---|
| Screenshots | cypress/screenshots | failure() |
| Videos | cypress/videos | always() |
| Report | cypress/reports | always() |
The Config Options
| Option | Effect |
|---|---|
video | Record the run |
screenshotOnRunFailure | Capture the failure screenshot |
retries.runMode | Retry the test in CI |
retries.openMode | Retry the test locally |
Best Practices
โ Do This:
# Use the official action
uses: cypress-io/github-action@v6 # โ
# Wait for the application before the run
wait-on: "http://localhost:3000" # โ
# Keep the record key in the secrets
CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }} # โ
# Upload the screenshots on failure
if: failure() # โ
# Retry the flaky test in CI only
retries: { runMode: 2, openMode: 0 } # โ
โ Don’t Do This:
# Run the interactive form in CI
run: npx cypress open # โ
# Start the run before the server responds
run: npx cypress run # โ
# Commit the record key
CYPRESS_RECORD_KEY: "abc-123" # โ
# Skip the failure artifacts
# (no upload step) # โ
# Retry the test locally
retries: { openMode: 3 } # โ
Common Pitfalls
| Pitfall | Why It Happens | Fix |
|---|---|---|
| Browser crash on start | The container lacks a display | Xvfb via the action |
| Slow first run | The binary is not cached | The action cache |
| Specs run twice | --parallel without a record key | Add the record key |
| No failure context | The artifacts are not uploaded | The upload step |
| Flaky test fails the pipeline | No CI retry | retries.runMode |
| Secret in the repository | The key is hardcoded | Repository secrets |
Real-World Examples
1. Headless run
npx cypress run --browser chrome --headless
2. Run a single spec
npx cypress run --spec "cypress/e2e/login.cy.ts"
3. Record and parallel
npx cypress run --record --key $CYPRESS_RECORD_KEY --parallel
4. The official action step
uses: cypress-io/github-action@v6
with:
start: npm run start:ci
5. The matrix
strategy:
matrix:
containers: [1, 2, 3]
6. The screenshot upload
if: failure()
uses: actions/upload-artifact@v4
7. The CI retry
retries: { runMode: 2, openMode: 0 }
8. The record secret
env:
CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }}
9. The reporter
reporter: "mochawesome"
10. The wait-on
wait-on: "http://localhost:3000"
Visual
The CI Pipeline Flow
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ GITHUB ACTIONS PIPELINE โ
โ โ
โ push / pull request โ
โ โ โ
โ โผ โ
โ checkout โโโบ setup node โโโบ npm ci โ
โ โ โ
โ โผ โ
โ start app โโโบ wait-on โโโบ cypress run โ
โ โ โ
โ โผ โ
โ screenshots / videos / report โโโบ artifacts โ
โ โ
โ exit code 0 โโโบ pass exit code 1 โโโบ fail โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
The Parallel Split
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ CYPRESS CLOUD COORDINATION โ
โ โ
โ spec A โโโบ machine 1 โโโบ result โ
โ spec B โโโบ machine 1 โโโบ result โ
โ spec C โโโบ machine 2 โโโบ result โ
โ spec D โโโบ machine 3 โโโบ result โ
โ โ
โ the Cloud dashboard tracks which spec ran where โ
โ each spec runs exactly once โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
The Artifact Upload
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ cypress run โ
โ โ โ
โ โโโโบ cypress/screenshots (on failure) โ
โ โ โ โ
โ โ โผ โ
โ โ upload-artifact (if: failure()) โ
โ โ โ
โ โโโโบ cypress/videos (always) โ
โ โ โ
โ โผ โ
โ upload-artifact (if: always()) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
The Binary Cache
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ COLD CACHE WARM CACHE โ
โ download the binary read the cache โ
โ ~60 seconds ~2 seconds โ
โ โ
โ the cache key: โ
โ cypress version + lockfile hash โ
โ โ
โ a dependency change invalidates the cache โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Summary
| Item | Value |
|---|---|
| CI command | cypress run |
| Official action | cypress-io/github-action@v6 |
| Parallel flag | --parallel with --record |
| Record secret | CYPRESS_RECORD_KEY |
| Screenshot path | cypress/screenshots |
| Video path | cypress/videos |
| Screenshot upload | if: failure() |
| CI retry | retries.runMode |
| Matrix | strategy.matrix.containers |
Key takeaways:
- The CI command is
cypress run. The headless form starts the browser without a display, runs the specs, and exits with a non-zero code when a test fails. Thecypress openform is for local development only. - The
cypress-io/github-actionstep handles the environment. It installs the binary, caches it by version and lockfile hash, provides the Xvfb display, and runs thestartandwait-oninputs. The manual steps are error-prone, and the action is the recommendation. - The
--parallelflag requires a record key. The Cloud dashboard coordinates the split across the matrix machines, so each spec runs exactly once. Thefail-fast: falsekeeps the other machines running when one fails. - The artifacts are the failure record. The screenshots upload on failure, and the videos upload always. The report is the third artifact. Without the upload, the CI failure is a stack trace with no visual context.
- The retry is the CI-only setting. The
retries.runModeretries the flaky test in CI, and theretries.openModekeeps the local run immediate. The separation is what keeps the local feedback fast and the CI pipeline stable.
Remember: The CI pipeline is the cypress run command inside a GitHub Actions workflow, wrapped by the official action that handles the binary cache, the display, and the application boot. The parallel flag with the record key spreads the specs across the matrix, and the screenshots and videos are the artifacts that make a CI failure diagnosable. The retries are for CI only, the record key lives in the repository secrets, and the exit code is what makes the pipeline a gate rather than a report.
Stop using slow, ad-bloated tool sites! ๐คฎ
๐ Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
โ
Finance (Mortgage, Interest, Inflation)
โ
Tech (Base64, JSON, Dev Suite, IP)
โ
Health (BMI, BMR, TDEE)
โ
Productivity (Timer, Workspace, QR)
โก๏ธ Fast & Private
๐ No data leaves your device
๐ 100% Free
๐ Use it now: https://tools.kandz.me
๐ Bookmark itโyouโll need it later!