这是一个老问题,您可能已经找到了自己的解决方法,但我有类似的需求,我想无论如何我都会分享我的解决方案。我想做的是生成一封每日一次的失败状态作业摘要电子邮件。从根本上讲,这与为单个作业发送每日一次的成功报告非常相似。
我的解决方案使用 Groovy 构建步骤以及 Email-Ext 插件的预发送脚本功能。我的想法来自于纳布尔线程 http://jenkins-ci.361315.n4.nabble.com/generating-a-daily-weekly-report-on-job-status-by-mail-td3766477.html上面的评论中引用了。也可以看看电子邮件分机食谱 https://wiki.jenkins-ci.org/display/JENKINS/Email-ext+Recipes在詹金斯网站上。
这是确定哪些构建失败的初始 groovy 脚本,配置如下执行系统 Groovy 脚本。您可以执行类似的操作来确定构建是否成功或失败:
// List the names of jobs you want to ignore for this check
ignore = [ ]
// Find all failed and unstable jobs
failed = hudson.model.Hudson.instance.getView("All").items.findAll{ job ->
job.getDisplayName() != "Daily Jenkins Job Nag" &&
!ignore.contains(job.getDisplayName()) &&
job.isBuildable() &&
job.lastCompletedBuild &&
(job.lastCompletedBuild.result == hudson.model.Result.FAILURE ||
job.lastCompletedBuild.result == hudson.model.Result.UNSTABLE)
}
// Log the job names so the build results are legible
failed.each { job ->
println(job.getDisplayName() +
" " + job.lastCompletedBuild.result +
" at build " + job.lastCompletedBuild.number +
" (" + job.lastCompletedBuild.timestamp.format("yyyy-MM-dd'T'HH:mm ZZZZ") + ")");
}
// Return failure if there are any failed jobs
return failed.size
然后,在下面可编辑的电子邮件通知部分,我设置了 Email-Ext 插件来通知失败。我设置内容类型 to 纯文本(文本/纯文本), left 默认内容空,并将以下内容设置为预发送脚本:
failed = hudson.model.Hudson.instance.getView("All").items.findAll{ job ->
job.getDisplayName() != "Daily Jenkins Job Nag" &&
job.isBuildable() &&
job.lastCompletedBuild &&
(job.lastCompletedBuild.result == hudson.model.Result.FAILURE ||
job.lastCompletedBuild.result == hudson.model.Result.UNSTABLE)
}
def output = StringBuilder.newInstance()
output << "<html>\n"
output << " <body>\n"
output << "<p>Jenkins reports the following failed jobs:</p>"
output << " <ul>\n"
failed.each { job ->
url = hudson.model.Hudson.instance.rootUrl + job.url + "/" + job.lastCompletedBuild.number + "/"
output << " <li>"
output << "<a href=\"" + url + "\">" + job.displayName + "</a>"
output << " " + job.lastCompletedBuild.result
output << " at build " + job.lastCompletedBuild.number
output << " (" + job.lastCompletedBuild.timestamp.format("yyyy-MM-dd'T'HH:mm ZZZZ") + ")"
output << "</li>\n"
}
output << " </ul>\n"
output << " </body>\n"
output << "</html>"
msg.setContent(output.toString(), "text/html")
关键是你可以访问msg
对象,这是一个哑剧讯息 http://docs.oracle.com/javaee/6/api/javax/mail/internet/MimeMessage.html。您可以将 MIME 消息的内容设置为您想要的任何内容。
在本例中,我将生成失败作业的列表,但在您的情况下,这将是您希望在每日一次的成功报告中收到的任何消息。根据您的需要,您可以让 Email-Ext 为每个构建发送结果,而不仅仅是失败的构建。