0

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

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...不管用。我发现这个这个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

当前结果1=空

当前结果2=空

当前结果3=空

UsingcurrentBuild.currentResult将只显示SUCCESSor FAILED,但不会显示UNSTABLE,以防某些测试失败。

任何想法问题出在哪里?

4

1 回答 1

3

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

你不需要使用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

于 2017-11-23T03:27:18.520 回答