0

Ideally I need a script that outputs the following information in a CSV format that's easy to import into Excel:

job name,number of times run in last year,number of times run overall,last run status

For that job, output no individual run details.

Tried this on my Jenkins: List Jenkins job build detials for last one year along with the user who triggered the build.

but got an error:

java.lang.NullPointerException: Cannot invoke method getShortDescription() on null object
    at org.codehaus.groovy.runtime.NullObject.invokeMethod(NullObject.java:91)
    at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:48)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)

Any idea what in the Groovy needs changing? or is there a better solution?

Thanks all!

Ian W
  • 4,559
  • 2
  • 18
  • 37
Speee
  • 1
  • 3
  • you have just one call to getShortDescription() in referenced code - just drop it. – daggett Dec 20 '22 at 21:27
  • `getShortDescription() on null object` suggests there's no `build.getCauses[0]` for a given job ie: `job.fullName`. Examine the job and see what's odd about it. My guess is it never run or may have been running for the first time while the script was running. Probably `build.getCauses()[0]?.getShortDescription()` will solve the issue. – Ian W Dec 21 '22 at 00:44
  • Also note, the script analyzes saved job build logs, so if old builds have been discarded [locally](https://plugins.jenkins.io/discard-old-build/) or [globally](https://plugins.jenkins.io/build-discarder/), [global option since 2.204.6](https://www.jenkins.io/changelog-stable/#v2.222.1), numbers will be inaccurate. – Ian W Dec 21 '22 at 00:44
  • PS: an [upvote](https://stackoverflow.com/questions/64501034/list-jenkins-job-build-detials-for-last-one-year-along-with-the-user-who-trigger) would be nice. – Ian W Dec 21 '22 at 03:48

1 Answers1

0

Thanks to @daggett and @ian . Both worked.

I went with IANS :

def jobNamePattern ='.*'   // adjust to folder/job regex as needed
def daysBack = 365   // adjust to how many days back to report on
def timeToDays = 24*60*60*1000  // converts msec to daysprintln "Job Name: ( # builds: last ${daysBack} days / overall )  Last Status\n   Number | Trigger | Status | Date | Duration\n"Jenkins.instance.allItems.findAll() {
  it instanceof Job && it.fullName.matches(jobNamePattern)
}.each { job ->
  builds = job.getBuilds().byTimestamp(System.currentTimeMillis() - daysBack*timeToDays, System.currentTimeMillis())
  println job.fullName + ' ( ' + builds.size() + ' / ' + job.builds.size() + ' )  ' + job.getLastBuild()?.result
  
  // individual build details
  builds.each { build ->
    println '   ' + build.number + ' | ' + build.getCauses()[0]?.getShortDescription() + ' | ' + build.result + ' | ' + build.getTimestampString2() + ' | ' + build.getDurationString()
  }
}
return
Ian W
  • 4,559
  • 2
  • 18
  • 37
Speee
  • 1
  • 3
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 23 '22 at 05:08