在Jenkinsfile失败中获取错误原因 [英] Get error reason in Jenkinsfile failure

查看:324
本文介绍了在Jenkinsfile失败中获取错误原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下post失败部分:

   post {
        failure {
            mail subject: "\u2639 ${env.JOB_NAME} (${env.BUILD_NUMBER}) has failed",
                    body: """Build ${env.BUILD_URL} is failing!
                          |Somebody should do something about that""",
                      to: "devel@example.com",
                 replyTo: "devel@example.com",
                    from: 'jenkins@example.com'
        }
    }

我想在错误消息的正文中包含构建失败的原因.

I would like to include the reasons why the build failed in the body of the error message.

我该怎么做?

如果没有,是否可以将构建日志文件附加到电子邮件?

If not, is there a way to attach the build log file to the email?

推荐答案

我不知道一种凭空自动获取故障原因的方法.

I don't know of a way to retrieve the failure reason automatically out of thin air.

但是,您可以在每个阶段中使用"post {failure {"块至少将其失败的阶段捕获到环境变量中(例如env.FAILURE_REASON),并在最终(全局作用域)中访问该env var )通知栏.

However, you can use "post{ failure {" blocks in each phase to capture at least the phase in which it failed into a environment variable (e.g. env.FAILURE_REASON), and access that env var in the final (global scope) notification block.

要获得更高的粒度,您可以重用全局env变量的相同机制,但可以使用try {} catch {}块来捕获哪个特定步骤失败了.

For more granularity, you can reuse the same mechanism of the global env variable, but use try { } catch { } blocks to capture which specific step failed.

一个通用的例子是:

   pipeline {
     stages {
       stage('Build') {
         steps {
           ...
         }
         post {
           failure {
             script { env.FAILURE_STAGE = 'Build' }
           }
         }
       }
       stage('Deploy') {
         steps {
           ...
         }
         post {
           failure {
             script { env.FAILURE_STAGE = 'Deploy' }
           }
         }
       }
       ...
     }
     post {
        failure {
            mail subject: "\u2639 ${env.JOB_NAME} (${env.BUILD_NUMBER}) has failed",
                    body: """Build ${env.BUILD_URL} is failing in ${env.FAILURE_STAGE} stage!
                          |Somebody should do something about that""",
                      to: "devel@example.com",
                 replyTo: "devel@example.com",
                    from: 'jenkins@example.com'
        }
      }
    }

从技术上讲,您甚至可以根据失败阶段进行一些自动分类,并发送更具针对性的通知,甚至可以创建特定(例如Jira)票证.

Technically, you can even do some automated triage based on the failing stage and send a more targeted notification, or even create specific (e.g. Jira) tickets.

要将控制台日志附加到电子邮件通知,您需要查看 emailext 和'attachLog:true'属性

For attaching the console log to the email notification, you'd want to look at emailext and the 'attachLog: true' attribute

这篇关于在Jenkinsfile失败中获取错误原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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