user6017989
user6017989

Reputation:

Grouping post conditions in a Jenkins declarative pipeline

Is there a way to group post conditions in a Jenkins declarative pipeline ?

For instance, I want to do the same thing for statuses aborted failure and success. Is there a shorter way to do it than the following ?

post {
  aborted { sendNotification(currentBuild.result, "$LIST_NOTIFICATION_JENKINS") 
  failure { sendNotification(currentBuild.result, "$LIST_NOTIFICATION_JENKINS")     
  success { sendNotification(currentBuild.result, "$LIST_NOTIFICATION_JENKINS")
}

Upvotes: 6

Views: 5905

Answers (1)

Erik Shreve
Erik Shreve

Reputation: 304

There is the 'always' condition:

post {
  always {sendNotification(currentBuild.result, "$LIST_NOTIFICATION_JENKINS")}
}

The 'always' condition will run regardless of the stage result.

See the documentation on the post section.

If you want a set of common actions between just a few conditions, for example if you wanted to do the same thing for failure and aborted, I would recommend creating a function in your script to call from the failure and aborted post conditions.

You can also do something like the following:

always {
    script{
        if (currentBuild.currentResult == "ABORTED" || currentBuild.currentResult == "FAILURE")
        {
            echo "was aborted or failed"
        }
    }
}

Upvotes: 5

Related Questions