在单个脚本管道中使用多个JDK [英] Using multiple JDKs in single scripted pipeline

查看:108
本文介绍了在单个脚本管道中使用多个JDK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在上述编译后的代码中,有一种情况是使用JDK1.7编译项目,而在声纳管道作业中使用JDK1.8.

We have a situation where JDK1.7 is used to compile a project and JDK1.8 is used in sonar pipeline job on the aforementioned compiled code.

是否可以在单个脚本作业中使用2个JDK.如果达到声纳级,则必须使用JDK 1.8.

Is it possible to use 2 JDKs in a single scripted job. If sonar stage is reached, then JDK 1.8 must be used.

当前,这是我们的管道.

Currently this is our pipeline.

node('jenkins_uat_sdg_1g'){

    env.JAVA_HOME="${tool 'JDK1.7_110'}"
    env.PATH="${env.JAVA_HOME}/bin:${env.PATH}"

    deleteDir()

    try{
        try{
            stage('SCM Code Checkout'){
                echo "Checking out source code from SVN..."
                echo "successfully checked out from SVN"
            }
        } catch(error){
            println("Unable to checkout...there were some errors!")
            currentBuild.result = "FAILURE"
            error()
        }
        try{
            stage('Compile & Package Generation'){
                echo "Begining to compile the code"
                bat label: 'build-maven', script: 'mvn -f pom.xml clean compile install'
                echo "Successfully compiled"
            }
        }catch(error){
            println("Unable to compile...there were some errors!")
            currentBuild.result = "FAILURE"
            error()      
        }

}

不同的管道脚本用于声纳分析.

Different pipeline script is used for sonar analysis.

node('jenkins_uat_sdg_1g'){

    env.JAVA_HOME="${tool 'JDK1.8'}"
    env.PATH="${env.JAVA_HOME}/bin:${env.PATH}"

    stage('SCM Code Checkout'){
                echo "Checking out source code from SVN..."
                echo "successfully checked out from SVN"
    }


    stage('sonarqube analysis'){
        withSonarQubeEnv('SonarServer'){
            bat label: 'sonar-analyis', script: '"D:/Apache Build Tools/apache-maven-3.6.1-bin/apache-maven-3.6.1/bin/mvn" org.sonarsource.scanner.maven:sonar-maven-plugin:3.6.0.1398:sonar -f parent/pom.xml -Dsonar.host.url=http://10.xx.xx.xx:9500 -Dsonar.login=xxxxxxxxxxxxxxxxxxxxxa4b4cf180c6'
        }
    }
}

stage('Quality Gate'){
    timeout(time: 5, unit: 'MINUTES'){
        def qg = waitForQualityGate()
        if(qg.status != 'OK'){
            error "Pipeline aborted due to quality gate failure: ${qg.status}"
        }
    }
}

是否可能有一个脚本在单个作业中使用两个JDK.

Is it possible to have a single script which utilizes two JDKs in a single job.

致谢

推荐答案

您可以使用

You can make use of withEnv step to set one or more environment variables within a block. These are available to any external processes spawned within that scope

在不同阶段访问不同Java版本的情况下,以下代码对我有用:

The following code works for me where i am accessing different Java versions in different stages:

node('slave1') {

    deleteDir()

    try {
        try {
            stage('During Build') {
                withEnv(['JAVA_HOME=/usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin']) {
                    sh '${JAVA_HOME}/java -version'
                    println("or whatever command you want to run in this block...")
                }
            }
        } catch(error) {
            println("Unable to find Java 7!")
            currentBuild.result = "FAILURE"
            error()
        }
        try {
            stage('During Sonar Analysis') {
                withEnv(['JAVA_HOME=/usr/lib/jvm/jre-1.8.0-openjdk.x86_64/bin']) {
                    sh '${JAVA_HOME}/java -version'
                    println("or whatever command you want to run in this block...")
                }
            }
        } catch(error) {
            println("Unable to find Java 8!")
            currentBuild.result = "FAILURE"
            error()
        }
    } catch(error) {
        println("Last catch block!")
        error()
    }
}

注意:正如您在上面注意到的那样,我们在Groovy中使用引号,因此变量扩展是由Bourne shell而不是Jenkins完成的.

Note: As you would have noticed above, we are using single quotes in Groovy, so the variable expansion is being done by the Bourne shell, not Jenkins

输出:

这篇关于在单个脚本管道中使用多个JDK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆