Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wait until a Jenkins build is complete

I am using Python 2.7 and Jenkins.

I am writing some code in Python that will perform a checkin and wait/poll for Jenkins job to be complete. I would like some thoughts on around how I achieve it.

  1. Python function to create a check-in in Perforce-> This can be easily done as P4 has CLI
  2. Python code to detect when a build got triggered -> I have the changelist and the job number. How do I poll the Jenkins API for the build log to check if it has the appropriate changelists? The output of this step is a build url which is carrying out the job
  3. How do I wait till the Jenkins job is complete?

Can I use snippets from the Jenkins Rest API or from Python Jenkins module?

like image 429
Kannan Ekanath Avatar asked Mar 08 '13 15:03

Kannan Ekanath


2 Answers

Simple solution using invoke and block_until_complete methods (tested with Python 3.7)

import jenkinsapi
from jenkinsapi.jenkins import Jenkins

...

server = Jenkins(jenkinsUrl, username=jenkinsUser,
                 password=jenkinsToken, ssl_verify=sslVerifyFlag)

job = server.create_job(jobName, None)
queue = job.invoke()
queue.block_until_complete()

Inpsired by a test method in pycontribs

like image 71
esko.tajakka Avatar answered Oct 15 '22 07:10

esko.tajakka


Don't know if this was available at the time of the question, but jenkinsapi module's Job.invoke() and/or Jenkins.build_job() return a QueueItem object, which can block_until_building(), or block_until_complete()

jobq = server.build_job(job_name, job_params)
jobq.block_until_building()
print("Job %s (%s) is building." % (jobq.get_job_name(), jobq.get_build_number()))
jobq.block_until_complete(5) # check every 5s instead of the default 15
print("Job complete, %s" % jobq.get_build().get_status())
like image 26
mushuweasel Avatar answered Oct 15 '22 07:10

mushuweasel