如何在脚本管道中将舞台设置为不稳定

问题描述 投票:0回答:1

在脚本管道中,我正在尝试检查下游作业的状态。如果下游作业失败,我想将舞台设置为不稳定。我正在尝试下面的代码,但它不起作用

if (JOBRUN == "true" ){
    def downstream = build job: "/project/A/${env.BRANCH}", wait: true
    if (downstream.getResult() != 'SUCCESS') {
        unstable(message: "Downstream job result is ${downstream.result}")
    }
}

我也试过这个选项

def downstream = build (job: "/project/A/${env.BRANCH}", wait: true).result
if (downstream.getResult() != 'SUCCESS') {
            unstable(message: "Downstream job result is ${downstream.result}")
        }

jenkins jenkins-pipeline jenkins-groovy
1个回答
0
投票

Jenkins 自动向上传播构建结果,因此您不必专门去从方法中获取结果,因为结果反映在上游构建作业的状态中。相反,您可以捕获错误并更改代码的结果。例如:

script {
    try {
        build job: 'yourjob', wait: true
    } catch (err) {                                        
        unstable(message: "abc")
    }
}    

所以在上面的例子中,如果

yourjob
失败,触发该作业的整个阶段也将失败。但是,由于正在捕获故障,因此无论您指定什么消息,错误都将转换为
unstable

© www.soinside.com 2019 - 2024. All rights reserved.