在前面的文章中提到过Jenkins的API的返回信息可以指定为xml/json/python方式,这篇文章在前面通过API创建Job并拷贝Job的基础之上,说明一下设定返回信息的具体格式。
返回信息格式
返回信息可以指定为xml/json/python方式,以/api为例,使用方式如下所示。
xml格式返回api信息示例:/api/xml
json格式返回api信息示例:/api/json
python格式返回api信息示例:/api/python
事前准备
在Jenkins的环境中,创建Job并以此创建的Job为基础拷贝生成一个新的Job。
xml格式
使用/api/xml返回xml格式的信息,这里使用xmllint对结果进行格式化,可以使用如下命令进行确认
命令:curl http://localhost:32002/api/xml 2>/dev/null |xmllint --format -
- 执行日志示例
liumiaocn:jenkins liumiao$ curl http://localhost:32002/api/xml 2>/dev/null |xmllint --format -
<?xml version="1.0"?>
<hudson _class="hudson.model.Hudson">
<assignedLabel>
<name>master</name>
</assignedLabel>
<mode>NORMAL</mode>
<nodeDescription>the master Jenkins node</nodeDescription>
<nodeName/>
<numExecutors>2</numExecutors>
<job _class="hudson.model.FreeStyleProject">
<name>free_style_job</name>
<url>http://localhost:32002/job/free_style_job/</url>
<color>notbuilt</color>
</job>
<job _class="hudson.model.FreeStyleProject">
<name>free_style_job_copy</name>
<url>http://localhost:32002/job/free_style_job_copy/</url>
<color>notbuilt</color>
</job>
<overallLoad/>
<primaryView _class="hudson.model.AllView">
<name>all</name>
<url>http://localhost:32002/</url>
</primaryView>
<quietingDown>false</quietingDown>
<slaveAgentPort>50000</slaveAgentPort>
<unlabeledLoad _class="jenkins.model.UnlabeledLoadStatistics"/>
<useCrumbs>true</useCrumbs>
<useSecurity>true</useSecurity>
<view _class="hudson.model.AllView">
<name>all</name>
<url>http://localhost:32002/</url>
</view>
</hudson>
liumiaocn:jenkins liumiao$
json格式
使用/api/json返回json格式的信息,这里使用jq对结果进行格式化,可以使用如下命令进行确认
命令:curl http://localhost:32002/api/json 2>/dev/null |jq .
- 执行日志示例
liumiaocn:jenkins liumiao$ curl http://localhost:32002/api/json 2>/dev/null |jq .
{
"_class": "hudson.model.Hudson",
"assignedLabels": [
{
"name": "master"
}
],
"mode": "NORMAL",
"nodeDescription": "the master Jenkins node",
"nodeName": "",
"numExecutors": 2,
"description": null,
"jobs": [
{
"_class": "hudson.model.FreeStyleProject",
"name": "free_style_job",
"url": "http://localhost:32002/job/free_style_job/",
"color": "notbuilt"
},
{
"_class": "hudson.model.FreeStyleProject",
"name": "free_style_job_copy",
"url": "http://localhost:32002/job/free_style_job_copy/",
"color": "notbuilt"
}
],
"overallLoad": {},
"primaryView": {
"_class": "hudson.model.AllView",
"name": "all",
"url": "http://localhost:32002/"
},
"quietingDown": false,
"slaveAgentPort": 50000,
"unlabeledLoad": {
"_class": "jenkins.model.UnlabeledLoadStatistics"
},
"useCrumbs": true,
"useSecurity": true,
"views": [
{
"_class": "hudson.model.AllView",
"name": "all",
"url": "http://localhost:32002/"
}
]
}
liumiaocn:jenkins liumiao$
注: 如果RHEL或者CENTOS上的如果没有jq的话,需要按照如下步骤进行安装或者下载可执行文件放至PATH的搜索路径中即可。
- 步骤1: yum install epel-release
- 步骤2: yum install jq
python格式
使用/api/python返回python格式的信息,这里使用jq对结果进行格式化,可以使用如下命令进行确认
命令:curl http://localhost:32002/api/python 2>/dev/null
- 执行日志示例
liumiaocn:jenkins liumiao$ curl http://localhost:32002/api/python 2>/dev/null
{"_class":"hudson.model.Hudson","assignedLabels":[{"name":"master"}],"mode":"NORMAL","nodeDescription":"the master Jenkins node","nodeName":"","numExecutors":2,"description":None,"jobs":[{"_class":"hudson.model.FreeStyleProject","name":"free_style_job","url":"http://localhost:32002/job/free_style_job/","color":"notbuilt"},{"_class":"hudson.model.FreeStyleProject","name":"free_style_job_copy","url":"http://localhost:32002/job/free_style_job_copy/","color":"notbuilt"}],"overallLoad":{},"primaryView":{"_class":"hudson.model.AllView","name":"all","url":"http://localhost:32002/"},"quietingDown":False,"slaveAgentPort":50000,"unlabeledLoad":{"_class":"jenkins.model.UnlabeledLoadStatistics"},"useCrumbs":True,"useSecurity":True,"views":[{"_class":"hudson.model.AllView","name":"all","url":"http://localhost:32002/"}]}liumiaocn:jenkins liumiao$