问题
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
回答1:
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")
来源:https://stackoverflow.com/questions/40672083/jenkins-pipeline-step-withenv-does-not-work-without-bash