-1

My requirement is to create a Jenkins job that runs everyday EOD, where

  1. I declare a list of job names, the job should iterate through each job and print all successful build history that happened that day.

  2. The printed message should print current iteration Job name, then each Build number, Build status, build completed timestamp, git commit id, commit message, commit author.

This printed message should be emailed to declared recipients.

Please share chunks of code or references that will be helpful. I am new to Jenkins and groovy. The chunks will be helpful for many as well. Solutions that works with only default plugins will be grateful.

torek
  • 448,244
  • 59
  • 642
  • 775
Sivaramakrishnan
  • 350
  • 3
  • 18

1 Answers1

3

Your question is too abstract and contains multiple questions. I typically don't like answering such questions, but here is a starting point for you. The following script will get you all the Jobs and Builds that Ran during the past 24 Hours.

Jenkins.instance.getAllItems(Job.class).each { jobitem ->
      def jobName = jobitem.getFullName()
      def jobInfo = Jenkins.instance.getItemByFullName(jobName)
  
      // Current time in Miliseconds
      def now = new Date().getTime()
      def before24Hours = now - (24 * 60 * 60 * 1000)
      println("Now: " + now + " Before24H: " + before24Hours)
      
      jobInfo.getBuilds().byTimestamp(before24Hours, now).each { build ->           
        if(build.getResult().toString().equals('SUCCESS')) {
          println("Job : " + jobName + " || BuildNumber: " + build.getNumber() + " || Timestamp: " + build.getTime())
        }
      }
}

Remember each Build can have multiple commits, hence multiple users. You can get the commit details with this method.

Start building your pipeline with the above and create SO questions for problems you face while doing it. Don't combine multiple questions into the same question.


Update: Full Pipeline to include commit details.

pipeline {
    agent any
    stages {
        stage('Report') {
            steps {
                script {
                    def jobsToInclude = ['Job1', 'Job2']
                    generateReport(jobsToInclude)
                }
            }
        }
    }
}

def generateReport(def jobs) {
      Jenkins.instance.getAllItems(Job.class).each { jobitem ->
          def jobName = jobitem.getFullName()
          if(jobs.contains(jobName)) {
               
              def jobInfo = Jenkins.instance.getItemByFullName(jobName)
              // Current time in Miliseconds
              def now = new Date().getTime()
              def before24Hours = now - (24 * 60 * 60 * 1000)

              jobInfo.getBuilds().byTimestamp(before24Hours, now).each { build ->  
                
                if(build.getResult().toString().equals('SUCCESS')) {
                  println("Job : " + jobName + " || BuildNumber: " + build.getNumber() + " || Timestamp: " + build.getTime())
                  // Get the commits
                  build.getChangeSets().each{change -> 
                    change.getItems().each { item ->
                      println("---" + "COMMITID: " + item.getCommitId() +  " || Message: " + item.getMsg() + " || Author: " + item.getAuthorName())
                    }
                  }   
                }
            }  
         }
    }  
}
ycr
  • 12,828
  • 2
  • 25
  • 45
  • Thank you for sharing. Ofcourse I also don't like to ask such questions. Just thought of having all report related code chunks in single place. I am also asking references so i can get help. I am gonna up vote every useful code shared. If one developer knows some part of this whole req. He can share right! – Sivaramakrishnan Oct 18 '22 at 01:44
  • @Sivaramakrishnan you should probably read https://stackoverflow.com/help/how-to-ask :) Your report requirement will most probably be different from someone else's requirement. So ideally your question has to be separated into 3 questions. – ycr Oct 18 '22 at 01:57
  • @ycr, the question and my [2-yr old response](https://stackoverflow.com/a/64509896/598141) seem very similar to yours. Suggest pointing to mine (or consider upvoting mine ), unless you want elaborate on the git aspect. – Ian W Oct 18 '22 at 05:00
  • 1
    @IanW I really didn't see your answer :) Anyway upvoted. Since you asked, added the full Pipeline to include commit details as well :) – ycr Oct 18 '22 at 12:03
  • @Sivaramakrishnan check the updated answer, it should have all the pieces you need :) – ycr Oct 18 '22 at 12:03