欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 社会 > jenkins-通过api获取所有job及最新build信息

jenkins-通过api获取所有job及最新build信息

2025/1/26 16:55:25 来源:https://blog.csdn.net/gujimoxiao/article/details/145306026  浏览:    关键词:jenkins-通过api获取所有job及最新build信息

日常维护的API接口功能:

前提: python

1. 获取所有job:

def get_all_jobs(jenkins_url, username, password):url = f"{jenkins_url}/api/json"response = requests.get(url, auth=(username, password))if response.status_code == 200:data = response.json()jobs = data["jobs"]job_names = [job["name"] for job in jobs]return job_nameselse:print("Failed to retrieve job information")return []jenkins_url = "http://jenkins.xxxxx.com:8080"
username = "xiaoxiang"
password = "xxxxx"all_jobs = get_all_jobs(jenkins_url, username, password)job_list = []for job_name in all_jobs:job_list.append(job_name)

2. 获取job 最新build时间:

def get_job_last_build_time(jenkins_url, job_name,username,password):url = f"{jenkins_url}/job/{job_name}/lastBuild/api/json"#print(url)try:response = requests.get(url,auth=(username,password))#print(response.status_code)#try:if response.status_code == 200:data = response.json()st = int(data['timestamp']) / 1000st2 = datetime.datetime.fromtimestamp(st)return st2.strftime('%Y-%m-%d %H:%M:%S')#print(data['lastBuild'])#last_build_timestamp = data["lastBuild"]["timestamp"]#return last_build_timestampelif response.status_code == 404:print('%s: 无构建记录' % job_name)else:print(f"Failed to retrieve job information for {job_name}")return Noneexcept Exception as e:print('%s: 获取参数异常' % job_name)jenkins_url = "http://jenkins.xxxxx.com:8080"
username = "xiaoxiang"
password = "xxxxx"
get_job_last_build_time(jenkins_url,i,username,password)

其他功能,按需参考。。。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com