很多公司都会做CI持续集成,一般CI都用Jenkins.如何快速获取Jenkins 上的build?
一般公司都会有CI服务器,各个项目build都在上面触发或生成。
如何快速获取CI服务器上最新的build呢?
有的人可能想到爬虫,当然爬虫可以做到,但是比较复杂,还要判断最新的build是否是success.
其实不用这么麻烦,Jenkins提供了一些接口,直接用接口就能获得build相关的信息。例如:
http://$host/job/$jobname/lastSuccessfulBuild/api/json
- lastBuild,
- lastStableBuild,
- lastSuccessfulBuild,
- lastFailedBuild,
- lastUnstableBuild,
- lastUnsuccessfulBuild,
- lastCompletedBuild
用得较多的是lastSuccessfulBuild, 当然也可以获取失败的build来分析构建失败的原因。
举个例子,我们android 在CI 上build 并且archive APK 文件。
那么我们就可以很轻松的得到想要的build.
如果我用UI的automation test来做BVT,那么代码可以这样写
from globals import *
import requests, re, os
import shutil
from ptest.plogger import preporter
JENKINS_HOST_ANDROID = "XXX"
current_dir = os.path.split(os.path.realpath(__file__))[0]
apk_path = current_dir + "/apk/"
build_name = "XXX"
class Jenkins:
def __init__(self):
self.Jenkins_build_url = JENKINS_HOST_ANDROID + "/job/{0}/lastSuccessfulBuild/api/json".format(
current_device_info.jenkins_job)
def get_build_url(self):
preporter.info(self.Jenkins_build_url)
builds_urls = requests.get(self.Jenkins_build_url)
builds = [each_build['relativePath'] for each_build in builds_urls.json()['artifacts']]
preporter.info(builds)
current_build = re.findall(
"/build/outputs/apk/{}-{}-{}.*?.apk".format(build_name,
current_device_info.device_env,
current_device_info.build_type), ','.join(builds))
preporter.info(current_build)
if current_build:
apk_url = builds_urls.json()['url'] + "artifact/" + current_build[0]
apk_name = current_build[0].split("/")[-1]
preporter.info(apk_url)
preporter.info(apk_name)
return apk_url, apk_name
else:
preporter.info("cannot find the build ")
def check_folder(sef, folder):
if os.path.exists(folder):
shutil.rmtree(folder)
os.makedirs(folder)
def download_file(self, url, path):
file = requests.get(url).content
with open(path, 'wb') as f:
f.write(file)
def download_build(self):
apk_url, apk_name = self.get_build_url()
self.check_folder(apk_path)
self.download_file(apk_url, apk_path + apk_name)
很容易就获取的最新的daily build.
结合build,就可以做其它的自动化测试了。
更多精彩,请关注微信公众号: python爱好部落
qrcode_for_gh_ca85abea157e_430.jpg
网友评论