Jenkins Pipeline: How to replace part of a sh command arg with a variable

Viewed 122

I am unsure about some syntax I am putting together for a Jenkins pipeline step where I want to replace part of a command line arg (for a filename) in a shell script with a variable I have defined

sh "npx cypress run --config-file cypress/cypress.$e2eEnvironment.json --env $envCredentials || true"

I think what I have above will look for a variable $e2eEnvironment.json but I dont want that as I need to keep the .json as is in the cli arg. E.g. if I have an $e2eEnvironment value set to integration then the value should get built as cypress/cypress.integration.json

Can someone guide me here please?

1 Answers

You can enforce the exact name of a variable resolution in string interpolation with ${<varName>} syntax in Groovy. In your example:

sh "npx cypress run --config-file cypress/cypress.${e2eEnvironment}.json --env $envCredentials || true"

will resolve to:

sh "npx cypress run --config-file cypress/cypress.integration.json --env $envCredentials || true"

as desired, instead of resolving the assigned value for a variable name e2eEnvironment.json.

Related