Jenkins管道Groovy脚本:在Jenkinsfile中使用`mail`
问题描述:
我试图发送电子邮件,当Jenkins失败时建立管道。一个例子可以在这里找到:https://github.com/jenkinsci/pipeline-examples/blob/master/jenkinsfile-examples/nodejs-build-test-deploy-docker-notify/JenkinsfileJenkins管道Groovy脚本:在Jenkinsfile中使用`mail`
我的具体常规脚本如下所示:
#!groovy
node('') {
def env = ["JAVA_HOME=${tool 'jdk1.8.0_131'}", "PATH+MAVEN=${tool 'maven_3.1.1'}/bin:${env.JAVA_HOME}/bin", "PATH+GRADLE=${tool 'gradle_4.1'}/bin:${env.JAVA_HOME}/bin" ]
def err = null
currentBuild.result = "SUCCESS"
try {
stage('errorStage') {
dir('error') {
git url: "unknown", branch: "master"
withEnv(env) {
sh "mvn -Pjenkins-build clean deploy"
}
}
}
} catch (caughtError) {
println "caught error :" + caughtError
err = caughtError
currentBuild.result = "FAILURE"
mail (body:
"Pipeline error: ${err}\nFix me.",
from: '[email protected]',
subject: 'Pipeline build failed',
to: '[email protected]')
} finally {
/* Must re-throw exception to propagate error */
if (err) {
throw err
}
}
}
其实,什么事也没发生,虽然异常被正确的捕捉和生成失败。有什么要求可以使用mail
?!也许另一个Jenkins插件什么的?
由于提前,
西蒙
答
我能够通过使用emailext
插件对詹金斯,而不是mail
解决它自己。代码看起来如下:
emailext body: "Pipeline error: ${err}\nPlease go to ${BUILD_URL} and verify the build",
recipientProviders: [[$class: 'DevelopersRecipientProvider'], [$class: 'RequesterRecipientProvider']],
subject: "'${JOB_NAME}' (${BUILD_NUMBER}) failed",
to: '...'
你在詹金斯配置了你的smtp服务器的地址吗?如果是的话,那么你应该检查地址是否可以通过使用groovy shell的机器到达。 –
它已在'电子邮件通知'部分中正确配置,并且在'我扩展的电子邮件通知'中也是如此 – s1m0nw1