import javax.mail.*
import javax.mail.internet.*
import jenkins.model.*
import java.text.SimpleDateFormat
import java.util.*;
import java.util.Calendar;
import java.lang.System;
import org.apache.tools.ant.Project
import org.apache.tools.ant.ProjectHelper
def env="Dev_Jenkins_Server";
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
sdf.setTimeZone(TimeZone.getTimeZone("EST"));
def d=new Date();
def oneHourPrior=new Date(System.currentTimeMillis() - 3600 * 1000);
def currentTime=sdf.format(d);
def monitoringStartTime=trim(oneHourPrior)
def totalMonitoringJobs=0;
def testSuiteIndex=0;
def passedTestSuiteCount=0;
def failedTestSuiteCount=0;
def passedJobsList=[];
def failedJobsList=[];
def totalJobsList=[];
println "Monitoring Start Time: "+monitoringStartTime ;
println "Monitoring Current Time: "+currentTime;
def matchedJobs = Jenkins.instance.items.findAll { job ->
}
println "*********************************************************************************"
println "Total Monitoring Suites Count:"+matchedJobs.size();
totalMonitoringJobs=matchedJobs.size();
println "***********************************Listing all Available Monitoring Projects**********************************************"
def index=0;
matchedJobs.each { job ->
index=index+1;
}
println "*********************************************************************************\n\n"
println "********************************Todays Execution Summary Results***************************************"
matchedJobs.each { job ->
def numbuilds = job.builds.size()
if (numbuilds == 0) {
println ' -> no build'
return
}
def lastbuild = job.builds[0]
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.setTime(monitoringStartTime);
cal2.setTime(lastbuild.getTime());
if (cal2.after(cal1)) {
testSuiteIndex=testSuiteIndex+1
def rightPadding=50;
int requiredPadding=50-(jobName.size());
while(requiredPadding>0){
jobName=jobName+" "
requiredPadding--;
}
def jobInfo=jobName+'\tlastbuild: ' + lastbuild.displayName + ' = ' + lastbuild.result + ', time: ' + lastbuild.getTime();
String latestbuildStatus=lastbuild.result;
if( latestbuildStatus.equals("SUCCESS")){
passedTestSuiteCount=passedTestSuiteCount+1
//println "Found a Success Project"
jobInfo="\t\t"+ passedTestSuiteCount+" . "+jobInfo
passedJobsList.add(jobInfo)
}
if( latestbuildStatus.equals("FAILURE")){
failedTestSuiteCount=failedTestSuiteCount+1
jobInfo="\t\t"+ failedTestSuiteCount+" . "+jobInfo
failedJobsList.add(jobInfo)
}
}
}
String passedJobsInfo= passedJobsList.join("\n")
String failedJobsInfo= failedJobsList.join("\n")
def jobsList ="";
def jobIndex=0;
totalJobsList.each{
jobIndex=jobIndex+1;
jobsList=jobIndex+". "+jobsList+it
}
String cumulativeStatus="PASS"
if(failedJobsList.size()>0){
cumulativeStatus="FAIL"
}
String emailBody="""
*******************************Todays Cumulative Test Execution Summary Report*****************
\tMonitoring Start Time:\t\t${monitoringStartTime}
\tReport Genretated Time:\t${currentTime}
\tTotal Monitoring Jobs Count Scheduled to run in Monitoring Window: ${totalMonitoringJobs}
\tTotal Jobs Executed Today(As of Now): ${testSuiteIndex}
\tTotal Jobs Passed Today(As of Now): ${passedJobsList.size()}
\tTotal Jobs Failed Today:(As of Now): ${failedJobsList.size()}
\tCumulative Result: ${cumulativeStatus}
***********************************************************************"""+"\n\n\t----->>>>>Failed Jobs Count: ${failedJobsList.size()} \n"+failedJobsInfo+"\n\n\t----->>>>>Passed Jobs Count: ${passedJobsList.size()}\n"+passedJobsInfo+"\n\n"
println emailBody;
String subject="${env} Jenkins Server:[ ${cumulativeStatus}] ${currentTime}: Cumulative Test Execution Summary Report";
public static Date trim(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.SECOND, 0);
calendar.add(Calendar.MINUTE, -60);
calendar.add(Calendar.HOUR_OF_DAY,0);
return calendar.getTime();
}
def padRight(String s, int n) {
String.format("%1-" + n + "s", s);
}
//You have Following use email service and send it :
def emailSubject=subject
def emailRecipientsList="email1,email2"
def email_subject= subject
def email_body= emailBody
Above groovy script collects the pass failed jobs info and composes a mail subject and mail body.
2. Create a Freestyle project
3. Good to 'Build. section of the free style job (Please look at attached pic -1)
4. Select Option : Execute system Groovy script (Please look at attached pic -1)
5. Select option Groovy Command
6. Copy the above groovy Script in the next text field of build area config
7. If you wan to run this script periodically every day define a cron entry build triggers section similar to the attached pic-3
I am filtering jobs based on the job name, i.e if job name contains 'Monitor' token then those jobs only checked and consolidated report will be generated.