Issue
I setup multi branch project on jenkins . this is my JenkinsFile:
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 = ["[email protected]", "[email protected]"]
def ccList = ["[email protected]", "[email protected]"]
def attachmentFiles = ["unit_test_result.tar.xz"]
lib.sendMail(toList, ccList, subject, content, attachmentFiles)
}
cleanWs()
}
sometimes Unit test
stage result a error , so in this case next steps not executed .
I want send email
stage executed under any circumstances .
How can config that on JenkinsFile ?
Solution
In scripted pipeline (shared library) you can define send email steps in function. Wrap your pipeline steps in try-catch-finally block and call function send_email() in finally part.
For declarative pipeline, you can wrap your pipeline steps in catchError block and send email outside it.
Example:
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 = ["[email protected]", "[email protected]"]
def ccList = ["[email protected]", "[email protected]"]
def attachmentFiles = ["unit_test_result.tar.xz"]
lib.sendMail(toList, ccList, subject, content, attachmentFiles)
}
cleanWs()
}
Answered By - Vladislav Kovalyov