Jenkins Pipeline Step withEnv does not work without BASH

|▌冷眼眸甩不掉的悲伤 提交于 2020-02-24 17:01:45

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!