Problem
I am working on a Jenkins job which accepts some parameters from the user. I am encountering an undesired behaviour: Jenkins appears to be expanding environment variables references within the parameter environment variables before my script has a chance to read them.
If the user enters foo-$BUILD_NUMBER
for a parameter, what my script actually sees is something like foo-123
; the environment variable is expanded. If input the value contains $$
, my script only sees a single $
. However, if it contains a $variable
which does not exist in the environment, the value is left unmodified (without raising any kind of error).
This is inconvenient because it even happens for password fields, and I typically use a password generator that can include $
characters. I don't want my passwords to potentially be silently mangled.
Example
My initial test case was the following, using the Jenkins Job Builder Groovy DSL.
new BaseJobBuilder(
jobName: 'example',
jobBuildName: 'example-${BUILD_NUMBER}',
).build(this).with {
parameters {
nonStoredPasswordParam('SERVICE_PASSWORD')
}
steps {
shell('echo "$SERVICE_PASSWORD";')
}
}
However, for a more reduced test case I created a new Jenkins installation from the jenkins/jenkins:lts
Docker image, configured it without any plugins (even the defaults), and created an equivalent job using the web UI.
When I run either of these jobs with the value hello $BUILD_NUMBER $HOME world
for my parameter SERVICE_PASSWORD
, the output has the variables expanded, instead of the literal value I want.
Started by user jeremy
Building in workspace /var/jenkins_home/workspace/jeremy
[jeremy] $ /bin/sh -xe /tmp/jenkins2451955822062381529.sh
+ echo hello 3 /var/jenkins_home world
hello 3 /var/jenkins_home world
Finished: SUCCESS
Question
Is there any way to access the original parameter values for a Jenkins before they're subject to variable expansion/interpolation, or to otherwise disable or circumvent this behaviour?
How can I accept raw text parameters that may contain $
dollar characters without risk of them being mangled?
Related Links
- JENKINS-10779 Non-existing parameters are not empty but replaced with parameter name
- JENKINS-16143 Jenkins escapes 2 dollar signs in the Parameterized Build fields
- JENKINS-26916 There should be an option to turn on/off variable substitution in build parameters
- Jenkins text parameter - special characters garbled (unwanted variable substitution)