One of the strengths of the new Jenkins declarative pipelines functionality is the abstraction of the post steps. Within the scripted pipeline it was vital to track currentBuild.result to differentiate the build status.

Declarative pipeline assumes post directive after stages (so still within the pipeline block) which will execute based on build status: always, success, failure and unstable. In a declarative way, one would have to either use a post-build step that was already aware of the build status, or use a post-build parser for log information. Additionally, it can be embedded into stage block to react to stage status rather than the whole job. See an example for Hipchat Plugin.

pipeline {  
    stages {
        stage('some stage') {
            steps {}
        }
    }
    post {
        always {
            echo 'I will do this 
                  no matter what the status is'
        }
        success {
            hipchatSend(color: 'GREEN',
            message: 'OK')
        }
        failure {
            hipchatSend(color: 'RED',
            message: 'FAIL')
        }
    }
}

In this case, there's no need to really go through the output.
You need to bear in mind though, that there are plugins which barely support scripted pipeline, let alone the declarative one. A great plugin - Stash Build Notifier Plugin for Jenkins is one of these. There are some snippets on how to work with it, but they are solely for the purpose of scripted pipeline. You need to abstract the steps performed by the plugin as a Groovy function and inspect currentBuild for status.

Declarative pipeline makes it much easier. True, to report the status correctly you still need to modify currentBuild.result - but there's no need to play try catch. For instance:

    post {
        success {
            script {
                currentBuild.result = 'SUCCESS'
            }
            step([$class: 'StashNotifier'])
        }
     }

StashNotifier will know that the build is successful simply by virtue of knowing what the property currentBuild.result returns.

post/always

A good way of using always directive under post is for tasks which need to execute apart from post-failure or post-success activities. If you specify always, the task will run no matter what the status of the build is and will not interfere with whatever the final status is.

pipeline {  
    ...
    post {
        always {
            archiveArtifacts '**/*.tar.gz'
        }
    }
}

The command above will always archive the artefacts regardless of the final status. Example: useful for repetitive task list automation.

Tags: jenkins