[Python-jenkins] Get job build information

Official documentation:

Requirement: When a job starts to build, get its build status. (Success, Fail, Reject, Building, Queue)

 

Key functions: 

Get the result of whether the job is queued

Get all the jobs in the pending state of the job queue that is being queued up, and return an empty list if there are no jobs in the pending state
queue_info = server.get_queue_info()

 Get job build results

server.get_build_info(name, build_number)[
            'result']  # 构建结束 SUCCESS|FAILURE<class 'str'>   ABORTED <class 'str'>  构建中None  None <class 'NoneType'>
Code example:
def get_build_state (server, name, build_number):
     '' ' 

    : param name: job_name 
    : param build_number: last build number 
    : param: jenkins_server 
    : return: last build state pending, success, false, building 
    ' '' 
    build_state = None 

    # overview of all job queued job queue that is constructed in a pending state, the pending state if there is no job i.e. returns an empty list 
    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 of queuing Construction' 
                build_state = ' Pending ' 
    the else : 

        build_state = server.get_build_info (name, BUILD_NUMBER) [
             ' Result ' ]   # Construction end SUCCESS | FAILURE <class 'str' > ABORTED <class 'str'> None None under construction <class 'NoneType'> 
    return build_state

 

Encapsulate the function through the interface

@ build_bp.route ( ' / gotest / api / getBuildState ' , methods = [ ' POST ' ])
 def get_job_build_state ():
     '' ' 
        Get job build status 
        return: build_state Success, Faild, Pending, Building 
        ' '' 
    data = request .get_json () 
    job_name = Data [ ' job_name ' ] 

    Server = get_jenkins_server ()
     # Get the last time to build job number 
    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[' dates ' ] = dates 

    return response

Test interface:

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)

response:

{"code":200,"datas":{"buildNumber":1010,"build_state":"SUCCESS"},"msg":"OK"}

 

Guess you like

Origin www.cnblogs.com/kaerxifa/p/12750297.html