我想利用Jenkins 现有的Mailer插件来Jenkinsfile定义管道构建作业.鉴于以下简单的失败脚本,我希望每个版本都有一封电子邮件.
#!groovy
stage 'Test'
node {
try {
sh 'exit 1'
} finally {
step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: 'me@me.com', sendToIndividuals: true])
}
}
Run Code Online (Sandbox Code Playgroud)
构建的输出是:
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
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,它确实记录了它step …
我正在尝试将现有的Jenkins管道转换为新的Declarative Pipeline,我想知道如何正确处理邮件通知?
我目前正在使用此代码:
node {
try {
...
currentBuild.result = 'SUCCESS'
} catch (any) {
currentBuild.result = 'FAILURE'
throw any
} finally {
step([$class: 'Mailer',
notifyEveryUnstableBuild: true,
recipients: "baptiste.wicht@gmail.com",
sendToIndividuals: true])
}
}
Run Code Online (Sandbox Code Playgroud)
它运行良好,但我没有看到如何使用新的声明性语法.我认为可以通过使用post()和不同的通知来完成某些事情,但我不确切知道如何.我试过这个:
post {
always {
step([$class: 'Mailer',
notifyEveryUnstableBuild: true,
recipients: "baptiste.wicht@gmail.com",
sendToIndividuals: true])
}
}
Run Code Online (Sandbox Code Playgroud)
但问题是它不会发送任何"恢复正常"的邮件.
如何在Jenkins声明性管道中使用Mailer插件才能发送"返回正常"邮件?
应该再次使用try/catch围绕所有声明性语法吗?