How to select multiple JDK version in declarative pipeline Jenkins

。_饼干妹妹 提交于 2019-12-21 01:44:30

问题


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

回答1:


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



回答2:


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

}




回答3:


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


来源:https://stackoverflow.com/questions/47895668/how-to-select-multiple-jdk-version-in-declarative-pipeline-jenkins

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!