3

We can get the list of all jobs with their current state through this API call:

/view/<pipeline-name>/api/json

Also, we can get data on the last completed build of any particular job:

/job/<jobname>/lastCompletedBuild/api/json

My question is - what is the best way to fetch data about all last completed builds in pipeline? Currently I'm requesting this kind of information in parallel, but hopefully there's an easier way to do it. Even if "easier" means to win something in terms of simplicity but to loose in parallelism.

shabunc
  • 87
  • 1
  • 2
  • 12
  • I can't see how to do that but what about checking the RSS for latest against failed. – gm3dmo Apr 08 '15 at 08:02
  • Can you clarify what details you are looking for, and whether using an API library is acceptable? What output are you comfortable using? – Mike Fiedler Apr 16 '15 at 14:23
  • @MikeFiedler I need information about all last completed builds in pipeline. Had changedlastSuccessfulBuild to lastCompletedBuild in question to make it more practical. – shabunc Apr 17 '15 at 15:35
  • @shabunc completed could be pass and/or fail. Also, what kind of information? – Mike Fiedler Apr 17 '15 at 15:47
  • @MikeFiedler precisely, I want to know either job had failed or passed - and the same for all jobs. – shabunc Apr 17 '15 at 15:53
  • @shabunc I would consider the first pipeline api endpoint - and parse the json for each job by `color` - the color relates to the status of pass/fail/abort - does that make sense? I could write an example as an answer. – Mike Fiedler Apr 17 '15 at 15:56

1 Answers1

2

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.

  1. 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" ...

  2. 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.

Mike Fiedler
  • 2,162
  • 1
  • 17
  • 34
  • @shabunc Bounty is over in 1 hour - I think I provided a decent method here and got no feedback. – Mike Fiedler Apr 19 '15 at 21:50
  • Mike thanks! it looks like I still missing something - how can I tell apart job that is building while preivous jobs was succesful from job that is building but previous job has failed. In other words, I can not see how this helps. – shabunc Apr 21 '15 at 22:20
  • @shabunc I guess this goes back to my question of "what data are you looking to get out of the api" - the last part is the first time you've mentioned builds prior to the current one. I guess I don't understand the structure of your pipeline jobs - can you describe more? – Mike Fiedler Apr 21 '15 at 23:22
  • I've mentioned that I need data about all last completed jobs in pipeline. Actually I apologize if was not too clear, English is not my native language. Will try re-edit question to make it clearer. – shabunc Apr 22 '15 at 00:44