I am very new to Python-Jenkins. I went through the API page but I don't find the answer.
I created a job in Jenkins, say MyJob. I want to use python to
- Add multiple builds for MyJob, which means execute
server.build_job('MyJob', params)multiple times. - Get one of created build status(finished or still waiting) at any time, which means I want
get_build_info('MyJob', build_id)['result']at any time.
More precisely, I am implementing a web server, there are requests that ask to create new builds(point 1) or query for status of created builds(point 2).
In API, build_id = server.get_job_info('MyJob')['nextBuildNumber'] can provide me the next build number, but I am unable to get this build's info using server.get_build_info('MyJob', build_id) at any time because if one build is in waiting queue, then we cannot use get_build_info to get its status, it will throw exception.
I don't understand, it's impossible for me to know when one build task would be put into execution, so when I am doing the query, one build can be any of (waiting in queue | executing | finished), then how could I get those builds' status? Actually I totally don't understand why get_build_info() doesn't support the situation when build task is in waiting queue.
===============================================================
Update:
I tried @apr_1985's suggestion. The following code slice could give what I want, but I am not sure if there is a better way to do this.
build_id = server.get_job_info('MyJob')['nextBuildNumber']
queue_id = server.build_job('MyJob',params)
...
try:
task = server.get_build_info('MyJob', build_id)
except:
queue = server.get_queue_item(queue_id)
print('I am waiting in queue.\n')
print(queue)
else:
print(task['result'])
print('I am executing/finished.\n')