Cypress in Jenkins or GitLab CI
In GitLab CI and Jenkins, Cypress runs with the same npx cypress run in an official image that ships with browsers; only the syntax changes. The steps are the same: npm ci, cache, start the app and wait for it, run, and save artifacts even after a failure. Secrets come from CI/CD variables or credentials, and a JUnit report shows results in the test tab.
Cypress · CI
The pipeline is in GitLab CI or Jenkins, not GitHub Actions. What changes and what stays?
GitHub Actions has a ready action that takes over most steps. GitLab CI and Jenkins do not, and it seems that everything has to be invented anew. In reality only the syntax changes: the same steps (image, dependencies, server, run, artifacts) are written in different words. The lesson shows what is shared and what is specific, with two working examples.
How it works and why. At the core is npx cypress run with the needed parameters: browser, config, specs, environment variables. The most convenient is an official cypress/included or cypress/browsers image where browsers and system libraries are already installed. Then four things are needed that the action does in Actions: install dependencies (npm ci), cache the npm cache and the Cypress binary, start the app and wait for it (start-server-and-test or wait-on) and, after the run, keep artifacts even on failure. Secrets are passed through CI/CD variables (GitLab) or credentials (Jenkins) and do not lie in the repository.
The same in three systems:
| Need | GitHub Actions | GitLab CI | Jenkins |
|---|---|---|---|
| Image with browsers | container.image | image: | Docker agent |
| Cache | actions/cache | cache: | a plugin or an agent volume |
| Secrets | secrets.* | CI/CD variables | credentials |
| Parallelism | strategy.matrix | parallel: | parallel stages |
| Report in a tab | an artifact | artifacts:reports:junit | the junit step |
Example: GitLab CI. This is a separate job in .gitlab-ci.yml. The cache is tied to the lock file, and the cache folders are moved into the working directory because GitLab caches only paths inside the project. parallel: 3 creates three copies of the job, each with its own CI_NODE_INDEX:
cypress:
image: cypress/browsers:24.22.1
variables:
CYPRESS_CACHE_FOLDER: $CI_PROJECT_DIR/cache/Cypress
npm_config_cache: $CI_PROJECT_DIR/.npm
cache:
key:
files: [package-lock.json]
paths: [.npm, cache/Cypress]
parallel: 3
script:
- npm ci
# CI_NODE_INDEX counts from 1, cypress-split from 0
- export SPLIT=$CI_NODE_TOTAL SPLIT_INDEX=$((CI_NODE_INDEX - 1))
- npx start-server-and-test start http://localhost:3000 "cypress run"
artifacts:
when: always
expire_in: 7 days
paths: [cypress/screenshots, cypress/videos]
reports:
junit: results/junit-*.xml
artifacts:when: always keeps the files after a failure too, and reports:junit shows results in the tests tab of the merge request. The split of specs between the three copies is done here by the cypress-split plugin, as in the lesson on parallelism.
Example: Jenkins. In Jenkins it is a stage in the Jenkinsfile, and the pipeline runs in a Docker agent:
pipeline {
agent { docker { image 'cypress/browsers:24.22.1' } }
environment {
// a secret from Jenkins credentials, masked in logs
CYPRESS_USER_PASSWORD = credentials('e2e-user-password')
}
stages {
stage('Install') { steps { sh 'npm ci' } }
stage('E2E') {
steps {
sh 'npx start-server-and-test start http://localhost:3000 "cypress run"'
}
}
}
post {
always {
junit 'results/*.xml'
archiveArtifacts artifacts: 'cypress/screenshots/**,cypress/videos/**',
allowEmptyArchive: true
}
}
}
The post { always } block runs regardless of the stage results: the junit step reads the XML and shows the results in the build (junit alone marks the build unstable, but here the sh step running cypress run has already failed it), and archiveArtifacts keeps screenshots and video. Jenkins masks the secret from credentials() in logs. If npm in the container complains about permissions to the home directory, redirect the cache to the working directory, as in the GitLab example.
Limits and alternatives. Parallelisation here is also done by several jobs: manually or through parallel: in GitLab, or through Cypress Cloud with --record --parallel. Artifacts: Cypress takes screenshots on failure by default, while video is off by default and is switched on with video: true; the cypress/screenshots and cypress/videos folders are kept even on failure (when: always, post { always }) with a limited retention so as not to take up space. For reports, mochawesome or Allure are used, and the JUnit reporter gives data for the tests tab if the system can read JUnit XML. Notifications in Slack or Teams with the result and a link to the report are set up separately.
Typical mistakes.
- Running
cypress runright after starting the server with no waiting: the first tests fail with a connection error. - Caching with a bad key or no key: old binaries pile up in the cache.
- Keeping artifacts only on success: in GitLab this is
when: always, in Jenkins thepost { always }block. - Logins and tokens in the repository instead of CI/CD variables or credentials.
Check yourself
In a GitLab job with parallel: 3 all three copies run the same specs. What is missing?
Answer
parallel only creates copies of the job and sets CI_NODE_INDEX and CI_NODE_TOTAL. Specs are split between copies by a separate mechanism: cypress-split (pass SPLIT and SPLIT_INDEX) or Cypress Cloud with --parallel.
The tests failed, the Jenkins build is red and there are no screenshots in the archive. Where do you look?
Answer
Probably archiveArtifacts stands in a stage after the run that failed, so it did not execute. Put it in post { always } and check that the path matches the screenshots folder.