温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python-jenkins如何获取job构建信息

发布时间:2020-07-28 09:24:07 来源:亿速云 阅读:708 作者:小猪 栏目:开发技术

这篇文章主要讲解了Python-jenkins如何获取job构建信息,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。

官方文档:

需求:当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构建信息有进一步的了解,如果还想学习更多内容,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI