Different post actions being called at the same time

Hey there,

I have a multibranch pipeline in Jenkins 2.440.3, with a complex post:

  • If the build fails (which I hope it is triggered when the compilation fails) is set to send a message on teams saying it so;
  • If the build is unstable (which I hope it is tied with the junit plugin and it is related to the tests), we parse the reports and properly post a list of errors on teams.

Because we have an integration with Bitbucket, we need to set the unstable status to fail, so it appears properly on BB interface. Because I don’t want the unstable to trigger the failure post, I set up a post for unstable that just changes the pipeline result in the end.

The thing is: It seems that when the unstable post changes the result to failure, it also triggers the failure post, and we end up with two messages.

Does this actually happens? Is there a better way to handle this situation?

Here’s our post

    post {
        // A failure may happen when the build failed (or we broke this script)
        failure {
            script {
                message = "The **build** of commit ${env.GIT_COMMIT} is **bad, no good, nuh-uh**.\n(This is before we could run any tests.)"
                office365ConnectorSend webhookUrl: WEBHOOK,
                                message: message,
                                color: '#e50f0f'
                return
            }
        }

        // Unstable happens when the tests fails. That means the call of `junit`
        // returned a health check of "BAD".
        unstable {
            script {
                // We need to set the build status to FAILURE 'cause, otherwise, Bitbucket
                // will not mark the build as bad in its interface.
                currentBuild.result = 'FAILURE'
                return
            }
        }

        always {
            script {
                echo "Current status: ${currentBuild.result}"
                if (currentBuild.result == 'NOT_BUILT') {
                    currentBuild.result = 'ABORTED'
                }
            }
        }
    }

Hello and welcome to this community, @JBiason. :wave:

As far as I know, your observation is correct. :+1:
In Jenkins, when you change the build status from UNSTABLE to FAILURE in the unstable post block, it will indeed trigger the failure post block. This is because the failure post block is designed to run whenever the build status is set to FAILURE, regardless of how or where it was set.

One way to handle this situation would maybe be to use a global variable to track whether the unstable post block has been executed, and then check this variable in the failure post block. :thinking:

If the variable indicates that the unstable post block has been executed, you could skip the failure post block. :person_shrugging:

Here’s an untested example of how you could perhaps modify your post blocks to implement this:

// Define a global variable to track whether the unstable post block has been executed
def unstableExecuted = false

post {
    failure {
        script {
            // Check if the unstable post block has been executed
            if (!unstableExecuted) {
                message = "The **build** of commit ${env.GIT_COMMIT} is **bad, no good, nuh-uh**.\n(This is before we could run any tests.)"
                office365ConnectorSend webhookUrl: WEBHOOK,
                                message: message,
                                color: '#e50f0f'
            }
            return
        }
    }

    unstable {
        script {
            // Set the global variable to indicate that the unstable post block has been executed
            unstableExecuted = true

            // Set the build status to FAILURE
            currentBuild.result = 'FAILURE'
            return
        }
    }

    always {
        script {
            echo "Current status: ${currentBuild.result}"
            if (currentBuild.result == 'NOT_BUILT') {
                currentBuild.result = 'ABORTED'
            }
        }
    }
}

Here, the failure post block will only send the message if the unstable post block has not been executed. This should prevent duplicate messages from being sent when the build status is changed from UNSTABLE to FAILURE.

Ah, thanks for the info @poddingue. I’ll change the pipeline to reflect that.

1 Like