如果下游作业失败,如何使主詹金斯管道作业失败

时间:2018-10-04 19:59:41

标签: jenkins continuous-integration

我有一个Jenkins管道master作业,它触发了两个作业buildjob1buildjob2

我的master管道作业很简单,如下所示:

   stage ("Test") {


                build (job: buildJob1,
                             parameters: [
                                    string(name: 'A', value: "AA),
                                    string(name: 'B', value: "BB),

                             ],
                             propagate: false)


              build (job: buildJob2,
                       parameters: [
                           string(name: 'A', value: "AA"),
                      ],
                     propagate: false)


        }


    cleanWs()
}

我希望下游两个作业都运行,并在任何作业失败时将主作业标记为不稳定/失败。
我该如何通过管道做到这一点?

2 个答案:

答案 0 :(得分:1)

如果作业不相互依赖,请与FailFast参数并行使用-并且您必须删除“传播”选项(或将其设置为true)

stage ("Test") {
    parallel buildJob1: {
        build (job: buildJob1,
            parameters: [
                string(name: 'A', value: "AA),
                string(name: 'B', value: "BB),
            ])
    }, buildJob2: {
        build (job: buildJob2,
            parameters: [
                string(name: 'A', value: "AA"),
            ])
    }, failFast: false
    }

cleanWs()
}

编辑:正如您所评论的,作业只能按顺序运行,我们必须手动检查作业结果并手动设置管道作业结果。这样,我们可以根据需要调用任意多个顺序的Jobs:

stage("buildjob1") {
    result = build (job: buildJob1,
        parameters: [
            string(name: 'A', value: "AA),
            string(name: 'B', value: "BB),
        ], propagate=false)
    // Inspect result
    if(result == 'FAILURE') {
        echo "buildjob1 failed"
        currentBuild.result = 'UNSTABLE' // of FAILURE
    }
}
stage("buildjob2") {
    result = build (job: buildJob2,
        parameters: [
            string(name: 'A', value: "AA"),
        ], propagate=false)
    // Inspect result
    if(result == 'FAILURE') {
        echo "buildjob2 failed"
        currentBuild.result = 'UNSTABLE' // of FAILURE
    }
}
stage("cleanup") {
    // Call your cleanup
    cleanWs()
}

答案 1 :(得分:0)

如果有任何错误,您可以将此阶段包装在try catch中==>将当前构建状态设置为FAILURE

try {
    stage("Test") {
      build (job: buildJob1,
             parameters: [
               string(name: 'A', value: "AA"),
               string(name: 'B', value: "BB"),
             ],
             propagate: false)
      build (job: buildJob2,
             parameters: [
               string(name: 'A', value: "AA"),
               string(name: 'B', value: "BB"),
             ],
             propagate: false)
    }
} catch (error) {
    currentBuild.result='FAILURE'
}