How to select multiple JDK version in declarative

2020-05-27 04:24发布

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

3条回答
2楼-- · 2020-05-27 05:05

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'
                }
            }
       }
}
查看更多
疯言疯语
3楼-- · 2020-05-27 05:10

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'
      }
    }
  }
}
查看更多
神经病院院长
4楼-- · 2020-05-27 05:24

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

}

查看更多
登录 后发表回答