尝试使用以下代码来触发多分支管道作业的电子邮件通知:
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...
无效。我发现this和this 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
CurrentResult1 =空
CurrentResult2 =空
CurrentResult3 =空
如果某些测试失败,使用currentBuild.currentResult
只会向我显示SUCCESS
或FAILED
,但不会显示UNSTABLE
。
问题出在哪里?
答案 0 :(得分:3)
构建状态为null,直到设置它或者直到作业完成为止。您是否正在使用会导致构建不稳定的任何单元测试步骤?
您不需要使用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