1

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 = ["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()
}

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 ?

mah454
  • 1,571
  • 15
  • 38
  • 1
    you can refer to this comment [Send an email on Jenkins pipeline failure](https://stackoverflow.com/a/47882245/15648070) – Eyal Solomon Nov 10 '21 at 12:06

2 Answers2

1

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

theraulpareja
  • 496
  • 3
  • 9
  • thank you for answer , but i can not understand how can use loaded library in post section ? , I do this before ask this question but does not work , can you give me a example script under my script structure ? – mah454 Nov 10 '21 at 19:09
1

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 = ["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()
}