跳至主要内容

API获取Jenkins构建信息

 原文链接

概述

Jenkins 的 REST API 可以从外部调用 Jenkins 实例,一些库例如 jenkins-rest 和 java-client-api 封装了相关 API,可以在 Java 中操作 Jenkins。

详述

本文参考jenkins-rest库,将API根据获取的资源类型不同分为6个类别。

API类型说明
JobsAP任务管理(任务信息、创建、修改)
OBPluginManagerAPI插件管理(插件信息、安装插件)
QueueAPI任务队列相关(队列状态)
StatisticsAPIJenkins统计信息
CrumbIssuerAPI系统哈希值信息(用于防御CSRF攻击)
SystemAPIJenkins系统状态(版本、路径)

术语定义

名词说明
job任务
payload在POST请求中提交的数据
{optionalFolderPath}可选参数:任务所在目录的路径
{project_name}必须参数:任务名称

注意:

  • 在 GET/POST 时需要附加 HTTP 认证才能访问 API
  • 本文使用的数据结构可以在 jenkins-rest/domain 中查看详细定义

Jobs 相关 API

job-info 获取任务信息

GET http://127.0.0.1:8080/{optionalFolderPath}job/{project_name}/api/json

返回类型:JobInfo

字段类型说明 
descriptionString描述 
nameString项目名称 
urlboolean路径 
buildableString是否可构建 
buildsList构建记录 
lastBuildBuildInfo上次构建记录 
……   

build-info 获取构筑信息

GET http://127.0.0.1:8080/{optionalFolderPath}job/{project_name}/{number}/api/json

返回类型:BuildInfo

字段类型说明
artifactsListartifacts
actionsLisactions
buildingboolean 路径 
descriptionString描述
……  

create 使用 XML 文件创建任务

从 XML 文件中加载任务配置并创建任务

POST http://127.0.0.1:8080/createItem

参数

keyvalue
name 任务名称 
payload XML配置文件 

返回类型:RequestStatus

字段类型说明
valueBoolean 
errorsList 

get-config 获取任务配置文件

GET http://127.0.0.1:8080/{optionalFolderPath}job/{project_name}/config.xml

返回类型: String

update-config 更新任务配置文件

POST http://127.0.0.1:8080/{optionalFolderPath}job/{project_name}/config.xml

参数

字段说明
payloadXML配置文件

返回类型:Boolean

get-description 获取任务描述

GET http://127.0.0.1:8080/{optionalFolderPath}job/{project_name}/description

返回类型:String

set-description 设置任务描述

POST http://127.0.0.1:8080/{optionalFolderPath}job/{project_name}/description

参数

keyvalue
description描述

返回类型:Boolean

delete 删除任务

POST http://127.0.0.1:8080/{optionalFolderPath}job/{project_name}/doDelete

返回类型:RequestStatus

enable允许任务

POST http://127.0.0.1:8080/{optionalFolderPath}job/{project_name}/enable

返回类型:Boolean

disable 禁止任务

POST http://127.0.0.1:8080/{optionalFolderPath}job/{project_name}/disable

返回类型:Boolean

build 构建

POST http://127.0.0.1:8080/{optionalFolderPath}job/{project_name}/build

返回类型: IntegerResponse

字段类型说明
valueInteger 
errorsList 

build-with-params 使用参数创建任务

POST http://127.0.0.1:8080/{optionalFolderPath}job/{project_name}/buildWithParameters

参数

keyvalue
payloadMap<String, List> properties

返回类型: IntegerResponse

last-build-number 获取上次构建序号

GET http://127.0.0.1:8080/{optionalFolderPath}job/{project_name}/lastBuild/buildNumber

返回类型:Integer

last-build-timestamp 获取上次构建时间戳

GET http://127.0.0.1:8080/{optionalFolderPath}job/{project_name}/lastBuild/buildTimestamp

返回类型:String

progressive-text 获取构建控制台输出

GET http://127.0.0.1:8080/{optionalFolderPath}job/{project_name}/lastBuild/logText/progressiveText

返回类型:ProgressiveText

字段类型说明
textString控制台输出
sizeInteger字数
hasMoreDataBoolean是否有更多数据

CrumbIssuer 系统哈希值信息(用于防御CSRF攻击)

  • CrumbIssuerApi
  • path: /crumbIssuer/api/xml

crumb

GET http://127.0.0.1:8080/crumbIssuer/api/xml?{key}={value}

参数
keyvalue
xpathconcat(//crumbRequestField,”:”,//crumb)

返回类型:Crumb

字段类型
valueString
errorsList

PluginManager 插件管理(插件信息、安装插件)

  • PluginManagerApi
  • path: /pluginManager

plugins 插件列表

GET http://127.0.0.1:8080/pluginManager/api/json

返回类型:List

字段类型说明
activeBoolean 
backupVersion String  
bundledBoolean 
deletedBoolean 
downgradableBoolean 
enabledBoolean 
longNameString 
……  

installNecessaryPlugins 安装插件

POST http://127.0.0.1:8080/pluginManager/installNecessaryPlugins

参数

payload: <jenkins><install plugin="{pluginID}"/></jenkins>

字段说明
{pluginID}要安装的插件ID

返回类型:RequestStatus

Queue 任务队列相关(队列状态)

  • QueueApi
  • path: /queue

queue 所有任务队列信息

GET http://127.0.0.1:8080/queue/api/json

返回类型:List

字段类型说明
blockedBoolean是否阻塞
buildableBoolean是否可构建
idInteger 
inQueueSinceLong 
paramsMap<String, String>任务参数
taskTaskTask中包含任务名称和URL
……  

item 任务队列信息

GET http://127.0.0.1:8080/queue/item/{queueId}/api/json

参数

字段说明
{queueId}任务队列ID

返回类型:QueueItem

cancel 取消任务队列 |

POST http://127.0.0.1:8080/cancelItem?id={id}

参数

字段说明
{id}任务队列ID

返回类型:RequestStatus

Statistics 统计信息

  • StatisticsApi
  • path: /

overall-load

GET http://127.0.0.1:8080/overallLoad/api/json

返回类型:OverallLoad

字段类型说明
availableExecutorsMap<String, String> 
busyExecutorsMap<String, String> 
connectingExecutorsMap<String, String> 
definedExecutorsMap<String, String> 
idleExecutorsMap<String, String> 
onlineExecutorsMap<String, String> 
queueLengthMap<String, String> 
totalExecutorsMap<String, String> 
totalQueueLengthMap<String, String> 

System 系统信息

  • path: /

返回类型:SystemInfo

字段类型说明
hudsonVersionString 
jenkinsVersionString 
jenkinsSessionString 
instanceIdentityString 
sshEndpointString 
serverString 

举几个例子

获取jenkins job状态

oot@node248:~# curl http://172.12.12.234:8080/job/pytest_7.0/lastBuild/api/xml --user jenkins:1

结果示例如下:

1
2
<freeStyleBuild _class='hudson.model.FreeStyleBuild'><action _class='hudson.model.CauseAction'><cause _class='hudson.model.Cause$UserIdCause'><shortDescription>Started by user jenkins</shortDescription><userId>jenkins</userId><userName>jenkins</userName></cause></action><action></action><action _class='hudson.plugins.git.util.BuildData'><buildsByBranchName><refsremotesoriginmaster _class='hudson.plugins.git.util.Build'><buildNumber>68</buildNumber><marked><SHA1>6388b644c4405a31611f179dca6c1e485ad92aa5</SHA1><branch><SHA1>6388b644c4405a31611f179dca6c1e485ad92aa5</SHA1><name>refs/remotes/origin/master</name></branch></marked><revision><SHA1>6388b644c4405a31611f179dca6c1e485ad92aa5</SHA1><branch><SHA1>6388b644c4405a31611f179dca6c1e485ad92aa5</SHA1><name>refs/remotes/origin/master</name></branch></revision></refsremotesoriginmaster></buildsByBranchName><lastBuiltRevision><SHA1>6388b644c4405a31611f179dca6c1e485ad92aa5</SHA1><branch><SHA1>6388b644c4405a31611f179dca6c1e485ad92aa5</SHA1><name>refs/remotes/origin/master</name></branch></lastBuiltRevision><remoteUrl>git@10.1.12.10:yunzeng_wang/pytest_framework.git</remoteUrl><scmName></scmName></action><action _class='hudson.plugins.git.GitTagAction'></action><action></action><action></action><action></action><action></action><action></action><action></action><action _class='hudson.plugins.violations.ViolationsBuildAction'></action><action></action><action></action><action></action><artifact><displayPath>allure-report.zip</displayPath><fileName>allure-report.zip</fileName><relativePath>allure-report.zip</relativePath></artifact><building>false</building><displayName>#68</displayName><duration>87155647</duration><estimatedDuration>85209450</estimatedDuration><fullDisplayName>pytest_7.0 #68</fullDisplayName><id>68</id><keepLog>false</keepLog><number>68</number><queueId>343</queueId><result>UNSTABLE</result><timestamp>1588762417068</timestamp><url>http://172.12.12.234:8080/job/pytest_7.0/68/</url><builtOn></builtOn><changeSet _class='hudson.plugins.git.GitChangeSetList'><kind>git</kind></changeSet><culprit><absoluteUrl>http://172.12.12.234:8080/user/shengdan061</absoluteUrl><fullName>shengdan061</fullName></culprit><culprit><absoluteUrl>http://172.12.12.234:8080/user/yunzeng_wang</absoluteUrl><fullName>yunzeng_wang</fullName></culprit></freeStyleBuild>root@node248:~#
root@node248:~#

获取某一个构建版本号为56的构建结果

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的账号和密码

评论

此博客中的热门博文

Resolving errSecInternalComponent errors during code signing

原文链接 One code signing issue I commonly see, both here on DevForums and in my Day Job™ with DTS, is that the codesign command fails with errSecInternalComponent. This issue crops up in a wide variety of circumstances and the correct fix depends on the specific problem. This post is my attempt to clarify the potential causes of this error and help folks resolve it. If you have any questions or comments about this, please start a new thread, tagging it with Code Signing so that I see it. Share and Enjoy — Quinn “The Eskimo!” @ Developer Technical Support @ Apple let myEmail = "eskimo" + "1" + "@" + "apple.com" Resolving errSecInternalComponent errors during code signing In some circumstances the codesign command might fail with the error errSecInternalComponent. For example: % codesign -s "Apple Development" "MyTrue" MyTrue: errSecInternalComponent This typically affects folks who are signing code in a nonstandard environm...

iOS:检测使用VPN或Proxy

参考链接: https://www.jianshu.com/p/c3b950dbf86a https://gist.github.com/PramodJoshi/4faad4c91f7dcb4eb9b06be8390c01db http://noodlecode.net/2018/04/check-if-ios-app-is-connected-to-vpn 第一种方法 需要导入框架CFNetwork 然后,这个方法是mrc的:需要添加-fno-objc-arc的flag 代码如下: + ( BOOL )getProxyStatus { NSDictionary *proxySettings = NSMakeCollectable ([( NSDictionary *) CFNetworkCopySystemProxySettings () autorelease]); NSArray *proxies = NSMakeCollectable ([( NSArray *) CFNetworkCopyProxiesForURL (( CFURLRef )[ NSURL URLWithString: @"http://www.google.com" ], ( CFDictionaryRef )proxySettings) autorelease]); NSDictionary *settings = [proxies objectAtIndex: 0 ]; NSLog ( @"host=%@" , [settings objectForKey:( NSString *)kCFProxyHostNameKey]); NSLog ( @"port=%@" , [settings objectForKey:( NSString *)kCFProxyPortNumberKey]); NSLog ( @"type=%@" , [settings objectForKey:( NSString *)kCFProxyTypeKey]); if ([[settings object...

去广告DNS设置,国内ADGuard DNS方案,手机电脑iOS去广告,保护隐私

 原文链接 之前分享过使用mac系统搭建adguard home,这几个月用下来零零散散基本上也被弃用了。主要原因是因为需要保持电脑一直开机。但是我的电脑是笔记本,存在移动各个地域的情况,也就是说只能够屏蔽电脑自身,对于手机而言不太现实。今天偶然发现dnspod推出了高级版的公共解析。dnspod背靠腾讯云,肯定是合法合规的公共解析服务,这个高级版用起来不错。 国内自己搭建解析服务是违法行为,所以这也是为什么使用dnspod的原因。 后台截图 开始使用 首先我们先进入dnspod的公共解析页面,点击开始使用。 专业版公共解析 dnspod会提供几种预设,我们选择「开发者」即可 开发者 然后你就成功的申请到自己个人使用的dns了! 更新拦截规则 我们可以将常见的广告过滤规则加入到dns中。我们在顶部选项卡中选择「拦截规则」。 拦截规则设置 打开adguard adguard 绑定iOS设备 推荐使用描述文件的方式,删除配置时删除描述文件即可。 描述文件 绑定macOS 推荐使用描述文件的方式,删除配置时删除描述文件即可。 描述文件 mac需要在「系统偏好设置」的「网络」中查看是否正在运行。 代理 如果没有运行需要点击「···」来启动服务。 启动服务 绑定路由器 找到自己路由器的DHCP设置,修改dns,然后记得绑定自己的ip。 修改dns 绑定ip 费用 目前有300万次/月的免费额度,但没有超出之后的价格。300万次一个人比较难用完,可以放心使用。 我个人使用iOS设备两台、智能家居、电脑两台,日均请求数大致2万/日。 判断是否搭建成功 可以通过查看日志的方式,日志大概有半小时到一小时的延迟,请耐心等待。