Jenkins: sending successful email only once a day (although the work is done @hourly)

I have a jenkins job configured to run hourly. I want a successful build email to be sent only once a day. Email-Ext gives me the ability to send emails for all successes, failures, etc. But what I wanted was the ability to send successful email only once.

+4
source share
3 answers

Well, there is no plugin that can do this for you. The default email feature in Jenkins is very simple and works fine. There is an email-ext plugin , and this can do more for you.

First of all, with Email-ext you can configure a specific trigger to send an email notification - it can be successful or unsuccessful, which is similar to Jenkins default behavior. But then you have more refined, as the first failure and still failure. This will give you great control over when and to whom (your recipient list, committer or respondent) your Jenkins will send an email. In my case, a good configuration here will help a lot with the mail traffic created by Jenkins. And you can send special messages specific in a specific situation to a specific list of people - great!

Another option, if you really don't need this level of control and want to limit email traffic to one vaulted day, is to set up a mailing list. Most mailing list engines allow you to send a daily digest of all mail traffic to the list. This should be enough, although I really do not feel that this is actually a good option in the long run. I would definitely try Email-ext module.

+2
source

This is an old question, and you probably already found your own workaround, but I had a similar need, and I thought I would share my solution anyway. What I was trying to do was generate a one-time email of tasks in a failed state. This is basically very similar to sending a success report once a day for one job.

My solution uses the Groovy build step in conjunction with the Email Sub Ext plugin pre-send feature. I got the idea from the Nabble thread mentioned in the comments above. See Also Email-Ext Recipes on Jenkins website.

Here's the initial Groovy script, which determines which builds fail, configured in Run Groovy Script System . You could do something like this to determine if your creation was successful or unsuccessful:

// List the names of jobs you want to ignore for this check ignore = [ ] // Find all failed and unstable jobs failed = hudson.model.Hudson.instance.getView("All").items.findAll{ job -> job.getDisplayName() != "Daily Jenkins Job Nag" && !ignore.contains(job.getDisplayName()) && job.isBuildable() && job.lastCompletedBuild && (job.lastCompletedBuild.result == hudson.model.Result.FAILURE || job.lastCompletedBuild.result == hudson.model.Result.UNSTABLE) } // Log the job names so the build results are legible failed.each { job -> println(job.getDisplayName() + " " + job.lastCompletedBuild.result + " at build " + job.lastCompletedBuild.number + " (" + job.lastCompletedBuild.timestamp.format("yyyy-MM-dd'T'HH:mm ZZZZ") + ")"); } // Return failure if there are any failed jobs return failed.size 

Then, in the Editable Email Notification section, I installed the Email-Ext plugin for error notification. I set the Content Type to plain text (text / plain), left the Content blank by default, and set the following as Pre-send Script :

 failed = hudson.model.Hudson.instance.getView("All").items.findAll{ job -> job.getDisplayName() != "Daily Jenkins Job Nag" && job.isBuildable() && job.lastCompletedBuild && (job.lastCompletedBuild.result == hudson.model.Result.FAILURE || job.lastCompletedBuild.result == hudson.model.Result.UNSTABLE) } def output = StringBuilder.newInstance() output << "<html>\n" output << " <body>\n" output << "<p>Jenkins reports the following failed jobs:</p>" output << " <ul>\n" failed.each { job -> url = hudson.model.Hudson.instance.rootUrl + job.url + "/" + job.lastCompletedBuild.number + "/" output << " <li>" output << "<a href=\"" + url + "\">" + job.displayName + "</a>" output << " " + job.lastCompletedBuild.result output << " at build " + job.lastCompletedBuild.number output << " (" + job.lastCompletedBuild.timestamp.format("yyyy-MM-dd'T'HH:mm ZZZZ") + ")" output << "</li>\n" } output << " </ul>\n" output << " </body>\n" output << "</html>" msg.setContent(output.toString(), "text/html") 

The key is that you have access to the msg object, which is MimeMessage . You can set the content of the MIME message at any time convenient for you.

In this case, I create a list of unsuccessful tasks, but in your case it will be any message that you want to receive for your report on the results once a day. Depending on what you need, you can send an Email-Ext result for each assembly, and not just for failed assemblies.

+1
source

How about suppressing email messages if the time has not passed since the previous message? Although this is not exactly what was requested, pre-submitting a script like this might be worth considering its simplicity?

 if (build.result != hudson.model.Result.SUCCESS) { cancel = true; } else { try { long minEmailGap = 1000 * 60 * 60 * 16; // 16 hours in milliseconds File file = new File("/TimestampForMyJob.txt"); if (file.exists() == false) { file.createNewFile(); } else { long currentTime = (new Date()).getTime(); if (file.lastModified() + minEmailGap > currentTime) { cancel = true; } else { file.setLastModified(currentTime); } } } catch(IOException e) { // We can't tell whether the e-mail should be sent out or not, so we do nothing // and it just gets sent anyway - probably the best we can do with this exception. } } 
+1
source

All Articles