I am looking at porting a Jenkins non-pipeline job to pipeline. I encountered the situation where, if a parameter value references an environment variable, such as “${HOME}/etc” then the parameter value gets evaluated (eval’d) and then passed to my shell script step as the post-evaluation value “/var/lib/jenkins/etc”.
First of all, I wasn’t expecting this evaluation to occur in the non-pipeline job and couldn’t find documentation that this is supposed to happen. Is this documented somewhere?
Second of all, given that this DOES happen, how do I duplicate the behavior in the pipeline version where the shell script is ran using the sh() command? The Pipeline version gets the parameter avar2 with value ${HOME}/etc. I haven’t found an option where the evaluation can be done for me before running the shell script in a pipeline job. It seems I have to code the script file to do the evaluation on any env variable that might need it.
In the pipeline job, I tried having the shell script specified immediately in the job as well as having the shell script specified in a file in the workspace. I only demonstrate the first case where the shell script is specified immediately in the job here.
I created a Freestyle Job with two string params:
Name: avar1
Default Value: avar1val
Name: avar2
Default Value: ${HOME}/etc
and an “Execute shell” Build Step having the text:
#!/bin/bash -e
env | sort
Running this job gives job console text that looks like the following:
Started by user X
Running as SYSTEM
Building on the built-in node in workspace /var/lib/jenkins/workspace/jam
[jam] $ /bin/bash -e /tmp/jenkins15623yadayada.sh
avar1=avar1val
avar2=/var/lib/jenkins/etc
.
.
.
Finished: SUCCESS
I then created a pipeline job invokes the same shell script using the sh command. Its definition is:
Create a Pipeline Job with two string params:
Name: avar1
Default Value: avar1val
Name: avar2
Default Value: ${HOME}/etc
and the Pipeline definition “Pipeline script”
pipeline {
agent any
stages {
stage('Hello') {
steps {
sh '''#!/bin/bash -e
env | sort
'''
}
}
}
}
Running this job includes this text in the Jenkins job Console Output
.
.
avar1=avar1val
avar2=${HOME}/etc
.
.
Finished: SUCCESS
This is on a linux jenkins v2.452.3 on java 11