In my docker run, I would like to pass a secret as an env variable. And that works just fine. However, I also have a second variable "foo" that I would like to echo inside my container. This does not work. How can I get the variable "foo" to expand inside the triple single quotes?
stages {
stage('test') {
steps {
script {
def foo = "bar"
sh '''
docker run \
--rm \
--env secret=$ENV_SECRET \
python:3.8.12 /bin/bash -c \
"echo $foo"
'''
}
}
}
}
CodePudding user response:
The syntax for interpolating strings with Groovy variables in Groovy involves "
. If your ENV_SECRET
is an environment variable bound from a withCredentials
block, then you want to interpolate it within the shell interpreter. Therefore, we can update your shell step method with:
sh """
docker run \
--rm \
--env secret=\$ENV_SECRET \
python:3.8.12 /bin/bash -c \
'echo $foo'
"""
Your syntax would have interpolated shell variables instead. Note that both will interpolate environment variables, but with different interpreters.