Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins: Sending email based on each build step result in same Jenkins Job

Tags:

email

jenkins

I am just wondering how to send out email with Email-ext plugin based on each build step result on same Jenkins Job. Here is my scenario

My Jenkins job has 3 Build steps

Build Step1:

   Pull latest code from github and Build the app - Gradle task which build the Jar

Build Step2:

   Run all Tests against that Jar - Gradle Task which runs all Junit tests and provide Report files.

Post Build Action:

   If Build step1 fails -> Send email telling Build fails and Console Log with Exception detials

   If Build success and Tests run -> Send email with Test Reports

I think i would need to write Pre-send script for Email-Ext plugin. But i am not sure how to write this script. Please assist.

like image 848
user1093097 Avatar asked Dec 25 '22 08:12

user1093097


1 Answers

In a normal configuration, if Build Step 1 fails, Build Step 2 will not execute anyways.

And if Build Step 2 (Test) fail, the build will be marked "Unstable", not "Failed".

Email-ext already supports triggers for these.

  • Scroll to your Editable Email Notification section.
  • Click Advanced Settings... button.
  • Look under Triggers section.
  • Click Add Trigger button.

You want to configure a trigger for "Failure" and a trigger for "Unstable" (you will also probably want a trigger for "Success")

In your "Failure" trigger, write the email content you want for when the build fails (Build Step 1 failure will cause this for you).
In your "Unstable" trigger, write the email content you want for when the test cases fail.
In your "Success" trigger, write the email content you want for when everything was successful and you want to send the test report.

Alternative:
Or you can look into Any Build Step plugin (which should allow post-build actions, like email-ext, to be executed as a build step), and Conditional Build Step plugin (which allows to create conditions for each build step). Combine the two and create conditions when you want to trigger the email-ext build step. However I have not tried this and can't guarantee this will work.

like image 141
Slav Avatar answered Feb 07 '23 09:02

Slav