5

I need to get the ID of a jenkins job that I start using the REST API (using python). Since Jenkins 1.529, it seems to be possible to trigger a build using the API abd to get in return an url pointing to the job in the queue.

Documentation of Jenkins:

Perform a build

To programmatically schedule a new build, post to this URL. If the build has parameters, post to this URL and provide the parameters as form data. Either way, the successful queueing will result in 201 status code with Location HTTP header pointing the URL of the item in the queue. By polling the api/xml sub-URL of the queue item, you can track the status of the queued task. Generally, the task will go through some state transitions, then eventually it becomes either cancelled (look for the "cancelled" boolean property), or gets executed (look for the "executable" property that typically points to the AbstractBuild object.)

But I do not succeed in getting an useful url.

I tried:

import requests
requestKwargs ={'headers': {'Content-Type': 'application/x-www-form-urlencoded'}, 'data': {'json': '{"parameter": []}'}, 'verify': True}
url=server+'job/test/build/'
req=requests.post(url, **requestKwargs)
print 'status',req.status_code
location=req.headers['location']
print 'location is:',location

The output of this code is:

status 201

location is: http://SERVER_PORT/job/test/build/

How can I use the information of "location" to track the status of the queue task?

Thanks.

Community
  • 1
  • 1
user2761432
  • 51
  • 1
  • 2

2 Answers2

4

I've worked around this issue by providing an 'ident' param to jobs that I wish to interact with programatically.

When submitting the job, I generate a uuid and submit this as the ident param to /job/<job_name>/buildWithParameters.

I then get /queue/api/json and iterate through the list of queued items, searching for an item with that uuid in it's params. That gets the queue id.

I then use this queue id to poll /queue/item/<queue_id>/api/json every N seconds, waiting for jenkins to provide a build number in the response (it will once the build has started). You can use this build number to construct the url you want, /job/<job_name>/<build_number>.

You actually need to add the ident as a param in your jenkins job. It's a pain, but it works reliably.

mikewaters
  • 3,668
  • 3
  • 28
  • 22
  • Wow. Talk about having to hack around a design flaw! – Stabledog Mar 12 '14 at 18:55
  • Question: what if the job passes through the queue before you can catch it? I cannot find anything in the Jenkins API which lets you fetch the params of a live or completed job. It seems entirely missing from the API... so it seems like if you don't catch it in the queue, you miss out on correlating the ID you've created? – Stabledog Mar 13 '14 at 01:57
  • Ok, I found out that I can get this from {server}/job/{jobname}/{jobNumber}/api/json. That data also shows up in the Python API's Job object in .get_actions(). So I retract my whine. Sorry for the spam! :) – Stabledog Mar 13 '14 at 02:12
  • @Stabledog which python api are you using? There are a few. – mikewaters Mar 13 '14 at 14:45
  • I was just using the one linked off of the top google hit (it's id'ed as 0.2.18 - https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0CCwQFjAA&url=https%3A%2F%2Fpypi.python.org%2Fpypi%2Fjenkinsapi&ei=9PshU5yDOMHZoATbk4LQDw&usg=AFQjCNEKK2gybghEnLluNoETI7Pf2F3EFQ&sig2=7jMDsE2ADotR8DWZRLjywA&bvm=bv.62922401,d.cGU) I see that people have cloned a bit on github, which I would happily do myself if I hadn't found a solution already. Thanks mainly to talking at you without regard for sounding half mad :) – Stabledog Mar 13 '14 at 18:42
  • BTW, my solution is a little different than yours -- I stole your id-parameter idea, but I just poll the job's recent builds, searching for the ID I assigned when starting the job. I ignore the build queue altogether, as that's a very short state on most of my jobs. – Stabledog Mar 13 '14 at 19:00
3

You need to add a header to your request:

Accept: application/json

And then it should work. Believe it or not, I had to read the source to figure this out... you can read the relevant snippet here.

EDIT: Apparently, though this does return the information for the project, it doesn't return the job that has just been queued! It's a known issue. The bug is #13546.

Bacon
  • 211
  • 3
  • 9