0

I'm having issues using pipeline variables i setup on aws codebuild with cypress. Basically I don't know how to call them from my Cypress code.

Example of my buildspec.yml

version: 0.2
env:
  variables:
    BASE_URL : "CYPRESS_BASE_URL"
    STAGING_USER : "CYPRESS_STAGING_USER"
    STAGING_PASSWORD : "CYPRESS_STAGING_PASSWORD" 
phases:
  install:
    runtime-versions:
      nodejs: latest
    commands:
      - sudo apt update
      - sudo apt-get install -y libgtk2.0-0 libgtk-3-0 libgbm-dev libnotify-dev libnss3 libxss1 libasound2 libxtst6 xauth xvfb
      - cd tests
      - rm /codebuild/global.json -f
      - npm ci
  build:
    commands:
      -  ls -a
      -  npx cypress run --config-file staging.config.ts

Using the example above CYPRESS_BASE_URL is the name of the pipeline env variable I setup like this enter image description here

Now how do I use the variables in my Cypress code, usually I'll use this from cypress.env.json but I don't use this file on the repo so I can't do that. I need to use variables set on the pipeline or in parameter store. For example in my cypress config I'm confused on how to set the baseUrl from the pipeline variable?

My config file enter image description here

1 Answer 1

0

This is a tricky one.

To overwrite any ENV var in cypress, it has to be prefixed with CYPRESS_ so your naming is already correct. But when I see your yaml file, you remove the prefix again.

If your config should overwrite the base_url, you should set the base url like so:

# this will overwrite the content of cypress.config.json

export CYPRESS_BASE_URL="http://localhost:3000"
cypress run

# or directly inject

CYPRESS_BASE_URL="http://localhost:3000" cypress run

this shell code will set the baseUrl to http://localhost:3000

The same process is used for different env vars. If there is something prefixed with CYPRESS_something the value is available in cypress as "something"

Cypress.env('something') // yields value of CYPRESS_something

edit:

try to rename this:

env:
  variables:
    CYPRESS_BASE_URL : "<your-base-url>"
    CYPRESS_STAGING_USER : "<your-username>"
    CYPRESS_STAGING_PASSWORD : "<your-userpass>"

in your config.json, you can set the env vars to a random string. it doesn't matter if you set the vars in the env.

Sign up to request clarification or add additional context in comments.

2 Comments

This is correct for scripts, but how does it apply to AWS? Also, does setting CYPRESS_BASE_URL set Cypress.config('BASE_URL') or Cypress.env('BASE_URL')?
Cypress works the same in containerized environments. I guess the names of the env files are not correct. I'll adjust my answer, but I can't test it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.