How to set variables in a multi-line shell script within Jenkins Groovy?

落爺英雄遲暮 提交于 2019-11-27 14:18:23

问题


Suppose I have a Groovy script in Jenkins that contains a multi-line shell script. How can I set and use a variable within that script? The normal way produces an error:

sh """
    foo='bar'
    echo $foo
"""

Caught: groovy.lang.MissingPropertyException: No such property: foo for class: groovy.lang.Binding


回答1:


You need to change to triple single quotes ''' or escape the dollar \$

Then you'll skip the groovy templating which is what's giving you this issue




回答2:


I'm just putting a '\' on the end of line

sh script: """\
  foo='bar' \
  echo $foo \
""", returnStdout: true

This statement works on my script.



来源:https://stackoverflow.com/questions/35047481/how-to-set-variables-in-a-multi-line-shell-script-within-jenkins-groovy

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