英文:
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 == "true" ){
def downstream = build job: "/project/A/${env.BRANCH}", wait: true
if (downstream.getResult() != 'SUCCESS') {
unstable(message: "Downstream job result is ${downstream.result}")
}
}
<!-- end snippet -->
I have tried this option too
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
def downstream = build (job: "/project/A/${env.BRANCH}", wait: true).result
if (downstream.getResult() != 'SUCCESS') {
unstable(message: "Downstream job result is ${downstream.result}")
}
<!-- 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: 'yourjob', wait: true
} catch (err) {
unstable(message: "abc")
}
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论