在jenkins工作流

时间:2016-03-11 08:30:45

标签: jenkins groovy jenkins-plugins jenkins-workflow

我正在执行3个并行作业,每个作业都运行测试,如下所示:

def run_job(job) {
   output = build(job:job, parameters:parameters)
   def buildNumber = output.getNumber().toString()
   test_results[job] = '/job/'+ job +'/' + buildNumber + '/artifact/test_result.xml'

}


def test_func_array = [:]
def test_results = [:]

test_func_array['Python_Tests'] = {run_job('Run_Python_Tests', test_results)}

test_func_array['JS_Tests'] = {run_job('Run_JS_Tests', test_results)}

test_func_array['Selenium_Tests'] = {run_job('Run_Selenium_Tests', test_results)}

parallel(test_func_array)

当每个作业成功时,我可以使用output.getNumber()调用获取内部版本号。但是,当作业失败时,build()函数调用会抛出异常,因此无法获取内部版本号。

但是,失败的构建仍然可以具有构建号并具有存档的工件。 如何获取失败构建的内部版本号?

3 个答案:

答案 0 :(得分:2)

使用propagate: false。有关详细信息,请参阅 Snippet Generator

答案 1 :(得分:0)

我认为,当你想要完成所有并行作业时,即使有一个或多个作业失败,Jesse的答案也是有效的。基本上,它将禁用failFast功能。

有没有人知道如何捕获失败的作业号码,同时仍然有failFast功能在作业失败的情况下使构建短路?例如,下面是我的代码,我也想在catch块中包含变量achild_job_info的值。

build_jobs = [“Build_A”, “ Build_B”, “ Build_C”]

// in this hashmap we'll place the jobs that we wish to run
def branches = [:] 
def achild_job_info = ""
def abuild_number = ""
for (x in build_jobs) {
    def abuild = x 
    branches[abuild] = { 
        stage(abuild){
            def allow_retry = true
            retry(2) {
                try {
                    achild_job_info = build job: abuild
                    echo “ achild_job_info”  // —>  this gives: org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper@232601dc
                    abuild_number = achild_job_info.getId()
                    build_job_to_number_mappings[abuild] = achild_job_info.getNumber()
                } catch (err) {
                    echo “ achild_job_info: ${achild_job_info } “  // —> This comes empty. I want the runwrapper here as well, just like in the try block. 
                    abuild_job_number = abuild_job_info.getId()
                    build_job_to_number_mappings[abuild] = achild_job_info.getNumber()
                } // try-catch
        } // stage
   } // branches
} // for
branches.failFast = true
parallel branches

答案 2 :(得分:0)

如果使用propagate: false,则不能使用try-catch块,因为build job在作业失败时不会抛出异常,因此您需要通过getResult()方法处理结果像这样:

stage('build something'){
    def job_info = build job: build_something, propagate: false,
    println "Build number: ${job_info.getNumber()}"
    currentBuild.result = job_info.getResult()
}

另见:https://jenkins.io/doc/pipeline/steps/pipeline-build-step/