I have variables in azure library that I want to pass to cypress so that I can have multiple stages (staging & production)
I have tried the following:
Using fixtures by having a file called environment.json that looks like so:
{
WEB_APP_BASE_URL": "https://blight-town.com" // the "default" url for lost souls like I
}
In Azure I have a library that contains the variable "
WEB_APP_BASE_URL" with the value "https://not-my-website.com".
my yaml file looks so:
- task: Npm@1
displayName: NPM run cy:run-smoke-tests
inputs:
workingDir: 'webapp'
command: 'custom'
customCommand: 'run cy:run-smoke-tests' //this just does runs cypress for a specific spec file
env:
WEB_APP_BASE_URL: $(WEB_APP_BASE_URL)
From this was hoping to overwrite the default base url and use the value from the library but alas. Any thoughts folks?
2
Answers
Let’s assume two variable groups named
smoke-tests-staging
andsmoke-tests-production
, each containing a variable namedWEB_APP_BASE_URL
.You can reference and use the variable groups like this:
According to the guidance outlined in Cypress documentation Cypress environment variables can be specified in the following:
--env <name>=<value>
You’re interested in the option #3, where
CYPRESS_WEB_APP_BASE_URL
overrides the value forWEB_APP_BASE_URL
In Azure DevOps, variables defined in the pipeline or referenced from a library are represented at runtime as operating system Environment Variables on the build agent performing the work. (The only exception to this rule is if the variable is listed as a secret, then it must be explicitly specified by referencing the variable name in the
env
parameter to the task)To have Cypress pick up your pipeline variables as Cypress Environment Variables, you have the following options:
Option 1: Explicitly Declare Cypress Variables in the Task
I recommend this approach if you only have a few variables that need to be mapped, or if you want to adapt existing variables to match the names used in your tests. For each variable that you want to pass in, explicitly declare it with a
CYPRESS_
prefix for the task process:Option 2: Define your variables in the Library using a CYPRESS_ naming convention
To have Cypress automatically infer values from environment variables, define them in your pipeline or your library starting with the
CYPRESS_
prefix.Your variable group has the following:
The task will automatically pick up the
CYPRESS_
values.