从python脚本获取jenkins构建状态

时间:2019-04-25 07:39:07

标签: python jenkins

我正在尝试获取所有作业的所有构建的状态。我编写了一个脚本,该脚本花费太多时间执行。无论如何,我是否可以优化该脚本?任何帮助将不胜感激。

 def jenkinsconn():
        server = jenkins.Jenkins('server',username=username,password=password)
        jobs = server.get_jobs()
        job_name_list=[]
        build_number_list=[]
        build_info_list=[]
        status_list_dict={}
        success=0
        failure=0
        unstable=0
        aborted=0
        #print dir(server)
        for i in range(len(jobs)):
                job_name=jobs[i]['name']
                job_name_list.append(job_name)
        for i in range(len(job_name_list)):
                job_info=server.get_job_info(job_name_list[i])
        lastbuilt=job_info['lastSuccessfulBuild']
                if lastbuilt:
                    b_number=job_info['lastSuccessfulBuild']['number']
                    build_number_list.append(b_number)
            build_zipped=zip(job_name_list,build_number_list)
    for i ,j in build_zipped:
                success=0
                failure=0
                unstable=0
                aborted=0
                for k in range(j):        
                            build_info=server.get_build_info(i,k+1)
                            build_info_list.append(build_info)
                            status=build_info['result']
                            if status=="SUCCESS":
                                    success+=1
                            elif status=="FAILURE":
                                    failure+=1
                            elif status=="UNSTABLE":
                                    unstable+=1
                            else:
                                    aborted+=1
                            statuscount=[success,failure,unstable,aborted]
                            status_list_dict[i]=statuscount 

1 个答案:

答案 0 :(得分:1)

如果仅需要构建成功,失败等的数量,则可以对每个作业执行一个请求,而不是像看起来代码那样对每个构建执行一个请求。我在python-jenkins模块中找不到一种方法可以执行此操作,但是您可以使用Jenkins API自己执行此操作。

例如:

try:                 # Python 3
    from urllib.request import urlopen
    from urllib.parse import quote
except ImportError:  # Python 2
    from urllib2 import urlopen, quote

import json
import contextlib

status_list_dict = {}
with contextlib.closing(
    urlopen("http://HOST_NAME:8080/api/json")
) as job_list_response:
    job_list = json.load(job_list_response)["jobs"]

for job in job_list:
    status_counts = [0,0,0,0]

    with contextlib.closing(
        urlopen(
            "http://HOST_NAME:8080/job/{job_name}/api/json?tree=allBuilds[result]".format(
                job_name=quote(job["name"])
            )
        )
    ) as build_list_response:
        build_list = json.load(build_list_response)["allBuilds"]

        for build_data in build_list:
            if build_data["result"] == "SUCCESS":
                status_counts[0] += 1
            elif build_data["result"] == "FAILURE":
                status_counts[1] += 1
            elif build_data["result"] == "UNSTABLE":
                status_counts[2] += 1
            elif build_data["result"] == "ABORTED":
                status_counts[3] += 1

     status_list_dict[job["name"]] = status_counts