0
votes

Say I have a simple Jenkins pipeline file as below:

pipeline {
    agent any

    stages {
        stage('Test') {
            steps {
                sh ...
            }
        }

        stage('Build') {
            steps {
                sh ...
            }
        }

        stage('Publish') {
            when {
                buildingTag()
            }

            steps {
                sh ...
                send_slack_message("Built tag")
            }
        }
    }

    post {
        failure {
            send_slack_message("Error building tag")
        }
    }
}

Since there's a lot non-tag builds everyday, I don't want to send any slack message about non-tag builds. But for the tag builds, I want to send either a success message or a failure message, despite of which stage it failed.

So for the above example, I want:

  1. When it's a tag build, and stage 'Test' failed, I shall see a "Error building tag" message. (This is a yes in the example)
  2. When it's a tag build, and all stages succeeded, I shall see a "Built tag" message. (This is also a yes in the example)
  3. When it's not a tag build, no slack message will ever been sent. (This is not the case in the example, for example, when the 'Test' stage fails, there's will be a "Error building tag" message)

As far as I know, there's no such thing as "conditional post section" in Jenkins pipeline syntax, which could really help me out here. So my question is, is there any other way I can do this?

3

3 Answers

3
votes
    post {
        failure {
            script {
                if (isTagBuild) {
                    send_slack_message("Error building tag")
                }
            }
        }
    }

where isTagBuild is whatever way you have to differentiate between a tag or no tag build.

You could also apply the same logic, and move send_slack_message("Built tag") down to a success post stage.

0
votes

In the postbuild step you can also use script step inside and use if. And inside this if step you can add emailext plugin.

0
votes

Well, for those who just want some copy-pastable code, here's what I ended-up with based on @eez0's answer.

pipeline {
    agent any

    environment {
        BUILDING_TAG = 'no'
    }

    stages {
        stage('Setup') {
            when {
                buildingTag()
            }

            steps {
                script {
                    BUILDING_TAG = 'yes'
                }
            }
        }

        stage('Test') {
            steps {
                sh ...
            }
        }

        stage('Build') {
            steps {
                sh ...
            }
        }

        stage('Publish') {
            when {
                buildingTag()
            }

            steps {
                sh ...
            }
        }
    }

    post {
        failure {
            script {
                if (BUILDING_TAG == 'yes') {
                    slackSend(color: '#dc3545', message: "Error publishing")
                }
            }
        }

        success {
            script {
                if (BUILDING_TAG == 'yes') {
                    slackSend(color: '#28a745', message: "Published")
                }
            }
        }
    }
}

As you can see, I'm really relying on Jenkins built-in buidingTag() function to help me sort things out, by using an env-var as a "bridge". I'm really not good at Jenkins pipeline, so please leave comments if you have any suggestions.