Snehil Mishra
Snehil Mishra

Reputation: 31

Send a automated email from jenkins for passed build in every hour exclude failed on

How to integrate the bitbucket repository to Jenkins and get it executed every 5 mins. get an email notification for every failed builds upfront and get a notification of all passed build every hour. Use Case

  1. Create an automation test for a few page URLs - Done
  2. Execute it every 5 mins (Schedule with Jenkins) - Done
  3. Send an email as soon as any build got fail - Done
  4. Send an email every 1 hour for all passed build in last one hours (exclude the fail one) - Need help on this

Upvotes: 0

Views: 1207

Answers (1)

JRichardsz
JRichardsz

Reputation: 16564

You need to combine:

Every hour trigger

enter image description here

Query builds with groovy + mail

import hudson.model.*

node {
    def nowDateInMilliseconds = new Date().getTime();
    def job = Jenkins.instance.getItemByFullName('my_job-full_name');
    def lastHourSucessBuild = [];
    
    for(def build in job.builds){
        if (build.result!=hudson.model.Result.SUCCESS) continue;
        
        def lastSuccessBuildDateOnMillis = build.getTime().getTime();
        long diff = nowDateInMilliseconds - lastSuccessBuildDateOnMillis;
        long diffMinutes = diff / (60 * 1000);         
        println build.number+" : Time in minutes: " + diffMinutes
        if(diffMinutes<60)lastHourSucessBuild.add(build.number)
    }
    
    println "Last hour success builds: " + lastHourSucessBuild
    
    emailext (
      subject: "Last hour success builds",
      body: "Last hour success builds: " + lastHourSucessBuild,
      to: "[email protected]"
    )
}

Full job config here

Advices

I advice to try bit by bit:

  • config the emailext and send a mail as demo
  • config the build trigger and print a message every minute

Only if you achieve these demos, combine all in one scripted pipeline ;)

Upvotes: 1

Related Questions