一、Maven
<dependency><groupId>com.cdancy</groupId><artifactId>jenkins-rest</artifactId><version>0.0.20</version>
</dependency>
github地址:https://github.com/cdancy/jenkins-rest
如果需要最新的代码版本,则拉取源码编译安装在maven仓库内
二、编程api
(1)构建jenkins客户端
JenkinsClient client = JenkinsClient.builder()
.endPoint("http://127.0.0.1:8080") // Optional. Defaults to http://127.0.0.1:8080
.credentials("admin:password") // Optional.
.build();JobsApi jobsApi = client.api().jobsApi();
//...
client.close();
(2)创建Jenkins job任务信息
RequestStatus status = jobsApi.create(null, "jobName", URLEncoder.encode(xmlJobConfig, "utf-8"));
boolean isSuccess = status!=null && Boolean.TRUE.equals(status.value())
&& status.errors().isEmpty();
(3)获取job任务信息
JobInfo jobInfo = jobsApi.jobInfo(null, "jobName");
(4)更新jenkins job任务信息
Boolean status = jobsApi.config(null, "jobName", URLEncoder.encode(xmlJobConfig, "utf-8"));
(5)构建
Map<String, List<String>> buildMap = new HashMap();
IntegerResponse buildStatus = jobsApi.buildWithParameters(null, jobName, buildMap);
boolean isSuccess = buildStatus !=null && buildStatus.errors().isEmpty();
Integewr buidNumber = buildStatus.getValue();
(6)工作负载情况
Workflow workflow = jobsApi.workflow(null, jobName, buildNumber);
int progress = workflow.stages().size() - 1;//进度boolean inProgress = workflow != null && "IN_PROGRESS".equals(workflow.status());
boolean isSuccess = workflow != null && "SUCCESS".equals(workflow.status());
(7)日志
ProgressiveText text = jobApi.progressiveText(null, jobName, buildNumber, 0);
String logText = text.text();