我想在定义管道构建作业的 }
player.x += xBump;
player.y += yBump;
if(player.hitTestObject(exit)) {
gotoAndStop("win");
}
} // closing function here, NOT above
内利用Jenkins现有的Mailer插件。鉴于以下简单的故障脚本,我希望每次构建都会收到一封电子邮件。
Jenkinsfile
构建的输出是:
#!groovy
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
正常工作,只是通过管道工作调用。
这与Jenkins 2.2和邮件程序1.17一起运行。
我应该使用不同的机制来调用失败的构建电子邮件吗?我不需要mailer
步骤的所有开销,只需要关于失败和恢复的通知。
答案 0 :(得分:60)
在管道中失败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
。它是一个内置的Pipeline,可捕获其范围内的任何异常,将其打印到控制台并设置构建状态。示例:
stage 'Test'
node {
catchError {
sh 'exit 1'
}
step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: 'me@me.com', sendToIndividuals: true])
}
答案 1 :(得分:27)
除了izzekil的优秀答案之外,您可能希望根据提交作者选择电子邮件收件人。您可以使用email-ext执行此操作(基于their pipeline examples):
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']]))
如果您没有使用声明性Jenkins文件,则需要放置checkout scm
以便Jenkins可以找到提交者。请参阅JENKINS-46431。
如果您仍然使用旧版本的email-ext,则会点击JENKINS-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'
}
}
答案 2 :(得分:14)
我认为在jenkins管道中发送邮件通知的更好方法是使用jenkins docs中描述的管道的post部分,而不是使用try catch:
pipeline {
agent any
stages {
stage('whatever') {
steps {
...
}
}
}
post {
always {
step([$class: 'Mailer',
notifyEveryUnstableBuild: true,
recipients: "example@example.com",
sendToIndividuals: true])
}
}
}
}
}