'currentBuild.result null on master

I am trying to send a Slack message when the build succeeds/fails through a scripted Jenkins pipeline, but I have noticed that currentBuild.result is null on master branch for some reason. Is there a better way to do this?

try {
    //pipline code
} finally {
    branch_name = env.BRANCH_NAME
    if (branch_name == 'master') {
        message_body = """"""
        if (currentBuild.result == 'SUCCESS') {
            message_body = """:green_check: Build succeeded for `$branch_name`. See $env.BUILD_URL for details"""
        }
        if (currentBuild.result == 'FAILURE') {
            message_body = """:red_x: Build failed for `$branch_name`. See $env.BUILD_URL for details"""
        }
        if (message_body != '') {
            slackSend(channel: "#channel", message: message_body.stripIndent().trim())
        }
    }
}


Solution 1:[1]

Are you using declarative or scripted pipelines? There are some differences between currentBuild.resultand currentBuild.currentResult. See here

The best way is to use a post condition.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MaGro