Jenkins 的 REST API 可以从外部调用 Jenkins 实例,一些库例如 jenkins-rest 和 java-client-api 封装了相关 API,可以在 Java 中操作 Jenkins。
本文参考jenkins-rest库,将API根据获取的资源类型不同分为6个类别。
API类型 | 说明 |
---|---|
JobsAP | 任务管理(任务信息、创建、修改) |
OBPluginManagerAPI | 插件管理(插件信息、安装插件) |
QueueAPI | 任务队列相关(队列状态) |
StatisticsAPI | Jenkins统计信息 |
CrumbIssuerAPI | 系统哈希值信息(用于防御CSRF攻击) |
SystemAPI | Jenkins系统状态(版本、路径) |
名词 | 说明 |
---|---|
job | 任务 |
payload | 在POST请求中提交的数据 |
{optionalFolderPath} | 可选参数:任务所在目录的路径 |
{project_name} | 必须参数:任务名称 |
注意:
在 GET/POST 时需要附加 HTTP 认证才能访问 API
本文使用的数据结构可以在 jenkins-rest/domain 中查看详细定义
GET http://127.0.0.1:8080/{optionalFolderPath}job/{project_name}/api/json
返回类型:JobInfo
字段 | 类型 | 说明 |
---|---|---|
description | String | 描述 |
name | String | 项目名称 |
url | boolean | 路径 |
buildable | String | 是否可构建 |
builds | List | 构建记录 |
lastBuild | BuildInfo | 上次构建记录 |
… |
GET http://127.0.0.1:8080/{optionalFolderPath}job/{project_name}/{number}/api/json
返回类型:BuildInfo
字段 | 类型 | 说明 |
---|---|---|
artifacts | List | artifacts |
actions | Lis | actions |
building | boolean 路径 | |
description | String | 描述 |
… |
从 XML 文件中加载任务配置并创建任务
POST http://127.0.0.1:8080/createItem
key | value |
---|---|
name | 任务名称 |
payload | XML配置文件 |
返回类型:RequestStatus
|字段 | 类型 | 说明 |
| ------- | -------------------- |
| value | Boolean |
| errors | List |
GET http://127.0.0.1:8080/{optionalFolderPath}job/{project_name}/config.xml
返回类型: String
POST http://127.0.0.1:8080/{optionalFolderPath}job/{project_name}/config.xml
字段 | 说明 |
---|---|
payload | XML配置文件 |
返回类型:Boolean
GET http://127.0.0.1:8080/{optionalFolderPath}job/{project_name}/description
返回类型:String
POST http://127.0.0.1:8080/{optionalFolderPath}job/{project_name}/description
key | value |
---|---|
description | 描述 |
返回类型:Boolean
POST http://127.0.0.1:8080/{optionalFolderPath}job/{project_name}/doDelete
返回类型:RequestStatus
POST http://127.0.0.1:8080/{optionalFolderPath}job/{project_name}/enable
返回类型:Boolean
POST http://127.0.0.1:8080/{optionalFolderPath}job/{project_name}/disable
返回类型:Boolean
POST http://127.0.0.1:8080/{optionalFolderPath}job/{project_name}/build
返回类型: IntegerResponse
字段 | 类型 | 说明 |
---|---|---|
value | Integer | |
errors | List |
POST http://127.0.0.1:8080/{optionalFolderPath}job/{project_name}/buildWithParameters
key | value |
---|---|
payload | Map |
返回类型: IntegerResponse
GET http://127.0.0.1:8080/{optionalFolderPath}job/{project_name}/lastBuild/buildNumber
返回类型:Integer
GET http://127.0.0.1:8080/{optionalFolderPath}job/{project_name}/lastBuild/buildTimestamp
返回类型:String
GET http://127.0.0.1:8080/{optionalFolderPath}job/{project_name}/lastBuild/logText/progressiveText
返回类型:ProgressiveText
字段 | 类型 | 说明 |
---|---|---|
text | String | 控制台输出 |
size | Integer | 字数 |
hasMoreData | Boolean | 是否有更多数据 |
CrumbIssuerApi
path: /crumbIssuer/api/xml
GET http://127.0.0.1:8080/crumbIssuer/api/xml?{key}={value}
key | value |
---|---|
xpath | concat(//crumbRequestField,“:”,//crumb) |
返回类型:Crumb
字段 | 类型 |
---|---|
value | String |
errors | List |
PluginManagerApi
path: /pluginManager
GET http://127.0.0.1:8080/pluginManager/api/json
返回类型:List
字段 | 类型 | 说明 |
---|---|---|
active | Boolean | |
backupVersion String | ||
bundled | Boolean | |
deleted | Boolean | |
downgradable | Boolean | |
enabled | Boolean | |
longName | String | |
… |
POST http://127.0.0.1:8080/pluginManager/installNecessaryPlugins
payload:
字段 | 说明 |
---|---|
{pluginID} | 要安装的插件ID |
返回类型:RequestStatus
QueueApi
path: /queue
GET http://127.0.0.1:8080/queue/api/json
返回类型:List
字段 | 类型 | 说明 |
---|---|---|
blocked | Boolean | 是否阻塞 |
buildable | Boolean | 是否可构建 |
id | Integer | |
inQueueSince | Long | |
params | Map |
任务参数 |
task | Task | Task中包含任务名称和URL |
… |
GET http://127.0.0.1:8080/queue/item/{queueId}/api/json
字段 | 说明 |
---|---|
{queueId} | 任务队列ID |
返回类型:QueueItem
POST http://127.0.0.1:8080/cancelItem?id={id}
字段 | 说明 |
---|---|
{id} | 任务队列ID |
返回类型:RequestStatus
StatisticsApi
path: /
GET http://127.0.0.1:8080/overallLoad/api/json
返回类型:OverallLoad
字段 | 类型 | 说明 |
---|---|---|
availableExecutors | Map |
|
busyExecutors | Map |
|
connectingExecutors | Map |
|
definedExecutors | Map |
|
idleExecutors | Map |
|
onlineExecutors | Map |
|
queueLength | Map |
|
totalExecutors | Map |
|
totalQueueLength | Map |
path: /
返回类型:SystemInfo
字段 | 类型 | 说明 |
---|---|---|
hudsonVersion | String | |
jenkinsVersion | String | |
jenkinsSession | String | |
instanceIdentity | String | |
sshEndpoint | String | |
server | String |
oot@node248:~# curl http://172.12.12.234:8080/job/pytest_7.0/lastBuild/api/xml --user jenkins:1
结果示例如下:
shell
Started by user jenkins jenkins jenkins 68 6388b644c4405a31611f179dca6c1e485ad92aa5 6388b644c4405a31611f179dca6c1e485ad92aa5 refs/remotes/origin/master 6388b644c4405a31611f179dca6c1e485ad92aa5 6388b644c4405a31611f179dca6c1e485ad92aa5 refs/remotes/origin/master 6388b644c4405a31611f179dca6c1e485ad92aa5 6388b644c4405a31611f179dca6c1e485ad92aa5 refs/remotes/origin/master git@10.1.12.10:yunzeng_wang/pytest_framework.git allure-report.zip allure-report.zip allure-report.zip false #68 87155647 85209450 pytest_7.0 #68 68 false 68 343 UNSTABLE 1588762417068 http://172.12.12.234:8080/job/pytest_7.0/68/ git http://172.12.12.234:8080/user/shengdan061 shengdan061 http://172.12.12.234:8080/user/yunzeng_wang yunzeng_wang root@node248:~#
root@node248:~#
curl http://172.12.12.234:8080/job/pytest_7.0/56/api/xml --user jenkins:1
curl http://172.12.12.234:8080/job/pytest_7.0/lastBuild/buildNumber --user jenkins:1
curl http://172.12.12.234:8080/job/pytest_7.0/api/xml --user jenkins:1
说明:
以上pytest_7.0为项目名称,jenkins:1是访问jenkins server的账号和密码
来源: Transcendent
文章作者: Gavin Wang
文章链接: API获取Jenkins构建信息 | Transcendent
本文章著作权归作者所有,任何形式的转载都请注明出处。