日常维护的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_names
else:
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']) / 1000
st2 = 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_timestamp
elif response.status_code == 404:
print('%s: 无构建记录' % job_name)
else:
print(f"Failed to retrieve job information for {job_name}")
return None
except 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)
其他功能,按需参考。。。