我在管道jenkins工作中使用以下步骤:
step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: 'my@xyz.com', sendToIndividuals: true])
但是在构建失败时(即错误)没有发送电子邮件。任何指针为什么?
P.S。可以从这台服务器发送电子邮件,我已经测试过了。
答案 0 :(得分:4)
使用新语法使用声明性管道,例如:
pipeline {
agent any
stages {
stage('Test') {
steps {
sh 'echo "Fail!"; exit 1'
}
}
}
post {
always {
echo 'This will always run'
}
success {
echo 'This will run only if successful'
}
failure {
mail bcc: '', body: "<b>Example</b><br>\n\<br>Project: ${env.JOB_NAME} <br>Build Number: ${env.BUILD_NUMBER} <br> URL de build: ${env.BUILD_URL}", cc: '', charset: 'UTF-8', from: '', mimeType: 'text/html', replyTo: '', subject: "ERROR CI: Project name -> ${env.JOB_NAME}", to: "foo@foomail.com";
}
unstable {
echo 'This will run only if the run was marked as unstable'
}
changed {
echo 'This will run only if the state of the Pipeline has changed'
echo 'For example, if the Pipeline was previously failing but is now successful'
}
}
}
您可以在Jenkins官方网站上找到更多信息:
https://jenkins.io/doc/pipeline/tour/running-multiple-steps/
请注意,这种新语法使您的管道更具可读性,逻辑性和可维护性。
答案 1 :(得分:0)
您需要手动将构建结果设置为失败,并确保它在工作区中运行。例如:
try {
throw new Exception('fail!')
} catch (all) {
currentBuild.result = "FAILURE"
} finally {
node('master') {
step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: 'my@xyz.com', sendToIndividuals: true])
}
}
插件正在检查currentBuild.result
状态,通常在脚本完成后才会更改。