Pipeline - Selecting which Java JDK to use for a node

Article ID:230922408
1 minute readKnowledge base

Issue

  • You want to select which Java JDK to use in Pipeline

Environment

  • Jenkins

  • CloudBees Jenkins Enterprise

  • Pipeline plugin

Resolution

As an example, in case you have a node called vagrant-agent and the tool is called jdk-8u45. The below code is what you should use to set-up the JAVA_HOME.

node('vagrant-agent') {
    env.JAVA_HOME="${tool 'jdk-8u45'}"
    env.PATH="${env.JAVA_HOME}/bin:${env.PATH}"
    sh 'java -version'
}