CuriousToLearn
CuriousToLearn

Reputation: 153

Jenkins-cli - help required to get list of builds in the job

I'm using jenkins-cli to connect with Jenkins from my shell script. I need to fetch the console output of the first 20 builds in the given job and do some computation.

I would want to know how can i get the list of builds available in a jenkins job using jenkins-cli or any other easily available ways, so that i can fetch the console output of my job using jenkins-cli using build number of the job.

Thanks in Advance

Upvotes: 1

Views: 2768

Answers (1)

Songy
Songy

Reputation: 851

I use the REST api instead of the cli they provide. I find it to be quicker and more flexible.

For your case I would generate a URL from a job name that returns XML data on all successful builds, then go though each of those builds and get the console output from another generated URL.

URL that will will return XML data containing the build number of all successful builds.

jenkins:8080/job/example_job/api/xml?tree=allBuilds[result,number]&xpath=//allBuild[result='SUCCESS']/number&wrapper=nums

jenkins:8080/job/example_job/api/xml? - Jenkins server with the XML api open on a job.

tree=allBuilds[result,number]& - Only ask for the number and result of all the builds belonging to a job.

xpath=//allBuild[result='SUCCESS']/number& - Get Jenkins to do work on that data to filter it down to a list of number tags which belong to an allBuild tag who had a result tag that had the value SUCCESS.

wrapper=nums - As xpath has split off all the number so they don't have a parent we need to wrap them up under another tag, nums.

With those num XML tags you can then just create a URL to get he console output.

jenkins:8080/job/example_job/132/consoleText

I see you have a Python tag so I am assuming your script it in Python. This means you can use minidom and urllib2 to parse the XML data and retrieve the webpages.

Upvotes: 5

Related Questions