we are using Cypress and Bitbucket pipelines. We have several development environments and I want to change the baseurl dynamically depending on which branch I select via Bitbucket pipelines.
the script for running the Cypress commands in Bitbucket pipelines looks like this:
script:
- ./hybris-url-mapping.sh $CYPRESS_BASE_URL
- cd /working
- npx cypress run --env baseUrl=$CYPRESS_BASE_URL
the script (./hybris-url-mapping.sh
) which should change the URL based on the selected branch looks like this
if [[ $BITBUCKET_BRANCH == 'develop' ]]; then CYPRESS_BASE_URL="develop_url" ; fi
if [[ $BITBUCKET_BRANCH == 'staging' ]]; then CYPRESS_BASE_URL="staging_url" ; fi
if [[ $BITBUCKET_BRANCH == 'staging2' ]]; then CYPRESS_BASE_URL="staging2_url" ; fi
if [[ $BITBUCKET_BRANCH == 'release' ]]; then CYPRESS_BASE_URL="prod_url" ; fi
echo $CYPRESS_BASE_URL
now we come to the problem. Unfortunately it does not work. At least in echo $CYPRESS_BASE_URL
the correct URL is returned based on the branch but it is not set as baseUrl
in the commandline npx cypress run --env baseUrl=$CYPRESS_BASE_URL
. There the url in the cypress.json
is always used:
{
"baseUrl": "local_url"
}
i have tried npx cypress run --env baseUrl=$CYPRESS_BASE_URL
and
npx cypress run --config baseUrl=$CYPRESS_BASE_URL
but neither works.
What am I doing wrong?
You're setting the variable CYPRESS_BASE_URL
in a different shell, so when hybris-url-mapping.sh
exits, it's not exported to the parent shell. It's a security feature, and it can't be changed.
The only way is to echo it and perform command substitution:
$ CYPRESS_BASE_URL=$(./hybris-url-mapping.sh)
or:
$ CYPRESS_BASE_URL=`./hybris-url-mapping.sh`
After this line, you will have whatever you echoed in hybris-url-mapping.sh
in CYPRESS_BASE_URL
.