Emiliano
Emiliano

Reputation: 728

Send an email when Jenkins has specific exception or error

On Jenkins sometimes errors occurs but I want notify different people depending the error.

IE: When SSH command do an exception I want don't send email or send to a dummy account and when a production check fail send to me. (all this happen in one task)

is it possible? how?

thanks in advance

Upvotes: 0

Views: 2332

Answers (1)

Coder
Coder

Reputation: 51

Use Jenkins Email-ext plugin.

It has the complete flexibility for recipients (and content) based on many conditions, including log content, which allows to analyze which command has failed and change the recipient list accordingly.

Because you don't provide much details about your task and what exactly is "SSH do an exception" or "production check", it is hard to be very specific, but you can, for example, examine log for "production check has failed" or any other desired text and allow or suppress email based on this examination.

  • Install the plugin
  • Select the checkbox labeled "Editable Email Notification" in the "Post-build Actions" section.
  • Leave "Failure - Any" trigger if you want to prrocess mail configuration for any failure; otherwise, delete failure and add desired triggers from the dropdown

There are multiple ways to allow or cancel email sending, the complete description is in documentation on the plugin page. The simplest way is to use Pre-send Script feature. From documentation section Advanced configuration / Pre-send Script:

You may also cancel sending the email by setting the boolean variable "cancel" to true.

In Pre-send Script add a Groovy script to evaluate a condition and set a variable cancel to true or false in order to send or prevent email. For example, based on "production check has failed" line existense in the build log:

cancel = build.logFile.text.readLines().any { it =~ /.*production check has failed.*/ }

Upvotes: 1

Related Questions