Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins pipeline email not sent on build failure

I am using following step in my pipeline jenkins job:

step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: '[email protected]', sendToIndividuals: true])

But no email was sent when the build failed (i.e. error). Any pointers why?

P.S. Emails can be sent from this server, I have tested that.

like image 911
Saikat Avatar asked May 01 '17 14:05

Saikat


1 Answers

Use Declarative Pipelines using new syntax, for example:

pipeline {
    agent any
    stages {
        stage('Test') {
            steps {
                sh 'echo "Fail!"; exit 1'
            }
        }
    }
    post {
        always {
            echo 'This will always run'
        }
        success {
            echo 'This will run only if successful'
        }
        failure {
            mail bcc: '', body: "<b>Example</b><br>\n\<br>Project: ${env.JOB_NAME} <br>Build Number: ${env.BUILD_NUMBER} <br> URL de build: ${env.BUILD_URL}", cc: '', charset: 'UTF-8', from: '', mimeType: 'text/html', replyTo: '', subject: "ERROR CI: Project name -> ${env.JOB_NAME}", to: "[email protected]";
        }
        unstable {
            echo 'This will run only if the run was marked as unstable'
        }
        changed {
            echo 'This will run only if the state of the Pipeline has changed'
            echo 'For example, if the Pipeline was previously failing but is now successful'
        }
    }
}

You can find more information in the Official Jenkins Site:

https://jenkins.io/doc/pipeline/tour/running-multiple-steps/

Note that this new syntax make your pipelines more readable, logic and maintainable.

like image 190
Daniel Hernández Avatar answered Sep 28 '22 19:09

Daniel Hernández