Home > Mobile >  Jenkins file send email on failure
Jenkins file send email on failure

Time:11-13

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 ?

CodePudding user response:

Most likely you just need to add a post section in your pipeline.

The post section defines one or more additional steps that are run upon the completion of a Pipeline’s or stage’s run (depending on the location of the post section within the Pipeline). post can support any of the following post-condition blocks: always, changed, fixed, regression, aborted, failure, success, unstable, unsuccessful, and cleanup. These condition blocks allow the execution of steps inside each condition depending on the completion status of the Pipeline or stage. The condition blocks are executed in the order shown below.

Find further information in the docs here

CodePudding user response:

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()
}
  • Related