Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins Pipeline Step withEnv does not work without BASH

I've trouble setting an environment variable for a container in an Jenkins pipeline. It seems, that "withEnv" does not work nicely with machines without bash.

Can you confirm that? I cannot find an official statement ;-)

When I run the following snippet on the Jenkins slave it works. But when it is executed in a docker container without BASH "$test" isn't set.

 withEnv(["test='asd'"]){
      sh 'echo $test'
 }

https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#code-withenv-code-set-environment-variables

like image 586
Sebastian Woinar Avatar asked Dec 30 '25 21:12

Sebastian Woinar


1 Answers

If I'm not mistaken, I believe the variable is not set correctly.

Try this:

withEnv(["test=asd"]){
      sh "echo \$test"
 }

Within a Jenkins pipeline:

$var = Groovy parameter
\$var (within a sh closure) = Bash parameter
${var} = also refers to Groovy parameter

In order to insert a groovy variable into a bash variable:

sh ("VAR=${GROOVY_VAR}")

Using a bash variable inside a sh closure:

sh (" echo \$BASH_VAR")
like image 108
Itai Ganot Avatar answered Jan 01 '26 12:01

Itai Ganot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!