Reputation: 22769
I would like to add general functionality to my Jenkins pipeline script, similar to what you can do with built-in functions (timestamps, ansiColor):
options {
timestamps()
ansiColor 'xterm'
// failureNotification() <- I want to add this
}
How do I write a function in the script so that it can be used as an option?
Upvotes: 1
Views: 3128
Reputation: 1509
Currently I don't believe that's possible with the declarative syntax that you're using. You could write your own Jenkins plugin to do this, but that could get hairy.
If you're willing to use a slightly more complicated syntax, I would look at this blog post: https://jenkins.io/blog/2017/02/15/declarative-notifications/
Essentially, you'll need to create a shared groovy library and use the step from that to manage your notification step. There's a few steps to this:
@Library('<library name you picked here>')
Now you should be able to call sendFailureNotification() at the end of your Jenkinsfile. Maybe in a post stage like so?:
post { failure { sendFailureNotification() } }
Upvotes: 2