9
votes

I am using Jenkins declarative pipeline and want to perform some post build actions depending on the build status.

To be more precise, I want to send an email when the build status changed (from success to failure, or success to unstable, or failure to success).

Here is my pipeline:

pipeline {
    agent none
    stages {
        stage('test') {
            agent any
            steps {                
                sh './tests.sh'
            }
        }
    }
    post {
        changed {
            // Send different emails depending on build status
            // Success       -> anything else
            // Anything else -> Success
        }
    }
}

Any idea ?

3
So what's your actual problem? Sending the email? - StephenKing
Sorry I realize that I was not that clear... my problem is that I would like to send an email on every failures, but only one when the status moves to success. I'd like to recover the feature "receive mail on failed builds and on fixed builds". - Mikael Gibert
In fact, my question was more generalist and about "how can I write conditions inside a post action block" - Mikael Gibert
You mentioned changed in your question. Why don't you use that? - StephenKing
I wanted to have notifications on every status except success (i.e, I want a new email at each failure). But you are right, I finally used exactly the snippet I posted and live with this. I have another use case where I need to perform some post actions only on certain branches, but your other response with script blocks solves this problem too! Thank you for your help! - Mikael Gibert

3 Answers

16
votes

For writing conditions you can define your own methods.

For example, if you want to send an email only when the build status changes:

def notifyStatusChangeViaEmail(buildStatus) {
    def status

    switch (buildStatus) {
        case 'SUCCESS':
            status = 'is now green again!'
            break

        case 'UNSTABLE':
            status = 'has become unstable..'
            break

        case 'FAILURE':
            status = 'has turned RED :('
            break
    }

    emailext (
        subject: "Job '${env.JOB_NAME}' ${status}",
        body: "See ${env.BUILD_URL} for more details",
        recipientProviders: [
            [$class: 'DevelopersRecipientProvider'], 
            [$class: 'RequesterRecipientProvider']
        ]
    )
}

pipeline {
    ...

    post {
        changed {
            // Will trigger only when job status changes: GREEN -> RED, RED -> GREEN, etc
            notifyStatusChangeViaEmail(currentBuild.currentResult)
        }
    }
}

Ideally, you would also want to put notifyStatusChangeViaEmail method definiton in your shared pipeline library so that it could be re-used in other jobs/pipelines.

1
votes

Refer to this pipeline:

post {
        success {
            emailext ( 
                subject: '${DEFAULT_SUBJECT}'+'SUCESSFUL', 
                body: '${DEFAULT_CONTENT}',
                to: '${EMAIL_RECIPIENTS}'
                );
                slackSend (color: 'good', message: ":csp_operational: ${env.JOB_NAME} - #${env.BUILD_NUMBER} Success (<${env.BUILD_URL}|Open>)");


        }
        failure {
            emailext ( 
                subject: '${DEFAULT_SUBJECT}'+'FAILED!', 
                body: '${DEFAULT_CONTENT}',
                to: '${EMAIL_RECIPIENTS}'
                );
                slackSend (color: 'danger', message: ":x: ${env.JOB_NAME} - #${env.BUILD_NUMBER} Failure (<${env.BUILD_URL}|Open>)");


        }

    }

You can set the Default Email parameters using Extended Email Plugin, Jenkins-> Configure Jenkins -> Extended Email Configuration.

0
votes

Exemple : failure & unstable to success

if (currentBuild.result == 'SUCCESS') {  if(hudson.model.Result.FAILURE.equals(currentBuild.rawBuild.getPreviousBuild()?.getResult()) || hudson.model.Result.UNSTABLE.equals(currentBuild.rawBuild.getPreviousBuild()?.getResult())) {
SEND MAIL()
}
}

for send email : https://jenkins.io/blog/2017/02/15/declarative-notifications/