我想在定义管道构建作业的Mailer中利用Jenkins现有的Jenkinsfile插件。考虑到以下简单的失败脚本,我希望在每次构建时都会收到一封电子邮件。
stage 'Test'
node {
try {
sh 'exit 1'
} finally {
step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: 'me@me.com', sendToIndividuals: true])
}
}生成的输出是:
Started by user xxxxx
[Pipeline] stage (Test)
Entering stage Test
Proceeding
[Pipeline] node
Running on master in /var/lib/jenkins/jobs/rpk-test/workspace
[Pipeline] {
[Pipeline] sh
[workspace] Running shell script
+ exit 1
[Pipeline] step
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 1
Finished: FAILURE正如您所看到的,它确实记录了在故障发生后立即执行管道step,但是没有生成任何电子邮件。
利用mailer的其他自由式工作中的电子邮件可以正常工作,它只是通过管道作业调用。
这是与Jenkins 2.2和mailer 1.17一起运行的。
,我应该通过一种不同的机制来调用失败的构建电子邮件吗?--我不需要mail步骤的所有开销,只需要关于失败和恢复的通知。
发布于 2016-05-11 21:07:49
在管道失败中,sh不会立即将currentBuild.result设置为FAILURE,而它的初始值是null。因此,依赖像Mailer这样的构建状态的构建步骤可能看起来不正确。
您可以通过添加调试打印来检查它:
stage 'Test'
node {
try {
sh 'exit 1'
} finally {
println currentBuild.result // this prints null
step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: 'me@me.com', sendToIndividuals: true])
}
}整个管道都由Jenkins提供的异常处理程序包装,这就是Jenkins最终将构建标记为失败的原因。
因此,如果您想使用Mailer,您需要正确地维护构建状态。例如:
stage 'Test'
node {
try {
sh 'exit 1'
currentBuild.result = 'SUCCESS'
} catch (any) {
currentBuild.result = 'FAILURE'
throw any //rethrow exception to prevent the build from proceeding
} finally {
step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: 'me@me.com', sendToIndividuals: true])
}
}如果不需要重新抛出异常,可以使用catchError。它是一个管道--在管道中捕捉其作用域内的任何异常,将其打印到控制台并设置构建状态。例如:
stage 'Test'
node {
catchError {
sh 'exit 1'
}
step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: 'me@me.com', sendToIndividuals: true])
}发布于 2016-09-14 20:58:39
除了izzekil的出色回答外,您还可以根据提交作者选择电子邮件收件人。您可以使用电子邮件分机进行此操作(基于它们的管道实例):
step([$class: 'Mailer',
notifyEveryUnstableBuild: true,
recipients: emailextrecipients([[$class: 'CulpritsRecipientProvider'],
[$class: 'RequesterRecipientProvider']])])如果您使用的是最近的电子邮件分机(2.50+),您可以在管道中使用:
emailext(body: '${DEFAULT_CONTENT}', mimeType: 'text/html',
replyTo: '$DEFAULT_REPLYTO', subject: '${DEFAULT_SUBJECT}',
to: emailextrecipients([[$class: 'CulpritsRecipientProvider'],
[$class: 'RequesterRecipientProvider']]))如果您没有使用声明式Jenkinsfile,则需要放置checkout scm以便Jenkins能够找到提交者。见詹金斯-46431。
如果你还在使用旧版本的电子邮件分机,你会点击詹金斯-25267。您可以自己发送HTML电子邮件:
def emailNotification() {
def to = emailextrecipients([[$class: 'CulpritsRecipientProvider'],
[$class: 'DevelopersRecipientProvider'],
[$class: 'RequesterRecipientProvider']])
String currentResult = currentBuild.result
String previousResult = currentBuild.getPreviousBuild().result
def causes = currentBuild.rawBuild.getCauses()
// E.g. 'started by user', 'triggered by scm change'
def cause = null
if (!causes.isEmpty()) {
cause = causes[0].getShortDescription()
}
// Ensure we don't keep a list of causes, or we get
// "java.io.NotSerializableException: hudson.model.Cause$UserIdCause"
// see http://stackoverflow.com/a/37897833/509706
causes = null
String subject = "$env.JOB_NAME $env.BUILD_NUMBER: $currentResult"
String body = """
<p>Build $env.BUILD_NUMBER ran on $env.NODE_NAME and terminated with $currentResult.
</p>
<p>Build trigger: $cause</p>
<p>See: <a href="$env.BUILD_URL">$env.BUILD_URL</a></p>
"""
String log = currentBuild.rawBuild.getLog(40).join('\n')
if (currentBuild != 'SUCCESS') {
body = body + """
<h2>Last lines of output</h2>
<pre>$log</pre>
"""
}
if (to != null && !to.isEmpty()) {
// Email on any failures, and on first success.
if (currentResult != 'SUCCESS' || currentResult != previousResult) {
mail to: to, subject: subject, body: body, mimeType: "text/html"
}
echo 'Sent email notification'
}
}发布于 2017-05-19 08:56:43
我认为在jenkins管道中发送邮件通知的一个更好的方法是使用管道的post部分,如詹金斯医生中所描述的,而不是使用think:
pipeline {
agent any
stages {
stage('whatever') {
steps {
...
}
}
}
post {
always {
step([$class: 'Mailer',
notifyEveryUnstableBuild: true,
recipients: "example@example.com",
sendToIndividuals: true])
}
}
}
}
}https://stackoverflow.com/questions/37169100
复制相似问题