使用Jenkins';梅勒';内部管道工作流
我想在定义管道构建作业的使用Jenkins';梅勒';内部管道工作流,jenkins,jenkins-plugins,jenkins-pipeline,Jenkins,Jenkins Plugins,Jenkins Pipeline,我想在定义管道构建作业的Jenkinsfile中利用来自Jenkins的现有插件。鉴于以下简单的失败脚本,我希望每次构建时都会收到一封电子邮件 #!棒极了 阶段“测试” 节点{ 试一试{ sh“1号出口” }最后{ 步骤([$class:'Mailer',notifyEveryUnstableBuild:true,收件人:'me@me.com,sendToIndividuals:true]) } } 生成的输出是: Started by user xxxxx [Pipeline] stage
Jenkinsfile
中利用来自Jenkins的现有插件。鉴于以下简单的失败脚本,我希望每次构建时都会收到一封电子邮件
#!棒极了
阶段“测试”
节点{
试一试{
sh“1号出口”
}最后{
步骤([$class:'Mailer',notifyEveryUnstableBuild:true,收件人:'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
如您所见,它确实记录了在故障发生后立即执行管道步骤
,但没有生成电子邮件
利用mailer
的其他自由风格作业中的电子邮件工作正常,它只是通过管道作业调用
这是运行詹金斯2.2和梅勒1.17
是否有其他机制可以调用失败的生成电子邮件?我不需要
邮件步骤的所有开销,只需要关于故障和恢复的通知。管道内故障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])
}
除了izzekil的优秀答案外,您还可以根据提交作者选择电子邮件收件人。您可以使用来执行此操作(基于):
步骤([$class:'Mailer',
notifyEveryUnstableBuild:true,
收件人:EmailExtrecPients([[$class:'CulpritsRecipientProvider'],
[$class:'RequesterRecipientProvider']])]
如果您使用的是最新的电子邮件ext(2.50+),您可以在管道中使用:
emailext(正文:'${DEFAULT_CONTENT}',mimeType:'text/html',
replyTo:“$DEFAULT\u replyTo”,主题:“${DEFAULT\u subject}”,
收件人:EmailExtrecients([[$class:'CulpritsRecipientProvider'],
[$class:'RequesterRecipientProvider']]))
如果您没有使用声明性Jenkins文件,则需要将签出scm
,以便Jenkins可以找到提交者。看
如果你仍然使用旧版本的email ext,你会点击。您可以滚动自己的HTML电子邮件:
def emailNotification(){
def to=EmailExtrecPients([[$class:'CumpritsRecipientProvider'],
[$class:'developerRecipientProvider'],
[$class:'RequesterRecipientProvider']]
字符串currentResult=currentBuild.result
字符串previousResult=currentBuild.getPreviousBuild().result
def causs=currentBuild.rawBuild.getcauss()
//例如,“由用户启动”,“由scm更改触发”
def cause=null
如果(!cause.isEmpty()){
原因=原因[0]。getShortDescription()
}
//确保我们没有列出原因,否则我们会
//“java.io.NotSerializableException:hudson.model.Cause$UserIdCause”
//看http://stackoverflow.com/a/37897833/509706
原因=空
String subject=“$env.JOB\u NAME$env.BUILD\u NUMBER:$currentResult”
字符串正文=“”“
生成$env.Build\u编号在$env.NODE\u名称上运行,并以$currentResult终止。
生成触发器:$cause
见:
"""
字符串log=currentBuild.rawBuild.getLog(40).join('\n')
如果(currentBuild!=“成功”){
body=body+“”“
输出的最后几行
$log
"""
}
if(to!=null&!to.isEmpty()){
//在任何失败和第一次成功时发送电子邮件。
如果(currentResult!=“成功”| | currentResult!=以前的结果){
邮件收件人:收件人,主题:主题,正文:正文,mimeType:“text/html”
}
回显“已发送电子邮件通知”
}
}
我认为在jenkins管道中发送邮件通知的更好方法是使用管道的post部分,如中所述,而不是使用try-catch:
这个满了吗?非常接近我试图做的,但更多的是指在失败后使用邮件插件。下面的答案中详细介绍了在管道过程中如何设置状态,这是使用非挂起构建状态的插件所必需的。这是一个奇妙而详细的答案,让我回到了正确的轨道上。谢谢你!如何确保生成失败(或不稳定)后的成功生成触发新电子邮件?@asmaier-AFAIK-Mailer-plugin不提供此功能。您可以使用groovy编写脚本(检查以前的构建状态)或查看一下。我自己也弄明白了:为了确保构建失败(或不稳定)后的成功构建会触发新的电子邮件,请在try块末尾添加currentBuild.result='SUCCESS'
(在sh'exit 1'之后)@izzekil Mailer插件明确具有此功能,请参阅上的“用法”下的第二点。极好的补充答案!ThanksHow应该是调试Jenkins文件的吗?例如,在哪个范围内emailext
可用?Mailer参数sendToIndividuals:true
是否应该向提交作者发送电子邮件?我已经尝试了您的技巧,但这不会发送“返回正常”通知。如何在post.always部分设置currentBuild.result
?@prat0318我认为currentBuild.result
在这一点上是固定的,因为它是一个post操作。您只能读取currentBuild.r的结果
pipeline {
agent any
stages {
stage('whatever') {
steps {
...
}
}
}
post {
always {
step([$class: 'Mailer',
notifyEveryUnstableBuild: true,
recipients: "example@example.com",
sendToIndividuals: true])
}
}
}
}
}