1

I am trying to fetch status of all the builds for all my jobs.I have written a script it takes way too much time to execute.Is there anyway I can optimize the script? Any help will be appreciated.

 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 Answer 1

1

If you only need the number of builds succeeding, failing, etc. then you can make do with one request per job, rather than a request per build like it looks like your code is doing. I can't find an method in the python-jenkins module to do this, but you can do it yourself with the Jenkins API.

Eg:

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
Sign up to request clarification or add additional context in comments.

1 Comment

If someone searching for way to do this via requests library and with authentication - github.com/squadcastHQ/Integration-scripts/blob/master/jenkins/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.