Search code examples
jenkinsjenkins-pipelinefile-exists

fileExists in jenkins pipeline not working with ${variant}


In jenkins pipeline

variant is read from job config as

variant = job_config['variant']

variant can be either 'abc' or 'xyz' depending on what is entered in job config.

if(fileExists('tmp/build/${variant}/bin/Test.xml'))
{
echo "Test.xml exists" 
}
else
{
echo "Test.xml doesnot exists" 
}

Lets assume variant is given as 'abc' in job config. Even though 'tmp/build/abc/bin/Test.xml' exists in real,

During jenkins build, the condition check fileexists of 'tmp/build/${variant}/bin/Test.xml is read as it is i.e., ${variant} is not replaced by abc during condition check.

Can you someone help how to mitigate this. Thanks!


Solution

  • You probably need to use double quotes in your path instead of single quotes : if(fileExists("tmp/build/${variant}/bin/Test.xml")) so that the variable is substituted by its value.

    Jenkins pipelines are written in groovy, so you can find more info about the difference between single quotes and double quotes in groovy in this question and in the official groovy documentation.