Jenkins - PostBuild 电子邮件通知有问题

Jenkins - Having issues with PostBuild Email notifications

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

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 
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... 无效。我发现 this and this Jenkins Jira 问题,这可能是原因,但没有解决方法。虽然这看起来很奇怪,如果手动启动构建,我说一个通过 Github Oauth 验证的用户,可以发送邮件。如果 Github 通过 webhook 开始构建,我会在 Jenkins 日志中得到这个:

Not sending mail to user firstname.lastname@domain.com with no permission to view

  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,以防某些测试失败。

知道问题出在哪里吗?

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

您不需要使用 emailextrecipients 而是使用。

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

Not sending mail to user firstname.lastname@domain.com with no permission to view

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

同样出于原因,将该逻辑放入不同的函数中并添加 @NonCPS 注释,这将阻止 jenkins 在该函数为 运行 时尝试序列化状态,正如您目前拥有的那样,有一个小它仍有可能因该异常而中断,请参阅