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

Fo. picture Fo. · Jan 27, 2016 · Viewed 62k times · Source

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

Answer

tim_yates picture tim_yates · Jan 27, 2016

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