Search code examples
jenkins-pipeline

In Jenkins pipeline, what is the correct syntax to accept the passed-in parameter for this function?


I have a function that sets the NodeJS version.

def setNodeVersion(final String nodeVersion="11.15.0") {
  env.NODE_HOME = "${tool 'Node-"${nodeVersion}"'}"
  env.PATH="${env.NODE_HOME}/bin:${env.PATH}"
  sh """
    node -v
    npm -v
  """
}

setNodeVersion("12.22.1")

But when I call it, I get

ERROR: No tool named Node-"${nodeVersion}" found
Finished: FAILURE

What is the correct syntax?


Solution

  • When you set NODE_HOME, you're already inside a GString when invoking tool; no need for a second ${}.

    Try env.NODE_HOME = "${tool ('Node-' + nodeVersion)}".