官方文档:
需求:当1个job启动构建后,获取它的构建状态.(成功,失败,驳回,构建中,正在排队)
关键函数:
获取job是否在排队的结果
获取正在排队构建的job队列 即pending状态中的所有job,如果没有 pending状态的job即返回1个空列表
queue_info = server.get_queue_info()
获取job构建结果
server.get_build_info(name, build_number)[ 'result'] # 构建结束 SUCCESS|FAILURE<class 'str'> ABORTED <class 'str'> 构建中None None <class 'NoneType'>
代码示例:
def get_build_state(server, name, build_number): ''' :param name: job_name :param build_number: 最后1次构建序号 :param:jenkins_server :return: 最后1次构建状态 pending,success,false,building ''' build_state = None # 获取正在排队构建的job队列 即pending状态中的所有job,如果没有 pending状态的job即返回1个空列表 queue_info = server.get_queue_info() if queue_info: for queue_job_info in queue_info: if queue_job_info['task']['name'] == name: # msg = 'pending期,排队构建中' build_state = 'pending' else: build_state = server.get_build_info(name, build_number)[ 'result'] # 构建结束 SUCCESS|FAILURE<class 'str'> ABORTED <class 'str'> 构建中None None <class 'NoneType'> return build_state
通过接口将该功能封装起来
@build_bp.route('/gotest/api/getBuildState', methods=['POST']) def get_job_build_state(): ''' 获取job构建状态 return:build_state Success,Faild,Pending,Building ''' data = request.get_json() job_name = data['job_name'] server = get_jenkins_server() # 获取job的最后次构建号 lastbuildNumber = server.get_job_info(job_name)['lastBuild']['number'] #获取构建状态 build_state = get_build_state(server, job_name, lastbuildNumber) if build_state is None: build_msg = 'Building' elif build_state == 'SUCCESS': build_msg = 'SUCCESS' elif build_state == 'FAILURE': build_msg = 'FAILURE' elif build_state == 'ABORTED': build_msg = 'ABORTED' elif build_state =='pending': build_msg = 'pending' response = {} datas={} response['code'] = 200 response['msg'] = 'OK' datas['build_state'] = build_msg datas['buildNumber'] = lastbuildNumber response['datas'] = datas return response
测试接口:
import requests url='http://127.0.0.1:5000/gotest/api/getBuildState' data={'job_name':'android_official'} r = requests.post(url,json=data) print(r.text)
响应:
{"code":200,"datas":{"buildNumber":1010,"build_state":"SUCCESS"},"msg":"OK"}
以上这篇Python-jenkins 获取job构建信息方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。