如何在Jenkinsfile中为失败的构建执行操作 [英] How to perform actions for failed builds in Jenkinsfile

查看:1348
本文介绍了如何在Jenkinsfile中为失败的构建执行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想告诉我们的Atlassian Stash实例,构建失败(或者回滚)如果在Jenkinsfile中构建失败,通过在正确的URL处执行 curl )。



基本上,当设置构建状态时,



我应该使用 try {} catch()吗?如果是这样,我应该捕获什么样的异常类型?

解决方案

我目前也在寻找解决这个问题的方法。到目前为止,我能想到的最好的方法是创建一个包装函数,它在try catch块中运行管道代码。如果您还想通知成功,则可以将Exception存储在变量中,并将通知代码移至finally块。另外请注意,您必须重新抛出异常,以便Jenkins认为构建失败。
$ b

  pipeline('linux'){
stage'Pull'可能有些读者会发现一个更优雅的方法来解决这个问题。 '
stage'Deploy'
echo部署
抛出FileNotFoundException(Nothing to pull)
// ...
}

def pipeline(String label,Closure body){
node(label){
wrap([$ class:'TimestamperBuildWrapper']){
try {
body。 call()
} catch(Exception e){
emailext subject:$ {env.JOB_NAME} - Build#$ {env.BUILD_NUMBER} - FAILURE($ {e.message})!, to:me@me.com,body:...
throw e; //重新抛出,因此构建被认为是失败的




code $ pre $ $ b

Is there a way to perform cleanup (or rollback) if the build in Jenkinsfile failed?

I would like to inform our Atlassian Stash instance that the build failed (by doing a curl at the correct URL).

Basically it would be a post step when build status is set to fail.

Should I use try {} catch ()? If so, what exception type should I catch?

解决方案

I'm currently also searching for a solution to this problem. So far the best I could come up with is to create a wrapper function that runs the pipeline code in a try catch block. If you also want to notify on success you can store the Exception in a variable and move the notification code to a finally block. Also note that you have to rethrow the exception so Jenkins considers the build as failed. Maybe some reader finds a more elegant approach to this problem.

pipeline('linux') {
    stage 'Pull'
    stage 'Deploy'
    echo "Deploying"
    throw new FileNotFoundException("Nothing to pull")
    // ... 
}

 def pipeline(String label, Closure body) {
     node(label) {
        wrap([$class: 'TimestamperBuildWrapper']) {
            try {
                body.call()
            } catch (Exception e) {
                emailext subject: "${env.JOB_NAME} - Build # ${env.BUILD_NUMBER} - FAILURE (${e.message})!", to: "me@me.com",body: "..."
                throw e; // rethrow so the build is considered failed                        
            } 
        }
    }
}

这篇关于如何在Jenkinsfile中为失败的构建执行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆