我正在使用Jenkins声明性管道,并希望根据构建状态执行一些后期构建操作。
更确切地说,我想在构建状态发生变化时发送电子邮件(从成功变为失败,或成功变为不稳定或未成功)。
这是我的管道:
pipeline {
agent none
stages {
stage('test') {
agent any
steps {
sh './tests.sh'
}
}
}
post {
changed {
// Send different emails depending on build status
// Success -> anything else
// Anything else -> Success
}
}
}
有什么想法吗?
答案 0 :(得分:10)
对于写条件,您可以定义自己的方法。
例如,如果您只想在构建状态发生变化时发送电子邮件:
def notifyStatusChangeViaEmail(buildStatus) {
def status
switch (buildStatus) {
case 'SUCCESS':
status = 'is now green again!'
break
case 'UNSTABLE':
status = 'has become unstable..'
break
case 'FAILURE':
status = 'has turned RED :('
break
}
emailext (
subject: "Job '${env.JOB_NAME}' ${status}",
body: "See ${env.BUILD_URL} for more details",
recipientProviders: [
[$class: 'DevelopersRecipientProvider'],
[$class: 'RequesterRecipientProvider']
]
)
}
pipeline {
...
post {
changed {
// Will trigger only when job status changes: GREEN -> RED, RED -> GREEN, etc
notifyStatusChangeViaEmail(currentBuild.currentResult)
}
}
}
理想情况下,您还希望将notifyStatusChangeViaEmail
方法定义在shared pipeline library中,以便可以在其他作业/管道中重复使用。
答案 1 :(得分:1)
参考此管道:
post {
success {
emailext (
subject: '${DEFAULT_SUBJECT}'+'SUCESSFUL',
body: '${DEFAULT_CONTENT}',
to: '${EMAIL_RECIPIENTS}'
);
slackSend (color: 'good', message: ":csp_operational: ${env.JOB_NAME} - #${env.BUILD_NUMBER} Success (<${env.BUILD_URL}|Open>)");
}
failure {
emailext (
subject: '${DEFAULT_SUBJECT}'+'FAILED!',
body: '${DEFAULT_CONTENT}',
to: '${EMAIL_RECIPIENTS}'
);
slackSend (color: 'danger', message: ":x: ${env.JOB_NAME} - #${env.BUILD_NUMBER} Failure (<${env.BUILD_URL}|Open>)");
}
}
您可以使用扩展电子邮件插件,Jenkins-&gt;设置默认电子邮件参数。配置Jenkins - &gt;扩展电子邮件配置。
答案 2 :(得分:0)
例如:失败&amp;成功不稳定
if (currentBuild.result == 'SUCCESS') { if(hudson.model.Result.FAILURE.equals(currentBuild.rawBuild.getPreviousBuild()?.getResult()) || hudson.model.Result.UNSTABLE.equals(currentBuild.rawBuild.getPreviousBuild()?.getResult())) {
SEND MAIL()
}
}
发送电子邮件:https://jenkins.io/blog/2017/02/15/declarative-notifications/