Environment variable set in TeamCity to override the Cypress environment variable

Completed

Hi team,

I run my Cypress automation suite using TeamCity. For security reasons, I need to set the value of a token in TeamCity instead of Cypress.

My env details in Cypress, for example, is as follows (defaulting it to null initially):

{
"env": {
"token": ""
}
}

My env variable in TeamCity via the Parameters > Environment Variables (env.) section shows the actual value (encrypted) of the token:

Name: env.token
Kind: Environment Variables (env.)
Value: abcd1234
Spec: password display='normal' 

I need my Cypress automation suite to call the value from TeamCity instead of the default null value in the Cypress env config file.

Basically, instead of Cypress passing values to TeamCity, I need it the other way around. Is this possible?

0
2 comments
Avatar
Fedor Rumyantsev

TeamCity environment variables are passed into the spawned build process as environment. (https://www.jetbrains.com/help/teamcity/configuring-build-parameters.html). This means that you could reference the variable through Cypress.env() as per the Cypress documentation (https://docs.cypress.io/guides/guides/environment-variables.html#We-can-move-this-into-a-Cypress-environment-variable). I would suggest to check if having a variable defined in configuration file will allow it to be overriden by environment variable (i.e. if you have an environment with Cypress installed and token var defined in configuration, does token variable defined as an environment one override that on manual run?).

0
Avatar
Permanently deleted user

Sorry, I forgot to mention that yes, I am using Cypress.env('token') to pass the variable. The problem is that it is taking the value from cypress.json (which as per above, token is set to null) instead of being overwritten by the token I have set from TeamCity.

I found the solution:

1. Replace the variable by adding the script below via navigating to Build Steps > Command Line:

sed -i -e 's|"token": ""|"token": "%token%"|g' ./cypress.json

2. Declare the variable and insert the value via navigating to Parameters > Configuration Parameters:

Name: token
Kind: Configuration paramater
Value: abcd1234
Spec: password display='normal' 
0

Please sign in to leave a comment.