学习文档:
https://www.cnblogs.com/hujinzhong/p/14593293.html
https://blog.csdn.net/qq_45939519/article/details/121052408
官方文档:
API 参考 — Python Jenkins 1.8.0 文档 (python-jenkins.readthedocs.io)
使用 Python-Jenkins — Python Jenkins 1.8.0 文档
import requests import jenkins import time # Jenkins 服务器的 URL JENKINS_URL = "http://116.205.245.238:8081/" # Jenkins 用户名和 密码 USERNAME = "lufeixiang" PASSWORD = "lfx@2024" # 列出你想构建的所有任务job,如果jobs为空,即jobs = [] 表示构建所有的job jobs = ["som-storage-mgt"] # 参数化构建,要构建的分支名称,只能针对所有的job params = {"branch": "origin/uat"} def action_build_jobs(server): # 所有的job信息 all_jobs_li = server.get_all_jobs() # 开始时间 start_time = time.time() # 构建job for item in all_jobs_li: # print('name: %s' % item['name'], 'URL: ', item['url']) if not jobs or item['name'] in jobs: server.build_job(item['name'],parameters=params) # 获取运营空间管理平台-生产者的视图的job # print(server.get_jobs(view_name='运营空间管理平台-生产者')) last_queue_job = [] last_building_job = [] while(True): queue_job = server.get_queue_info() building_job = server.get_running_builds() if(queue_job and len(queue_job) != len(last_queue_job)): queue_job_nameList = [] for job in queue_job: queue_job_nameList.append(job['task']['name']) print("等待队列中job: " + str(queue_job_nameList)) if(building_job and building_job != last_building_job): building_job_nameList = [] for job in building_job: building_job_nameList.append(job['name']) print("构建队列中job: " + str(building_job_nameList)) if(not queue_job and not building_job): break last_queue_job = queue_job last_building_job = building_job time.sleep(1) # 结束时间 end_time = time.time() duration = end_time - start_time # 打印耗时 print("所有job构建完成,耗时: " + str(duration) + "s") def is_last_build_successful(server, job_name): # 获取指定作业的最新构建信息 last_build_info = server.get_job_info(job_name) # 检查是否有构建信息 if last_build_info['lastCompletedBuild'] is not None: last_build_number = last_build_info['lastCompletedBuild']['number'] # 获取最新构建的结果 build_result = server.get_build_info(job_name, last_build_number)['result'] # 如果结果为 'SUCCESS',则最新构建成功 if build_result == 'SUCCESS': return True else: return False else: # 没有最近的构建信息,可能作业还没有构建过 return False if __name__ == "__main__": # 连接jenkins server = jenkins.Jenkins(JENKINS_URL, USERNAME, PASSWORD) # 构建job action_build_jobs(server) # 检查每个job最近一次构建是否成功 if jobs: for job in jobs: success = is_last_build_successful(server, job) if not success: print(job + " 构建失败!") else: for job in server.get_all_jobs(): success = is_last_build_successful(server, job) if not success: print(job + " 构建失败!")
标签:jobs,last,python,server,queue,job,build,jenkins From: https://www.cnblogs.com/lfxx/p/18180259