Jenkins文件在失败时发送电子邮件



我在jenkins上设置了多分支项目。这是我的詹金斯档案:

properties([[$class: 'BuildDiscarderProperty', strategy: [$class: 'LogRotator', artifactDaysToKeepStr: '14', artifactNumToKeepStr: '10', daysToKeepStr: '14', numToKeepStr: '10']]])
node {
checkout scm
def lib = load 'cicd/shared-library.groovy'
stage('build project') {
lib.compileProject()
}
stage('Unit test') {
lib.executeUnitTest()
}
stage('Archive log files') {
def files = ["failure_services.txt", "unit_test.log"]
lib.archiveFile(files, "unit_test_result.tar.xz")
}
stage('send email') {
def subject = "Test Result"
def content = 'ًLog file attached'
def toList = ["aaa@gmail.com", "bbb@gmail.com"]
def ccList = ["xxx@gmail.com", "zzz@gmail.com"]
def attachmentFiles = ["unit_test_result.tar.xz"]
lib.sendMail(toList, ccList, subject, content, attachmentFiles)
}
cleanWs()
}

有时Unit test阶段会导致错误,因此在这种情况下不执行下一步。

我希望在任何情况下执行send email阶段
如何在JenkinsFile上进行配置?

在脚本管道(共享库(中,您可以在函数中定义发送电子邮件的步骤。在try-catch finally块中封装管道步骤,并在finally部分调用函数send_email((。

对于声明性管道,您可以将管道步骤封装在catchError块中,并在其外部发送电子邮件

示例:

properties([[$class: 'BuildDiscarderProperty', strategy: [$class: 'LogRotator', artifactDaysToKeepStr: '14', artifactNumToKeepStr: '10', daysToKeepStr: '14', numToKeepStr: '10']]])
node {
catchError {
checkout scm
def lib = load 'cicd/shared-library.groovy'
stage('build project') {
lib.compileProject()
}
stage('Unit test') {
lib.executeUnitTest()
}
stage('Archive log files') {
def files = ["failure_services.txt", "unit_test.log"]
lib.archiveFile(files, "unit_test_result.tar.xz")
}
}
stage('send email') {
def subject = "Test Result"
def content = 'ًLog file attached'
def toList = ["aaa@gmail.com", "bbb@gmail.com"]
def ccList = ["xxx@gmail.com", "zzz@gmail.com"]
def attachmentFiles = ["unit_test_result.tar.xz"]
lib.sendMail(toList, ccList, subject, content, attachmentFiles)
}
cleanWs()
}

很可能您只需要在管道中添加一个post部分。

后段定义了在管道或阶段运行完成时运行的一个或多个附加步骤(取决于后段在管道中的位置(。post可以支持以下任何post条件块:始终、更改、修复、回归、中止、失败、成功、不稳定、不成功和清理。这些条件块允许根据管道或阶段的完成状态在每个条件内执行步骤。条件块的执行顺序如下所示。

在此处的文档中查找更多信息

最新更新