Skip to content

Instantly share code, notes, and snippets.

@agarthetiger
Last active February 15, 2024 10:06
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save agarthetiger/c25afa0a13dcc97c3d2d5362590567a5 to your computer and use it in GitHub Desktop.
Save agarthetiger/c25afa0a13dcc97c3d2d5362590567a5 to your computer and use it in GitHub Desktop.
Quoting strings and variable interpolation in Jenkinsfiles, passing values to shell tasks.
node{
timestamps{
stage('Samples'){
// Single quotes with no interpolation, at least not in Jenkins.
// The dollar variable will be evaluated before being run by the
// shell command, so variables which Jenkins makes available as
// environment variables can still be accessed from within a
// single-quoted string, passed to the sh task.
sh 'echo $PATH'
// Triple single quotes doesn't change how the interpolation works,
// only allows spanning multiple lines to execute multiple commands.
sh '''
echo The build number is $BUILD_NUMBER
'''
// Double quotes are silently dropped. Note that $BUILD_NUMBER here
// is being evaluated by the shell. There is no interpolation by
// Groovy or Jenkins before this single-quoted string is being passed
// to the sh task.
sh 'echo Dropping double quotes in build "$BUILD_NUMBER"'
// To preserve double quotes, they need to be escaped with double
// back slashes. Note that the line echo'd because of the default
// -x sh option is different from the string which is echo'd. See
// console output for this job.
sh 'echo Preserving double quotes in build \\"$BUILD_NUMBER\\"'
// As in any programming language, variables in code don't need a
// dollar prefix or braces.
echo env.BUILD_NUMBER
// Groovy will interpolate anything beginning with a dollar sign in
// a double-quoted string. In this case there was no need to include
// curly braces, these are only required if the variable name is
// otherwise ambiguous within the string.
echo "$env.BUILD_NUMBER"
// A single backslash is required to delimit the dollar if the
// string should be printed exactly as is.
echo "Exact print of text \$env.BUILD_NUMBER"
// Jenkins will interpolate the variable in this string, so will
// pass "echo n" to the sh task, where n is the current build number.
// Note that env is a variable made available in groovy by Jenkins.
sh "echo Build number from double-quoted string is $env.BUILD_NUMBER"
sh "echo Single quotes 'inside' double quotes need no dereferencing."
sh "echo Double quotes need triple backslashes to preserve, like \\\" "
sh """
echo Single quotes 'inside' double quotes from
echo multi-line triple double quoted sh task.
"""
// Example of outputting double quotes into a file, as a workaround
// for not having JSON groovy methods whitelisted across Jenkins
// Masters. This works with variables which sh can interpolate, ie.
// environment variables.
sh '''
echo "{\\"build_number\\":\\"$BUILD_NUMBER\\"}" > temp.json
cat temp.json
'''
// There are two other options, either change the enclosing quotes
// to double quotes and adjust the inner quotes and delimiters.
def local_build_number = env.BUILD_NUMBER
sh """
echo "{\\\"build_number\\\":\\\"$local_build_number\\\"}" > temp2.json
cat temp2.json
"""
// or wrap the wingle quoted example with withenv.
// Note that the snippet generator put single quotes around the
// variable which need to be double quotes here in order to
// interpolate BUILD_NUMBER.
withEnv(["myvar=$BUILD_NUMBER"]) {
sh '''
echo {\\"build_number\\":\\"$myvar\\"} > temp3.json
cat temp3.json
'''
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment