Chris Maes
Chris Maes

Reputation: 37782

Jenkins declarative pipeline post failure or fixed

Jenkins declarative pipelines allow to declare different post stages to be executed. I have something like this:

post {
    fixed {
        emailext (
            ... code to send email
        )
    }
    failure {
        emailext (
            ... code to send email
        )
    }
}

my real code is much longer and exactly duplicate. Does something exist to combine this code together? Something like

post {
    fixed || failure {
        emailext (
            ... code to send email
        )
    }
}

Upvotes: 1

Views: 1000

Answers (1)

Justin Nimmo
Justin Nimmo

Reputation: 131

One round about way to do it is to define the code to send email in a function and call the function from both scenarios.

def SendEmail(){
   ... code to send email
}
post {
    fixed {
        emailext (
            SendEmail()
        )
    }
    failure {
        emailext (
            SendEmail()
        )
    }
}

If you're truly looking for a fixed || failure, I'd suggest looking into the when(){} command. https://jenkins.io/blog/2018/04/09/whats-in-declarative/

Upvotes: 1

Related Questions