Creating a jenkins pipeline job that will get all the jenkins job name, builds (with build infos e.g. number,status,duration)

Hi we need a job for generating a report of all jobs in the jenkins platform.
the report will also include the builds on that specific job and build information which are number, result, start time, duration and end time.

usually we run this groovy script in jenkins script console to get these info but when we apply this script in a jenkins pipeline , we get a lot of security vulnerability for the methods. Hence, we can’t use it there.

def monthToFilter = Month
def format = “MM-yyyy”
def folder = Jenkins.instance.getItemByFullName(Folder_Name)
def jobs = folder.getAllItems(hudson.model.Job.class)

for (job in jobs) {
def foundBuilds = false // flag to keep track of whether any builds were found for this job
def builds = job.getBuilds()
for (build in builds) {
def buildMonth = new java.text.SimpleDateFormat(format).format(build.getTime())
if (buildMonth.equals(monthToFilter)) {
if (!foundBuilds) {
// Print job name only once when the first matching build is found
println “======Job: ${job.fullName}======”
foundBuilds = true
}
println " Build ${build.number}:"
println " Result: ${build.result}"
println " Start time: ${build.getTime()}"
println " Duration: ${build.getDurationString()}"
println " End time: ${new Date(build.getTimeInMillis() + build.getDuration())}"
}
}
}

That script works without issues. Just need to uncheck the sandbox option so it has full access.

You could also run this in a freestyle job with systemgroovy

tried that. but im getting this

Obviously you’re not creating the job as administrator it seems. You will need to approve the script then.

1 Like

That won’t solve your rights problem, but note that you can use the Jenkins REST API to retrieve the required information and generate the report. Here’s an example script that you can use in a Jenkins pipeline to generate a report of all jobs and builds:

import groovy.json.JsonSlurper

def monthToFilter = '03' // replace with the month you want to filter
def baseUrl = 'http://localhost:8080' // replace with your Jenkins instance URL
def jobsApiUrl = "${baseUrl}/api/json?tree=jobs[name]"
def buildsApiUrl = "${baseUrl}/job/%s/api/json?tree=builds[number,result,timestamp,duration]"

// function to convert timestamp to date string
def formatDate(timestamp) {
    def date = new Date(timestamp)
    def format = new java.text.SimpleDateFormat('yyyy-MM-dd HH:mm:ss')
    format.format(date)
}

// retrieve job names
def response = new URL(jobsApiUrl).getText()
def json = new JsonSlurper().parseText(response)
def jobNames = json.jobs.name

// loop through jobs and builds
jobNames.each { jobName ->
    def buildsResponse = new URL(buildsApiUrl.format(jobName)).getText()
    def buildsJson = new JsonSlurper().parseText(buildsResponse)
    buildsJson.builds.each { build ->
        def buildMonth = new java.text.SimpleDateFormat('MM').format(new Date(build.timestamp))
        if (buildMonth == monthToFilter) {
            // print job name only once when the first matching build is found
            if (buildsJson.builds.indexOf(build) == 0) {
                println "======Job: ${jobName}======"
            }
            println " Build ${build.number}:"
            println " Result: ${build.result}"
            println " Start time: ${formatDate(build.timestamp)}"
            println " Duration: ${build.duration}ms"
            println " End time: ${formatDate(build.timestamp + build.duration)}"
        }
    }
}

Note that you’ll need to replace the monthToFilter variable with the month you want to filter, and update the baseUrl variable with the URL of your Jenkins instance.

1 Like