背景
Jenkins 作为项目构建,持续部署必不可少的工具,如何使用程序或者脚本来控制构建流程成为本文研究的内容.
默认情况下,安装好Jenkins后,/api
路径下面有一些基本 api 的介绍,可供参考,但是不是很直观,没有罗列出 api 接口.
本文使用的 jenkins-java 客户端是:
<dependency>
<groupId>com.offbytwo.jenkins</groupId>
<artifactId>jenkins-client</artifactId>
<version>0.3.7</version>
</dependency>
假设 jenkins 部署地址为:
http://localhost:8080
本文用到的 api:
名称 | API |
---|---|
创建 Job | POST http://localhost:8080/createItem/api/json?name=xxx |
更新 Job | POST http://localhost:8080/job/{job_name}/config.xml/api/json |
获取 Job | GET http://localhost:8080/job/{job_name}/api/json |
获取 JobXml | GET http://localhost:8080/job/{job_name}/config.xml/api/json |
创建 Build | POST http://localhost:8080/job/{job_name}/build/api/json |
获取 QueueItem | GET http://localhost:8080/queue/item/17/api/json |
获取 Build信息 | GET http://localhost:8080/job/test/6/api/json |
获取TXT日志 | GET http://localhost:8080/job/test/{build_number}/logText/progressiveText/api/json |
获取 Html 日志 | GET http://localhost:8080/job/test/{build_number}/logText/progressiveHtml/api/json |
创建 Job API
jenkins 的配置都是靠 xml 的格式落地的,所以配置其实都是 xml 的形式.
POST http://localhost:8080/createItem/api/json?name=xxx
将创建需要的 config.xml也用请求传入到服务端,既可以创建成功.
如何知道 config.xml应该如何编写呢?
- 可以在 jenkins 收工创建一个需要的项目,然后编辑完成后,到 jenkins 工作目录下 找到 jobs/{job_name}/config.xml 用他作为模板来书写你需要的模板.
- 通过获取 xml 的 api 来获取
GET http://localhost:8080/job/{job_name}/config.xml/api/json
Java Client
jenkinsServer.createJob("auto_test_job", replacedText, true);
String xml = jenkinsServer.getJobXml("auto_test_job");
修改 Job API
修改 job 也是修改xml.
POST http://localhost:8080/job/{job_name}/config.xml/api/json
config.xml的内容传入到 body 中,contentType 设置为text/xml
Java Client
jenkinsServer.updateJob("auto_test_job", replacedText, true);
构建 Build API
根据 job 名称获取 Job
GET http://localhost:8080/job/{job_name}/api/json
新的构建
POST http://localhost:8080/job/{job_name}/build/api/json
提交到服务器成功后返回一个类似于队列ID的东西,因为是异步构建,那么要获取构建的状态,就需要,用这个队列 id去进一步的获取.
例如:
$ curl -v -XPOST http://localhost:8080/job/test/build/api/json
* Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> POST /job/test/build/api/json HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 201 Created
< Date: Sat, 05 Jan 2019 08:33:45 GMT
< X-Content-Type-Options: nosniff
< Location: http://localhost:8080/queue/item/17/
< Content-Length: 0
< Server: Jetty(9.4.z-SNAPSHOT)
<
* Connection #0 to host localhost left intact
上面的Location: http://localhost:8080/queue/item/17/
就是返回的队列信息,下面的 queueItem 获取就是依赖这个.
根据 QueueId 获取 QueueItem
GET http://localhost:8080/queue/item/17/api/json
举例
$ curl http://localhost:8080/queue/item/17/api/json\?pretty\=true
{
"_class" : "hudson.model.Queue$LeftItem",
"actions" : [
{
"_class" : "hudson.model.CauseAction",
"causes" : [
{
"_class" : "hudson.model.Cause$UserIdCause",
"shortDescription" : "由用户 anonymous 启动",
"userId" : null,
"userName" : "anonymous"
}
]
}
],
"blocked" : false,
"buildable" : false,
"id" : 17,
"inQueueSince" : 1546677225670,
"params" : "",
"stuck" : false,
"task" : {
"_class" : "hudson.maven.MavenModuleSet",
"name" : "test",
"url" : "http://localhost:8080/job/test/",
"color" : "blue"
},
"url" : "queue/item/17/",
"why" : null,
"cancelled" : false,
"executable" : {
"_class" : "hudson.maven.MavenModuleSetBuild",
"number" : 6,
"url" : "http://localhost:8080/job/test/6/"
}
}
获取到的 QueueItem 上面就知道这个 item 有没有被 build 执行,比较关键的参数就是executable
,如果executable
不为空,就说明 jenkins 已经在对这个任务进行 build 的了,build 的编号就是number
,也就是我们在 jenkins 页面上看到的编号.你可以通过url
字段的值在浏览器打开.
获取 Build 详情
既然开始了 build,我们就可以获取 build 的详细信息.
curl http://localhost:8080/job/test/6/api/json\?pretty\=true
{
...
"building" : false,
"description" : null,
"displayName" : "#6",
"duration" : 13631,
"estimatedDuration" : 17999,
"executor" : null,
"fullDisplayName" : "test #6",
"id" : "6",
"keepLog" : false,
"number" : 6,
"queueId" : 17,
"result" : "SUCCESS",
"timestamp" : 1546677234794,
"url" : "http://localhost:8080/job/test/6/",
"builtOn" : "",
...
}
从返回结果可以看到 是否还在 build:"building" : false,
,如果 build 结束状态就在:"result" : "SUCCESS",
获取Build日志
文本:
GET http://localhost:8080/job/test/{build_number}/logText/progressiveText/api/json
html
GET http://localhost:8080/job/test/{build_number}/logText/progressiveHtml/api/json
Java Client
JobWithDetails job = jenkinsServer.getJob(jenkinsJob);
...
QueueReference reference = job.build( true);
...
QueueItem queueItem = jenkinsServer.getQueueItem(new QueueReference(queuePart));
...
Build build = jenkinsServer.getBuild(queueItem);
...
BuildWithDetails details = build.details();
BuildResult result = details.getResult();
...
String logs = details.getConsoleOutputText();