dropbear
dropbear

Reputation: 1727

Expand variable inside Jenkins pipeline tool directive

I would like to be able to programmatically decide which tool will be installed in an Agent for a Jenkins pipeline.

This is something I have that's working today:

withEnv(["JAVA_HOME=${tool 'OPENJDK11'}",
         "PATH+JAVA=${tool 'OPENJDK11'}"]) {
    ... do stuff ...
}

So I have a global tool OPENJDK11 installed, along with OPENJDK14, and now I would like to change the Groovy script to be able to decide which JDK to install.

So before the part above I have saved the name of the tool in a variable jdkToInstall, how am I able to reference this variable inside the tool directive?

I have tried:

${tool '${jdkToInstall}'} and ${tool '$jdkToInstall'}.

That doesn't expand my variable, so I get an error message saying it can't find the tool "$jdkToInstall".

I also tried with string concatenation, but that ended up with a similar error message with my plus and everything.

Upvotes: 0

Views: 620

Answers (1)

Melkjot
Melkjot

Reputation: 518

It is sufficient to expand (${}) the variable only once. Following works as expected:

withEnv(["JAVA_HOME=${tool jdkToInstall}", "PATH+JAVA=${tool jdkToInstall}"]) {  
  ... do stuff ... 
}

Upvotes: 1

Related Questions