How to select multiple JDK version in declarative pipeline Jenkins

别说谁变了你拦得住时间么 提交于 2019-12-03 06:49:55

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'
        }
    }
}

}

From the Pipeline tools directive:

tools: A section defining tools to auto-install and put on the PATH. 

The tool name must be pre-configured in Jenkins under 
Manage Jenkins → Global Tool Configuration.

From the pipeline-examples and cloudbess example:

  pipeline {
  agent any

  tools {
    jdk 'jdk_1.8.0_151'
  }

  stages {
    stage('jdk 8') {
      steps {
        sh 'java -version'
        sh 'javac -version'
      }
    }
    stage('jdk 6') {
      steps {
        withEnv(["JAVA_HOME=${tool 'openjdk_1.6.0_45'}", "PATH=${tool 'openjdk_1.6.0_45'}/bin:${env.PATH}"]) {
          sh 'java -version'
          sh 'javac -version'
        }
      }
    }
    stage('global jdk') {
      steps {
        sh 'java -version'
        sh 'javac -version'
      }
    }
  }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!