Jenkins API access to Build History

5,684 views
Skip to first unread message

sebastian.nickel

unread,
Jul 8, 2014, 8:02:08 AM7/8/14
to jenkins...@googlegroups.com
Heya,
we are using jenkins in combination with capistrano to deploy the artifacts build by jenkins directly to our servers. To get the URL for an artifact we are trying to get the build number for a specific git branch/tag we build in jenkins (doing this with json API calls). We noticed that the API shows only one entry for a specific branch/tag (always the last one). Sometimes it is the case that the latest build failed, so we would like to deploy the last successful build of a specific branch/tag from git. Currently I would like to find the build number via the build history of jenkins, but it seems jenkins doesn't provide API access to the build history. Does anyone have an idea how to get the last successful build for a specific git branch/tag via the API?

Cheers
Nick

DarrelVun

unread,
Jul 8, 2014, 3:26:05 PM7/8/14
to jenkins...@googlegroups.com
Nick:

If you're using Git and Gerrit, then you can find the branch and sha1 under "actions" and "parameters" as name/value pairs, with name of "GERRIT_BRANCH" and "GERRIT_PATCHSET_REVISION".  I don't know of a way to find the specific build except to walk a project's builds until you find it.

So, to be clear:
- Start at http://jenkins_url/job/project_name/api/json?pretty=true
- Find the array of "builds", each element of which contains a key/value pair of "url" and the URL you need to visit
- E.g. a build URL will look like http://jenkins_url/job/project_name/build_number/api/json?pretty=true
- Look for "actions" -> "parameters" -> "name" == "GERRIT_BRANCH"

I don't know of an easier way. 


Regards,
Darrel V in NC

sebastian.nickel

unread,
Jul 9, 2014, 3:54:31 AM7/9/14
to jenkins...@googlegroups.com
Hi Darrel,
thank you for your answer. I already feared that I have to do it this way :-) This means that I need to do multiple API calls until I found all the builds for my specific branch/tag. I hoped there is a method with doing only one API call and then search for the desired build.


Cheers
Nick

teilo

unread,
Jul 9, 2014, 4:40:19 AM7/9/14
to jenkins...@googlegroups.com

Best practice is to normally have each branch in a different job.  Otherwise trends and linear history become a bit meaningless.

 It also solves your issue as you just use lastStable/api/…

 /James

Reply all
Reply to author
Forward
0 new messages