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'
}
}
}
}
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.
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.
Running Jenkins Jenkins requires Java 11 or 17 since Jenkins 2.357 and LTS 2.361.
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.
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'
}
}
}
}
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'
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With