Add waiting for the correct build_id in the JenkinClient:run_build()
If the job is queued while waiting for the free executors, then
the method was getting the wrong build_id belonging to the
previous job, for instance it will return build_id=28 in this example:
№29 (pending—Build #28 is already in progress (ETA:17 min))
№28 [In progress] May 31, 2018 6:07 PM
* Add 'timeout' parameter (default 600sec) in run_build()
to wait until the queued build is started
* Add pooling the queue until the build is started, get the
build number assigned to build in queue object
* Add 'verbose' parameter (default False) to show the reason
from Jenkins why the build is not started yet, for example:
pending the job 'deploy_openstack' : Build #22 is already in progress (ETA:1 min 48 sec)
pending the job 'deploy_openstack' : Build #22 is already in progress (ETA:1 min 18 sec)
pending the job 'deploy_openstack' : Build #22 is already in progress (ETA:48 sec)
* Rename the parameter 'print_job_output' to 'verbose'
in the method wait_end_of_build() to have the common parameter
naming.
Change-Id: Id49b5b45a8127e769b89860b19424081f37f6f38
diff --git a/tcp_tests/managers/jenkins/client.py b/tcp_tests/managers/jenkins/client.py
index 8c9939f..0b0d7de 100644
--- a/tcp_tests/managers/jenkins/client.py
+++ b/tcp_tests/managers/jenkins/client.py
@@ -1,5 +1,4 @@
from __future__ import print_function
-import time
import jenkins
import requests
@@ -48,20 +47,33 @@
for j in job_params])
return def_params
- def run_build(self, name, params=None):
+ def run_build(self, name, params=None, timeout=600, verbose=False):
params = params or self.make_defults_params(name)
- self.__client.build_job(name, params)
- time.sleep(10) # wait while jobs started:
- build_id = self.job_info(name)['lastBuild']['number']
+ num = self.__client.build_job(name, params)
+
+ def is_blocked():
+ queued = self.__client.get_queue_item(num)
+ status = not queued['blocked']
+ if not status and verbose:
+ print("pending the job [{}] : {}".format(name, queued['why']))
+ return status
+
+ helpers.wait(
+ is_blocked,
+ timeout=timeout,
+ interval=30,
+ timeout_msg='Timeout waiting to run the job [{}]'.format(name))
+
+ build_id = self.__client.get_queue_item(num)['executable']['number']
return name, build_id
def wait_end_of_build(self, name, build_id, timeout=600,
- print_job_output=False):
+ verbose=False):
start = [0]
def building():
status = not self.build_info(name, build_id)['building']
- if print_job_output:
+ if verbose:
res = self.get_progressive_build_output(name,
build_id,
start=start[0])