How to limit Jenkins API response to last n build IDs How to limit Jenkins API response to last n build IDs jenkins jenkins

How to limit Jenkins API response to last n build IDs


The tree query parameter allows you to explicitly specify and retrieve only the information you are looking for, by using an XPath-ish path expression. The value should be a list of property names to include, with sub-properties inside square braces. Try tree=jobs[name],views[name,jobs[name]] to see just a list of jobs (only giving the name) and views (giving the name and jobs they contain). Note: for array-type properties (such as jobs in this example), the name must be given in the original plural, not in the singular as the element would appear in XML (). This will be more natural for e.g. json?tree=jobs[name] anyway: the JSON writer does not do plural-to-singular mangling because arrays are represented explicitly.

For array-type properties, a range specifier is supported. For example, tree=jobs[name]{0,10} would retrieve the name of the first 10 jobs. The range specifier has the following variants:

{M,N}: From the M-th element (inclusive) to the N-th element (exclusive).{M,}: From the M-th element (inclusive) to the end.{,N}: From the first element (inclusive) to the N-th element (exclusive). The same as {0,N}.{N}: Just retrieve the N-th element. The same as {N,N+1}.Another way to retrieve more data is to use the depth=N query parameter . This retrieves all the data up to the specified depth. Compare depth=0 and depth=1 and see what the difference is for yourself. Also note that data created by a smaller depth value is always a subset of the data created by a bigger depth value.

Because of the size of the data, the depth parameter should really be only used to explore what data Jenkins can return. Once you identify the data you want to retrieve, you can then come up with the tree parameter to exactly specify the data you need.

I'm on version 1.509.4. which doesn't support range specifier.

Source: http://ci.citizensnpcs.co/api/


You can create an xml object with the build numbers via xpath and parse it yourself with via different means.

http://xxx/api/xml?xpath=//build/number&wrapper=meep

Creates an xml that looks like:

<meep>     <number>n</number>     <number>n+1</number>      ...     <number>m</number> </meep>

And will be populated with the build numbers n through m that are currently in jenkins for the specified job in the url. You can substitute anything for the word "meep", that will become the wrapper object for the newly created xml object.

How are you collecting/manipulating the api xml output once you get it? Because there is a solution here for How do I select the last N elements with XPath?. I tried using some of these xpath manipulations but I couldn't get it to work when playing with the url in my browser; it might work if you are doing something else.

When I get the xml object, I happen to manipulate it via shell scripts.

#!/bin/sh# NOTE: To get the url to work with curl, you need a valid jenkins user and api token# Put all build numbers in a variable called build_idsbuild_ids="$(curl -sL --user ${_jenkins_api_user}:${_jenkins_api_token} \    "${_jenkins_url}/job/${_job_name}/api/xml?xpath=//build/number&wrapper=meep" \    | sed -e 's/<[^>]*>/ /g' | sed -e 's/  / /g')"# Print the last 5 items with awkecho "${build_ids}" | awk '{n = 5; for (--n; n >= 0; n--){ printf "%s\t",$(NF-n)} print ""}';

Once you have your xml object you can essentially parse it however you want.

NOTE: I am running Jenkins ver. 2.46.1