1 获取jobs的当前任务状态
server_1 = jenkins.Jenkins('http://%s:%s@192.168.37.134:8081/',username, password)
获取状态前先确认2019文件夹下的get_node_list任务是否存在:
server_1.assert_job_exists('2019/get_node_list')
获取最后一次完成(不包括执行中的)的job任务执行number:
server_1.get_job_info('2019/get_node_list')['lastCompletedBuild']['number']
查看job状态(SUCCESS/FAILURE/ABORTED):
server_1.get_build_info('2019/get_node_list',3)['result']
server_1.get_build_console_output('2019/get_node_list',7).split('\n')[-2].split(':')[-1].strip()
启动jobs:
server_1.build_job('2019/get_node_list')
在job执行结束前使用server_1.get_build_console_output(‘2019/get_node_list',7).split('\n')[-2].split(':')[-1].strip()获取的状态信息不符合预期。
job状态应该还包括running,pending状态,那么获取job的当前状态正确姿势如下:
job_name = '2019/get_node_list' def get_jobs_status(job_name,server): try: server.assert_job_exists(job_name) except Exception as e: print(e) job_statue = '1' #判断job是否处于排队状态 inQueue = server.get_job_info(job_name)['inQueue'] if str(inQueue) == 'True': job_statue = 'pending' running_number = server.get_job_info(job_name)['nextBuildNumber'] else: #先假设job处于running状态,则running_number = nextBuildNumber -1 ,执行中的job的nextBuildNumber已经更新 running_number = server.get_job_info(job_name)['nextBuildNumber'] -1 try: running_status = server.get_build_info(job_name,running_number)['building'] if str(running_status) == 'True': job_statue = 'running' else: #若running_status不是True说明job执行完成 job_statue = server.get_build_info(job_name,running_number)['result'] except Exception as e: #上面假设job处于running状态的假设不成立,则job的最新number应该是['lastCompletedBuild']['number'] lastCompletedBuild_number = server.get_job_info(job_name)['lastCompletedBuild']['number'] job_statue = server.get_build_info(job_name,lastCompletedBuild_number)['result'] return job_statue,running_number
注意:
可能还存在下图的情况,这个时候获取的是26的状态,这时候也许你想获取25的状态,26是不小心误操作触发的,这个时候任务的最新状态也许就无法满足预期要求,或者是支持并发构建的job场景中就不适用了,关键还是需要结合应用场景制定对应的方案。
2 统计jobs的执行成功率和平均执行时间
统计场景说明:
设计了一个统计job执行成功率的工程,主要从执行时间以及视图两个维度来划定需要统计的jobs及jobs对应的运行范围。
在这里我在job里面添加了DAYS和VIEWS两个参数:
**DAYS:**默认统计最近一天的运行情况,如果执行的时候输入的是0则代表统计所有的运行情况。
**VIEWS:**对应的是视图名称,“2019-1,test”代表统计这两个视图的运行情况
对应的视图如下:
执行成功后以表格形式列出统计的数据,表头如下
列出了序号、视图名称、job名称、job执行成功的平均执行时间、job执行成功次数、总的执行时间、job执行成功率
job执行演示:
执行构建时配置的参数如下
job_data任务的主要执行内容如下:
执行成功后查看HTML_Report统计的数据如下:
get_job_data.py源码如下:
#!/usr/bin/env python # -*- coding:utf-8 -*- # author: Sudley # ctime: 2020/02/12 import sys import jenkins import time from dominate.tags import * def Count_the_success_rate_of_jobs(days,views): username = 'sudley' password = '******' with open('//home/Sudley/python-jenkins/get_job_data.txt','w') as f: print('create a new file //home/Sudley/python-jenkins/get_job_data.txt') serial_number = 0 #统计任务的累计序号 for view in views.split(','): #根据视图名称拼接视图的URL,多个视图间用','分隔 URL = ('http://%s:%s@192.168.37.134:8081/job/2019/view/%s/')%(username, password, view) server = jenkins.Jenkins(URL) #依次获取当前view视图中jobs的信息 for num in range(0,len(server.get_all_jobs())): job_name = server.get_all_jobs()[num]['fullname'] #获取最后一次完成构建的编号,用于划定时间范围(如果需要的话) try: lastCompletedBuild_num = server.get_job_info(job_name)['lastCompletedBuild']['number'] except: #假如job下面一个构建记录都没有则补0 print('There is not build number in',job_name) average_success_duration = success_count = all_count = success_rate = 0 line = str(serial_number) + ' ' + view + ' ' + job_name + ' ' + str(int(average_success_duration)) + ' ' + str(success_count) + ' ' + str(all_count) + ' ' + str(success_rate) + '%' with open('//home/Sudley/python-jenkins/get_job_data.txt','a') as f: f.write(str(line)) f.write('\n') serial_number = serial_number + 1 continue #获取最后一次完成构建的时间戳,单位由毫秒转换为秒 lastCompletedBuild_timestamp = server.get_build_info(job_name,lastCompletedBuild_num)['timestamp'] / 1000 #将时间先由秒转化为元组在转化为字符串并取到天数 lastCompletedBuild_date = time.strftime("%Y%m%d",time.localtime(lastCompletedBuild_timestamp)) #print(lastCompletedBuild_date) #根据变量days和lastCompletedBuild_timestamp计算出days天前的日期,若days为0则没有日期限制,统计之前运行的所有任务 if str(days) == '0': end_date = 'false' else: end_timestamp = float(lastCompletedBuild_timestamp) - float(days) * 24 * 3600 end_date = time.strftime("%Y%m%d",time.localtime(end_timestamp)) #print(end_date) #获取days天内job的执行情况 success_count = 0 #job执行成功的总数 success_duration = 0 #执行成功的job执行时间之和,单位是s for number in range(0,len(server.get_job_info(job_name)['builds'])): job_build_number = server.get_job_info(job_name)['builds'][number]['number'] job_build_timestamp = server.get_build_info(job_name,job_build_number)['timestamp'] / 1000 job_build_date = time.strftime("%Y%m%d",time.localtime(job_build_timestamp)) #如果日期和end_date相同则终止此job数据的累计 if job_build_date == end_date: number = number - 1 break #累计执行成功的次数和duration执行时间 job_build_result = server.get_build_info(job_name,job_build_number)['result'] if str(job_build_result) == 'SUCCESS': job_build_duration = server.get_build_info(job_name,job_build_number)['duration'] success_duration = success_duration + job_build_duration / 1000 success_count = success_count + 1 #计算执行成功的平均执行时间和成功率,打印关键信息 all_count = number + 1 success_rate = success_count * 1.0 / all_count * 100 if success_count == 0: average_success_duration = success_duration else: average_success_duration = success_duration * 1.0 / success_count #将关心的数据按照一定的格式写到/home/Sudley/python-jenkins/get_job_data.txt文件中 line = str(serial_number) + ' ' + view + ' ' + job_name + ' ' + str(int(average_success_duration)) + ' ' + str(success_count) + ' ' + str(all_count) + ' ' + str(round(success_rate,2)) + '%' with open('//home/Sudley/python-jenkins/get_job_data.txt','a') as f: f.write(str(line)) f.write('\n') serial_number = serial_number + 1 def txt2xml(): h = html() with h.add(body()): h2('job执行效率统计') caption('summary:') with table(border="2",cellspacing="0"): l = tr(bgcolor="#0000FF") l += th('序号') l += th('view_name') l += th('job_name') l += th('average_success_duration') l += th('success_count') l += th('all_count') l += th('success_rate') file=open('/home/Sudley/python-jenkins/get_job_data.txt') for line in file.readlines(): curLine=line.strip().split(" ") l = tr() for i in range(0,len(curLine)): l += td(curLine[i]) with open('/home/Sudley/python-jenkins/get_job_data.html','w') as f: f.write(h.render()) if __name__ == '__main__' : days = sys.argv[1] views = sys.argv[2] Count_the_success_rate_of_jobs(days,views) txt2xml()
以上这篇Python-jenkins模块获取jobs的执行状态操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
更新日志
- 雨林唱片《赏》新曲+精选集SACD版[ISO][2.3G]
- 罗大佑与OK男女合唱团.1995-再会吧!素兰【音乐工厂】【WAV+CUE】
- 草蜢.1993-宝贝对不起(国)【宝丽金】【WAV+CUE】
- 杨培安.2009-抒·情(EP)【擎天娱乐】【WAV+CUE】
- 周慧敏《EndlessDream》[WAV+CUE]
- 彭芳《纯色角3》2007[WAV+CUE]
- 江志丰2008-今生为你[豪记][WAV+CUE]
- 罗大佑1994《恋曲2000》音乐工厂[WAV+CUE][1G]
- 群星《一首歌一个故事》赵英俊某些作品重唱企划[FLAC分轨][1G]
- 群星《网易云英文歌曲播放量TOP100》[MP3][1G]
- 方大同.2024-梦想家TheDreamer【赋音乐】【FLAC分轨】
- 李慧珍.2007-爱死了【华谊兄弟】【WAV+CUE】
- 王大文.2019-国际太空站【环球】【FLAC分轨】
- 群星《2022超好听的十倍音质网络歌曲(163)》U盘音乐[WAV分轨][1.1G]
- 童丽《啼笑姻缘》头版限量编号24K金碟[低速原抓WAV+CUE][1.1G]