一、Jenkins api接口简介
- Jenkins对外暴露的动作交付入口
- 为外部程序提供的入口,可以控制Jenkins
- 支持协议:http
- api接口支持用户名、密码认证
- Jenkins job支持典型的功能:运行job、查看状态、返回任务编号等等
二、Jenkins常用接口
2.1、使用curl调用 获取所有Jobs接口
curl -u USERNAME:PASSWORD "JENKINSURL:PORT/api/json?pretty=true"
# 可以加上&tree=jobs[name,url]进行显示内容的展示
2.2、使用curl调用 获取Job信息
# 获取任务所有信息
curl -u USERNAME:PASSWORD JENKINSURL:PORT/job/JOBNAME/api/json?pretty=true
# 获取最后一次构建信息
curl -u USERNAME:PASSWORD JENKINSURL:PORT/job/JOBNAME/lastBuild/api/json?pretty=true
# 获取最后稳定构建信息
curl -u USERNAME:PASSWORD JENKINSURL:PORT/job/JOBNAME/lastStableBuild/api/json?pretty=true
# 获取最后成功的构建
curl -u USERNAME:PASSWORD JENKINSURL:PORT/job/JOBNAME/lastSuccessfulBuild/api/json?pretty=true
# 获取最后完成的构建
curl -u USERNAME:PASSWORD JENKINSURL:PORT/job/JOBNAME/lastCompletedBuild/api/json?pretty=true
# 最近失败的构建
curl -u USERNAME:PASSWORD JENKINSURL:PORT/job/JOBNAME/lastFailedBuild/api/json?pretty=
2.3、使用curl调用 操作Job
# 获取job的config
curl -u USERNAME:PASSWORD -O JENKINSURL:PORT/job/JOBNAME/config.xml
# 创建job
curl -u USERNAME:PASSWORD "JENKINSURL:PORT/createItem?name=JOBNAME" --cookie "${cookie_file}" -X POST -H "${jenkins_crumb}" -H 'Content-Type:text/xml' --data-binary "@./config.xml"
# 复制job
curl -u USERNAME:PASSWORD "JENKINSURL:PORT/createItem?name=NEWJOBNAME&mode=copy&from=JOBNAME" --cookie "${cookie_file}" -X POST -H "${jenkins_crumb}"
# 修改job
curl -u USERNAME:PASSWORD "JENKINSURL:PORT/job/JOBNAME/config.xml" --cookie "${cookie_file}" -X POST -H "${jenkins_crumb}" -H 'Content-Type:text/xml' --data-binary "@./config.xml"
# 无参构建
curl -u USERNAME:PASSWORD "JENKINSURL:PORT/job/JOBNAME/build" --cookie "${cookie_file}" -X POST -H "${jenkins_crumb}"
# 有参构建
curl -u USERNAME:PASSWORD "JENKINSURL:PORT/job/JOBNAME/buildWithParameters" --cookie "${cookie_file}" -X POST -H "${jenkins_crumb}" --data-urlencode json='{"key1":"value1","key2":"value2"}'
# 删除job
curl -u USERNAME:PASSWORD "JENKINSURL:PORT/job/JOBNAME/doDelete" --cookie "${cookie_file}" -X POST -H "${jenkins_crumb}"
2.4、使用curl调用 操作指定构建的Job
# 查看最新构建任务编号
curl -u USERNAME:PASSWORD "JENKINSURL:PORT/job/son_demo/lastBuild/buildNumber"
# 查看某次构建控制台日志
curl -u USERNAME:PASSWORD "JENKINSURL:PORT/job/JOBNAME/ID/logText/progressiveText"
# 删除某次构建
curl -u USERNAME:PASSWORD "JENKINSURL:PORT/job/JOBNAME/ID/doDelete" -X POST --cookie "${cookie_file}" -H "${jenkins_crum
三、Python Jenkins Api常用接口操作
- 安装 pip install jenkinsapi
3.1、jenkinsapi 连接Jenkins服务
class JenkinsApiDemo:
host = "http://192.168.111.100:5003"
username = "admin100"
password = "1121f1466861fbb826ae47a520ecb91f55"
def __init__(self):
self.jenkins = jenkinsapi.jenkins.Jenkins(self.host, self.username, self.password)
3.2、jenkinsapi 获取所有jobs
# 需要注意,如果请求的url与Jenkins Location配置的Jenkins URL不一致时,获取到的key会拼接上一截地址
print(my_jenkins.keys()) # 获取所有的job名称
for info in my_jenkins.get_jobs_info():
print(info) # 获取job的url与名称 比较卡
3.3、jenkinsapi 获取Job信息
def get_job_info(self, job_name):
# 获取job对象
my_job = self.jenkins.get_job(job_name)
print(my_job.get_config())
# 获取最后一次构建对象
print(my_job.get_last_build())
# 获取最后一次完成的构建对象
print(my_job.get_last_completed_build())
# 获取最后一次成功的构建对象
print(my_job.get_last_good_build())
# 获取最后一次失败的构建编号
print(my_job.get_last_failed_buildnumber())
3.4、jenkinsapi 操作Job信息
# 获取job的config
print(my_job.get_config())
# 创建job
# 先获取到job的config
with open("config.xml", "w", encoding="utf-8") as f:
my = my_jenkins.get_job("JOBNAME").get_config()
f.write(my)
my_jenkins.create_job("NEWJOBNAME", open("./config.xml",encoding="utf-8").read())
# 复制job
my_jenkins.copy_job("JOBNAME", "NEWJOBNAME")
# 禁用job
my_job.disable()
# 启用job
my_job.enable()
# 重命名job
my_jenkins.rename_job("JOBNAME", "NEWJOBNAME")
# 无参构建
my_jenkins.build_job("JOBNAME")
# 有参构建
my_jenkins.build_job("JOBNAME", params={"key": "value"})
# 删除job
my_jenkins.delete_job("JOBNAME")
3.5、jenkinsapi 操作指定构建的Job
# 当前job对象是否在队列/构建中
print(my_job.is_queued_or_running())
# 获取指定构建ID的对象
build_object = my_job.get_build(BUILDID)
# 获取构建对象的时间戳
print(build_object.get_timestamp())
# 获取控制台日志
print(build_object.get_console())
# 获取构建参数
print(build_object.get_params())
# 获取构建的状态
print(build_object.get_status())
# 获取构建的变更日志
print(build_object.get_changeset_items())
# 删除指定构建
my_job.delete_build(BUILDID)