Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select multiple JDK version in declarative pipeline Jenkins

I want to use different JDK versions for different stages in Jenkins declarative pipeline. In the first stage I am using Java 8. In the second stage i am using Java 6. How to select multiple JDK version in declarative pipeline in Jenkins?

  pipeline {
  agent any

  tools {
    jdk 'jdk_1.8.0_151'
    jdk 'jdk_1.6.0_45'
  }

  stages {
    stage('java 8') {
      steps {
        sh 'java -version'
        sh 'javac -version'
      }
    }
    stage('java 6') {
      steps {
        sh 'java -version'
        sh 'javac -version'
      }
    }
  }
}
like image 544
YourAboutMeIsBlank Avatar asked Dec 19 '17 21:12

YourAboutMeIsBlank


People also ask

Can I have multiple versions of JDK?

Yes you can. JDK is merely a directory somewhere on your disk. So you can easily download and unpack all the versions you want, and run java and javac from the directory you're currently interested in.

How do I select Java version in Jenkins?

On the Jenkins Main Page, to the Left click "Manage Jenkins." Access "Global Tool Configuration" menu, and under this menu there is a location about half way down that should say "JDK Installations." Click that button and it will open a new menu where you can add an alternate JDK.

Which JDK version is best for Jenkins?

Running Jenkins Jenkins requires Java 11 or 17 since Jenkins 2.357 and LTS 2.361.

Can we have multiple stages in Jenkins pipeline?

Pipelines are made up of multiple steps that allow you to build, test and deploy applications. Jenkins Pipeline allows you to compose multiple steps in an easy way that can help you model any sort of automation process. Think of a "step" like a single command which performs a single action.


2 Answers

you can add a tools section for each stage.

pipeline {
        agent any

        stages {  
            stage ("first") {
                tools {
                   jdk "jdk-1.8.101"
                }
                steps {
                    sh 'java -version'
                }
            }
            stage("second"){
                tools {
                   jdk "jdk-1.8.152"
                }
                steps{
                    sh 'java -version'
                }
            }
       }
}
like image 130
Mor Lajb Avatar answered Sep 23 '22 12:09

Mor Lajb


I would recommend you to use different docker images for each stage if you want to have different JDK versions. You can achieve using the docker hub openjdk images with the correct tag. https://hub.docker.com/r/library/openjdk/

https://hub.docker.com/r/library/openjdk/tags/ Something like that:

pipeline {
agent none
stages {
    stage('openjdk:7-jdk') {
        agent {
            docker { image 'jdk7_image' }
        }
        steps {
            sh 'java -version'
        }
    }
    stage('java8') {
        agent {
            docker { image 'openjdk:8-jdk' }
        }
        steps {
            sh 'java -version'
        }
    }
}

}

like image 25
Guel135 Avatar answered Sep 24 '22 12:09

Guel135