如何在脚本化流水线中设置舞台为不稳定。

huangapple go评论81阅读模式
英文:

How to set the stage to unstable in scripted pipeline

问题

如果下游任务失败,我想将阶段设置为不稳定。我尝试了以下代码但未成功:

```js
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}")
    }
}
英文:

In scripted pipeline i am trying to check the status of the downstream job . if the downstream job is failed i want to set the stage as unstable . i am trying the below code but it does not work

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

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

<!-- end snippet -->

I have tried this option too

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

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

<!-- end snippet -->

答案1

得分: 0

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

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

因此,在上述情况下,如果yourjob失败,触发该作业的整个阶段也将失败。然而,由于已捕获了错误,错误将转换为unstable,并附带您指定的任何消息。

英文:

Jenkins automatically propagates the build result upwards so you don't have to specifically go and get the result from a method as the result is reflected in the status of the upstream build job. Instead, you can catch the error and change the result from your code. For example:

script {
    try {
        build job: &#39;yourjob&#39;, wait: true
    } catch (err) {                                        
        unstable(message: &quot;abc&quot;)
    }
}    

So in the above case, if yourjob fails, the entire stage that is triggering that job will also fail. However, since the failure is being caught, the error will be converted to unstable with whatever message you specify.

huangapple
  • 本文由 发表于 2023年4月10日 23:36:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75978484.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定