groovy script - sh with variable
If the code here is meant to assign the groovy variable value ("green") to the environment variable COLOR, and echo $COLOR
is meant to print out the shell variable, the $
needs to be escaped like so that the shell can read it, like this:
sh """COLOR=${COLOR}
echo \$COLOR"""
You have to use double quotes instead of single in order to replace expressions in the string:
def COLOR
node('nodename'){
stage ('color') {
COLOR = "green"
echo "color is $COLOR"
sh """COLOR=${COLOR}
echo $COLOR"""
}
}
If single quotes need to be used for some reason, try concatenating using +
:
sh '''COLOR=''' + COLOR + '''
echo ''' + COLOR