【问题标题】:Jenkins - Having issues with PostBuild Email notificationsJenkins - PostBuild 电子邮件通知有问题
【发布时间】:2017-11-22 13:19:17
【问题描述】:

尝试使用以下代码触发多分支管道作业的电子邮件通知:

1    def emailNotification() {
2        def to = emailextrecipients([[$class: 'CulpritsRecipientProvider'],
3                                     [$class: 'DevelopersRecipientProvider'],
4                                     [$class: 'RequesterRecipientProvider']])
5       
6       //def to = "firstname.lastname@domain.com"                               
7        //String currentResult = currentBuild.result
8       String currentResult = manager.build.getResult()
9       echo "CurrentResult1=${currentResult}"
10      echo "CurrentResult2=${manager.build.getResult()}"
11      echo "CurrentResult3=${manager.build.result}"
12        String previousResult = currentBuild.getPreviousBuild().result
13    
14        def causes = currentBuild.rawBuild.getCauses()
15        // E.g. 'started by user', 'triggered by scm change'
16        def cause = null
17        if (!causes.isEmpty()) {
18            cause = causes[0].getShortDescription()
19        }
20    
21        // Ensure we don't keep a list of causes, or we get
22        // "java.io.NotSerializableException: hudson.model.Cause$UserIdCause"
23        // see http://stackoverflow.com/a/37897833/509706
25        causes = null
26    
27        String subject = "${env.JOB_NAME} ${env.BUILD_NUMBER}: ${currentResult}"
28    
29        String body = """
30                      <p>Triggered by: <b>${cause}</b></p>
31    
32                      <p>Last build result: <b>${previousResult}</b></p>
33    
34    
35                      <p>Build <b>${env.BUILD_NUMBER}</b> ran on <b>${env.NODE_NAME}</b> and terminated with <b>${currentResult}</b>.
36                      </p>
37    
38                      <p>See: <a href="${env.BUILD_URL}">${env.BUILD_URL}</a></p>
39    
40                      """
41    
42        String log = currentBuild.rawBuild.getLog(40).join('\n')
43        if (currentBuild != 'SUCCESS') {
44            body = body + """
45                          <h2>Last lines of output</h2>
46                          <pre>${log}</pre>
47                          """
48        }
49    
50        if (to != null && !to.isEmpty()) {
51            // Email on any failures, and on first success.
52            if (currentResult != 'SUCCESS' || currentResult != previousResult) {
53                mail to: to, subject: subject, body: body, mimeType: "text/html"
54            }
55            echo 'Sent email notification'
56        }
57    }

现在,我面临的问题:

  1. def to = emailextrecipients... 不工作。我发现 thisthis Jenkins Jira 问题可能是原因,但没有解决方法。虽然如果手动启动构建看起来很奇怪,比如我说一个通过 Github Oauth 认证的用户,邮件可以发送。如果 Github 通过 webhook 开始构建,我会在 Jenkins 日志中看到:

不向用户 firstname.lastname@domain.com 发送邮件 查看权限

  1. 我看到的第二个问题是 PostBuild 电子邮件触发器。 管道如下所示:

    def emailNotification() {
        //the one from above
    }
    try {
       stage('Stage1') {
           /*
           creating multiple nodes based on an array provided
           each node will execute:
           checkout scm
           buildSolution() //custom method defined
           */
           parallel <stuff_above>
       }
       stage('Stage2') {
           //do other stuff
           parallel <other_stuff_above>
        }
    } finally {
        emailNotification()
    }
    

上面的回声(第9-11行)都显示null

CurrentResult1=null

CurrentResult2=null

CurrentResult3=null

使用currentBuild.currentResult 将只显示SUCCESSFAILED,而不显示UNSTABLE,以防某些测试失败。

有什么想法吗?

【问题讨论】:

    标签: email jenkins jenkins-pipeline email-ext jenkins-email-ext


    【解决方案1】:

    构建状态为空,直到有东西设置它或直到作业完成。您是否使用了任何会导致构建不稳定的单元测试步骤?

    您无需使用emailextrecipients 而是使用。

    emailext body: body, mimeType: 'text/html', recipientProviders: [
      [$class: 'CulpritsRecipientProvider'], 
      [$class: 'DevelopersRecipientProvider'], 
      [$class: 'RequesterRecipientProvider']], subject: subject
    

    不向用户 firstname.lastname@domain.com 发送邮件 查看权限

    表示没有 jenkins 用户关联此电子邮件地址,或者与之关联的用户没有该工作的权限

    另外,由于原因,将该逻辑放在不同的函数中并添加 @NonCPS 注释,这将阻止 jenkins 在该函数运行时尝试序列化状态,因为您目前拥有它,它仍然会因该异常而中断的可能性很小,见https://stackoverflow.com/a/38439681/963402

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-11
      • 1970-01-01
      • 2015-02-15
      • 2015-03-14
      • 1970-01-01
      • 2010-10-23
      相关资源
      最近更新 更多