Jenkins基础:API:3:设定返回信息格式

在前面的文章中提到过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$ 
淼叔 CSDN认证博客专家 神经网络 TensorFlow NLP
资深架构师,PMP、OCP、CSM、HPE University讲师,EXIN DevOps Professional与DevOps Master认证讲师,曾担任HPE GD China DevOps & Agile Leader,帮助企业级客户提供DevOps咨询培训以及实施指导。熟悉通信和金融领域,有超过十年金融外汇行业的架构设计、开发、维护经验,在十几年的IT从业生涯中拥有了软件开发设计领域接近全生命周期的经验和知识积累,著有企业级DevOps技术与工具实战。
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页