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
Note that echo does not work in Groovy itself - only in Jenkins. println works in both.
On Linux, BSD, and Mac OS (Unix-like) systems, the sh step is used to execute a shell command in a Pipeline. Jenkinsfile (Declarative Pipeline) pipeline { agent any stages { stage('Build') { steps { sh 'echo "Hello World"' sh ''' echo "Multiline shell steps works too" ls -lah ''' } } } }
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
I'm just putting a '\' on the end of line
sh script: """\
foo='bar' \
echo $foo \
""", returnStdout: true
This statement works on my script.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With