Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins get list of builds and parameters

Tags:

jenkins

api

I would like to make an API call to Jenkins to fetch a list of builds along with their parameters and status. We currently pass a git commit sha1 as a parameter to build a specific branch. Any ideas how I can get this information easily?

like image 676
Joe Avatar asked May 15 '13 22:05

Joe


People also ask

How do I get Jenkins build parameters?

Defining Build Parameters Any Jenkins job or pipeline can be parameterized. All we need to do is check the box on the General settings tab, “This project is parameterized”: Then we click the Add Parameter button.

How do I get parameters in Jenkins pipeline?

You can generate the parameter pipeline code block easily using the Jenkins pipeline generator. You will find the Pipeline syntax generator link under all the pipeline jobs, as shown in the image below. Navigate to the pipeline generator in Jenkins and under steps, search for properties, as shown below.

How do I get a list of all jobs in Jenkins?

Go to Script Console under Manage Jenkins, this script will print the name of all jobs including jobs inside of a folder and the folders themselves: Jenkins. instance. getAllItems(AbstractItem.


1 Answers

Combining @user1255162 's comment to an answer. I had to query set of builds and print its parameter for a report. Here is the code snippet in groovy

import groovy.json.JsonSlurper


def root = "<url to job>"
def options = "/api/json?tree=builds[actions[parameters[name,value]],result,building,number,duration,estimatedDuration]"

def jsonSlurper = new JsonSlurper()
def text = new URL("${root}/${options}").text
def data = jsonSlurper.parseText(text)

data["builds"].each { buildsdata ->
    def result = buildsdata["result"]
    def num = buildsdata["number"]
    print("${root}/${num}/parameters  |")
    buildsdata["actions"].each { actions ->
        if (actions["_class"].equals("hudson.model.ParametersAction")) {
            actions["parameters"].sort({it.name}).each { param ->
                    print("${param.name}=${param.value}|")
            }
        }
    }
    println("")
}
like image 120
Jayan Avatar answered Oct 13 '22 11:10

Jayan