gpt4 book ai didi

python - 从 python 脚本获取 jenkins 构建状态

转载 作者:行者123 更新时间:2023-12-01 07:58:09 26 4
gpt4 key购买 nike

我正在尝试获取我所有作业的所有构建的状态。我编写了一个脚本,执行起来需要太多时间。我可以优化该脚本吗?任何帮助将不胜感激。

 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

最佳答案

如果您只需要成功、失败等的构建数量,那么您可以对每个作业一个请求进行处理,而不是像您的代码所做的那样每个构建一个请求。我在 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

关于python - 从 python 脚本获取 jenkins 构建状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55844156/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com