尝试使用以下代码触发多分支管道作业的电子邮件通知:
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 }
现在,我面临的问题:
def to = emailextrecipients...
不管用。我发现这个和这个Jenkins Jira 问题可能是原因,但没有解决方法。虽然如果手动启动构建看起来很奇怪,比如我说一个通过 Github Oauth 认证的用户,邮件可以发送。如果 Github 通过 webhook 开始构建,我会在 Jenkins 日志中得到这个:
未向无查看权限的用户 firstname.lastname@domain.com 发送邮件
我看到的第二个问题是 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
将只显示SUCCESS
or FAILED
,但不会显示UNSTABLE
,以防某些测试失败。
任何想法问题出在哪里?