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

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

问题描述

我有以下 post 失败部分:

I have the following post failure section:

   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天全站免登陆