There are multiple ways to retrieve data from the Jenkins API, depending on formatting, details, etc.
In this example, I'll use a public-facing Jenkins service from the Ubuntu build pipeline.
Use curl
, jq
and sort
on command line
curl -s https://jenkins.qa.ubuntu.com/view/Precise/view/All%20Precise/api/json | jq -r '.jobs[] | [.color, .name] | @csv' | sort
The output from this pipeline subdirectory (Precise/All Precise) is parsed where blue
= passing, red
= failure.
Here's a sample of the output:
...
"blue","precise-server-i386_raid1"
"blue","precise-server-i386_samba-server"
"blue","precise-server-i386_tomcat-server"
"blue","precise-server-i386_virtual-host"
"red","precise-adt-apport"
"red","precise-adt-apport-armhf"
"red","precise-adt-chromium-browser"
"red","precise-adt-chromium-browser-armhf"
"red","precise-adt-chromium-browser-ppc64el"
...
Use a higher-level language to parse the output. XML, Python, Ruby are all viable options, all depending on your use case and how you want to present the data further.
No example given here, as the terms are still too broad on what data you want retrieved and how you want to send it along.
** EDIT **
In the comments, @shabunc explains that the desire is for information (still unclear as to what information or data is being searched for, so I'll use one example) about the last completed builds.
Using the API request modifier of depth
to the original call opens up another level of details agains the original query endpoint.
In this example, we expand the same pipeline (view) with one more level, using depth=1
query parameter, and retrieve the last completed build number for each job.
curl -s 'https://jenkins.qa.ubuntu.com/view/Precise/view/All%20Precise/api/json?depth=1' | jq -r '.jobs[] | [.color, .name, .lastCompletedBuild.number] | @csv' | sort
Results now include the build number of each job:
...
"blue","precise-server-i386_raid1",910
"blue","precise-server-i386_samba-server",909
"blue","precise-server-i386_tomcat-server",901
"blue","precise-server-i386_virtual-host",905
"red","precise-adt-apport",2
"red","precise-adt-apport-armhf",1
"red","precise-adt-chromium-browser",2
"red","precise-adt-chromium-browser-armhf",1
"red","precise-adt-chromium-browser-ppc64el",2
...
You could explore all the fields returned with the following command:
curl -s 'https://jenkins.qa.ubuntu.com/view/Precise/view/All%20Precise/api/json?depth=1' | jq '.jobs[0]'
This will return the first job in the list with all the fields returned from the depth
level modifier.
You can then use the jq accessors to pick the fields you want, as can be seen in previous examples.