chengluo
chengluo

Reputation: 21

How to retrieve build job status (success/error) for using in post-build steps in pipeline?

In my build job on Jenkins via pipeline, I need a post-build steps which depend on the build status. If the job was successful then do 'this'. Otherwise do 'that'.

How can I retrieve the build job status, using pipeline, and save it e.g. in an environment variable for using it in the post-build steps?

Upvotes: 0

Views: 1990

Answers (2)

Hieu Huynh
Hieu Huynh

Reputation: 1083

Define color in top of pipeline (easy to track success/failure) and add post action after stages section

def COLOR_MAP = ['SUCCESS': 'good', 'FAILURE': 'danger', 'UNSTABLE': 'danger', 'ABORTED': 'danger']

pipeline {
  agent any 

  options {
    ansiColor('xterm')
  }

  stages {
    stage("Build") {
    }
    stage("Deploy") {
    }
  } //end of stages

  post {
    always {
      slackSend (color: COLOR_MAP[currentBuild.currentResult], message: "Job: *${env.JOB_NAME}, build #${env.BUILD_NUMBER}* is *`${currentBuild.currentResult}`* \nRun in ${currentBuild.durationString} - <${env.BUILD_URL}|Go to this job>")
    }
  }
}

Configure slack integration in https://plugins.jenkins.io/slack

Upvotes: 2

psalvi21
psalvi21

Reputation: 143

You can use the post block to perform actions based on build status as shown below:

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>)");


        }

    }

Upvotes: 0

Related Questions