Schedule Cypress Run with Gitlab Pipeline Schedule

Art Krisada
3 min readApr 30, 2024

--

My short note on Schedule Cypress Test run with Gitlab Pipeline Schedules and view the results on Cypress Cloud.

I assume you already have running Cypress project on Gitlab. And you need to sign up for Cypress Cloud.

Sign in to Cypress Cloud Then Create Project.

You will get Project ID. Add to your Code.

Choose Gitlab CI as your provider (I use Gitlab CI. You can choose other provider.)

You can try to run and push result by CLI command above. Replace Key with your key on Cypress Cloud.

Also, Set up Cypress Record Key as Gitlab CI Variables as Above.

My .gitlab-ci.yml file is a little different.

stages:
- test
variables:
CYPRESS_RECORD_KEY: $CYPRESS_RECORD_KEY
cache:
key:
files:
- package-lock.json
paths:
- .npm/
test:
# Uses official Cypress docker image for dependencies
# https://docs.cypress.io/guides/continuous-integration/introduction#Official-Cypress-Docker-Images
image: cypress/browsers:node-20.9.0-chrome-118.0.5993.88-1-ff-118.0.2-edge-118.0.2088.46-1
stage: test
script:
- echo ${DEPLOY_ENVIRONMENT}
- npm ci
- npm start &
- npx cypress run --record --browser chrome --config-file --key $CYPRESS_RECORD_KEY

Next, Schedule Gitlab Pipeline as you like.

That will be all. It should run on your schedule.

###############################################################

The following section is my note on running for multiple environments.

This need 3 Projects in Cypress Cloud to receive test results from 3 environments (dev, uat, prod) seperate by 3 Keys.

You need to add 3 Gitlab CI Variables.

  • PROD_CYPRESS_RECORD_KEY (Cypress Key for Production Projects)
  • UAT_CYPRESS_RECORD_KEY (Cypress Key for UAT Projects)
  • DEV_CYPRESS_RECORD_KEY (Cypress Key for Dev Projects)

I also set 3 config files for 3 projects. Keep your checked variables separate by env config files.

stages:
- test
variables:
DEPLOY_ENVIRONMENT:
value: "dev"
options:
- "prod"
- "uat"
- "dev"
description: "The deployment target. Set to 'dev' by default."
PROD_CYPRESS_RECORD_KEY: $PROD_CYPRESS_RECORD_KEY
UAT_CYPRESS_RECORD_KEY: $UAT_CYPRESS_RECORD_KEY
DEV_CYPRESS_RECORD_KEY: $DEV_CYPRESS_RECORD_KEY
cache:
key:
files:
- package-lock.json
paths:
- .npm/
test:
# Uses official Cypress docker image for dependencies
# https://docs.cypress.io/guides/continuous-integration/introduction#Official-Cypress-Docker-Images
image: cypress/browsers:node-20.9.0-chrome-118.0.5993.88-1-ff-118.0.2-edge-118.0.2088.46-1
stage: test
script:
- echo ${DEPLOY_ENVIRONMENT}
- if [ "$DEPLOY_ENVIRONMENT" == "prod" ]; then CYPRESS_RECORD_KEY=$PROD_CYPRESS_RECORD_KEY; fi
- if [ "$DEPLOY_ENVIRONMENT" == "uat" ]; then CYPRESS_RECORD_KEY=$UAT_CYPRESS_RECORD_KEY; fi
- if [ "$DEPLOY_ENVIRONMENT" == "dev" ]; then CYPRESS_RECORD_KEY=$DEV_CYPRESS_RECORD_KEY; fi
- npm ci
- npm start &
- npx cypress run --record --browser chrome --config-file $DEPLOY_ENVIRONMENT.config.js --key $CYPRESS_RECORD_KEY

This is my Gitlab Project. You can see code example from here. It needs cypress key to run, replace with yours.

--

--

Art Krisada
Art Krisada

Written by Art Krisada

Never stop learning, because life never stop teaching.

No responses yet