Jenkins Api简介
- Jenkins 对外暴露的动作交付入口
- 为外部程序提供入口,可以控制Jenkins
- 支持协议 - Http
- Api 接口支持用户名、密码认证
- Jenkins Api支持的典型功能:运行Job、查看任务状态、返回任务编号…等
Jenkins Api 环境准备
使用curl调用 获取所有Jobs接口
- 创建一个有任务运行和查询权限的用户
- 较老版本的Jenkins操作关闭跨站脚本伪造请求保护,新版本的Jenkins无此选项了
-
官方文档 HTTPS://WWW.JENKINS.IO/DOC/BOOK/USING/REMOTE-ACCESS-API/
curl -u USERNAME:PASSWORD "JENKINSURL:PORT/api/json?pretty=true"
# 可以加上&tree=jobs[name,url]进行显示内容的展示
使用curl调用 获取所有构建队列
curl -u USERNAME:PASSWORD JENKINSURL:PORT/queue/api/json?pretty=true
使用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=true
使用curl调用 操作Job
# 方式1:获取crumb
username_password="USERNAME:PASSWORD"
jenkins_url="JENKINSURL:PORT"
cookie_file="$(mktemp)"
jenkins_crumb=$(curl -s -f -u "${username_password}" --cookie-jar "${cookie_file}" -s "${jenkins_url}/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,%22:%22,//crumb)")
# 方式2:使用token
使用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}"
使用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_crumb}"
Python Jenkins Api常用接口操作
- 安装 pip install jenkinsapi
jenkinsapi 连接Jenkins服务
from jenkinsapi import jenkins
my_jenkins = jenkins.Jenkins("JENKINSURL", "USERNAME", "PASSWORD", useCrumb=True)
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与名称 比较卡
jenkinsapi 获取构建队列
print(my_jenkins.get_queue().keys())
jenkinsapi 获取Job信息
# 获取job对象
my_job = my_jenkins.get_job("JOBSNAME")
# 获取最后一次构建对象
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())
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")
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)