jenkins 通过api触发,构建job,获取job执行结果,执行状态
程序员文章站
2022-03-28 22:32:03
#!/usr/bin/python# -*- coding: UTF-8 -*-import jenkins #pip install python-jenkinsimport datetime,timeclass Job(): def __init__(self, jenkins_master, jenkins_job, jenkins_user, jenkins_passwd,jenkins_server): ''' jenkins_master :....
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import jenkins #pip install python-jenkins
import datetime,time
class Job():
def __init__(self, jenkins_master, jenkins_job, jenkins_user, jenkins_passwd,jenkins_server):
'''
jenkins_master :master地址
jenkins_job :jenkins_job名字
jenkins_user :登陆账号
jenkins_passwd :登陆密码
'''
self.jenkins_master = jenkins_master
self.jenkins_job = jenkins_job
self.login_user = jenkins_user
self.login_passwd = jenkins_passwd
jkServer = jenkins.Jenkins(self.jenkins_master, self.login_user, self.login_passwd)
self.jenkins_server = jkServer
def build_job(self, parameters=None):
'''
api触发job
return queueId : 378956
'''
print "build job"
token=datetime.datetime.now().strftime('%Y%m%d_%H%M%S%f')
#parameters = {"order_date": 2020071111, "test_locals_id_point_version": 2222}
if parameters: # ##带参数构建触发,参数必须是字典类型
return self.jenkins_server.build_job(self.jenkins_job, parameters=parameters, token=token)
return self.jenkins_server.build_job(self.jenkins_job, token=token) ## 无参数构建触发
def getLastJobId(self):
'''
role : 获取job名为job_name的job的最后次构建号
'''
return self.jenkins_server.get_job_info(self.jenkins_job)['lastBuild']['number']
def getJobResultStatus(self,jobId):
'''
role:获取job名为job_name的job的某次构建的执行结果状态
SUCCESS : job执行成功
FAILURE :job执行失败
ABORTED :人为结束构建
None : 正在构建中
'''
return self.jenkins_server.get_build_info(self.jenkins_job,jobId)['result']
def getJobBuilding(self,jobId):
'''
role:判断job名为job_name的job的某次构建是否还在构建中
True:正在构建
Fase:构建结束
'''
return self.jenkins_server.get_build_info(self.jenkins_job,jobId)['building']
if __name__=='__main__':
jkPath="http://jenkinsxxxxxxxx.com"
jkJob="job_name" #jobname
jkLoginUser="xiaohong"
jkLoginPwd="hdu7298cbopsncdk92"
jk = Job(jkPath, jkJob, jkLoginUser, jkLoginPwd, None)
data={"order_date":2020071111,"key_kkk":2222}
print jk.build_job(data)
time.sleep(12) #api出发怕jenkin没来得及响应
jobid=jk.getLastJobId()
print "job id",jobid
print "job result status",jk.getJobResultStatus(jobid)
print "job building result",jk.getJobBuilding(jobid)
参考:https://www.cnblogs.com/znicy/p/5498609.html
本文地址:https://blog.csdn.net/YMY_mine/article/details/107377700