Anjan
Anjan

Reputation: 613

Workspace variable not printing in Jenkins Pipeline post step

i have configured a Jenkins pipeline job as follows and env.WORKSPACE works fine

steps {
script
{
echo "${env.WORKSPACE}"
}
}

during build ouput is /home/mach1/workspace

but when i added some lines in post build stage in jenkins pipeline job. it is returning the following error ${FILE,path="echo "${env.WORKSPACE}"/dir1/file2.html"}

script
{
echo "${env.WORKSPACE}"
}
}
 post{
  success{
   script{ 
body:'${FILE,path="${env.WORKSPACE}/dir1/file2.html"}'
}
}
}

may i know where its getting wrong, or how do i correct so that in post step it takes /home/mach1/workspace/dir1/file2.html

Upvotes: -1

Views: 1472

Answers (1)

Matthew Schuchard
Matthew Schuchard

Reputation: 28739

A shell environment variable is referenced as a key within the env Map within Jenkins Pipeline Groovy, and as a string within the shell step method. In your original example, you referenced the variable as a Groovy variable, and interpolated it within the pipeline with the correct " syntax. However, in your second example, you are still referencing the variable as a Groovy variable, but attempting to interpolate it within the executed shell command from the step method. This will not work, as you will pass a literal string of the env Map variable.

You can fix this either by referencing the environment variable as a shell environment variable by removing the env Map key:

body:'${FILE,path="${WORKSPACE}/dir1/file2.html"}'

or by interpolating the Groovy variable within the pipeline:

body:"\${FILE,path=\"${env.WORKSPACE}/dir1/file2.html\"}"

where characters are properly escaped to denote a shell variable FILE as opposed to a Groovy variable, and for the embedded ".

The first correction is, of course, easier to read, and probably what you will prefer.

Upvotes: 2

Related Questions