jenkins-rest 库是一个面向对象的 Java 项目,它通过编程方式提供对 Jenkins REST API 的访问,以访问 Jenkins 提供的一些远程 API。它使用 jclouds 工具包构建,可以轻松扩展以支持更多 REST 端点。其功能集不断发展,用户可以通过拉取请求贡献新的端点。在当前状态下,使用此库可以提交作业、跟踪其通过队列的进度并监控其执行直到完成,并获取构建状态。目前提供的服务包括:
注意,不同的版本要求的jdk版本不一致,可以在这里查看,
https://github.com/cdancy/jenkins-rest
以下示例版本1.0.2要求jdk11
<dependency>
<groupId>io.github.cdancy</groupId>
<artifactId>jenkins-rest</artifactId>
<version>1.0.2</version>
<classifier>all</classifier>
</dependency>
假设Jenkins地址是 http://192.168.56.114:8080
private static JenkinsClient client;
private static final String JENKINS_URL = "http://192.168.56.114:8080";
public static void init() {
client = JenkinsClient.builder()
.endPoint(JENKINS_URL) // Jenkins地址
.credentials("admin:admin123") // 账号密码.
.build();
SystemInfo systemInfo = client.api().systemApi().systemInfo();
log.info("jenkinsVersion: {}", systemInfo.jenkinsVersion());
}
public static void jobs() {
JobsApi jobsApi = client.api().jobsApi();
JobList jobList = jobsApi.jobList("");
jobList.jobs().forEach(job -> System.out.println(job.name()));
}
返回的value不为null,代表执行成功
public static void build() {
JobsApi jobsApi = client.api().jobsApi();
// 必须是无参数任务
String jobName = "job_no_params";
IntegerResponse response = jobsApi.build("", jobName);
System.out.println("build = " + response);
// 返回的value不为null,代表执行成功
if (response.value() != null) {
System.out.println("任务执行成功:" + jobName);
} else {
System.out.println("任务执行失败:" + response.errors());
}
}
这里的任务必须是带参数的,如果调用的无参任务,会报错!
返回的value不为null,代表执行成功
public static void buildWithParameters() {
JobsApi jobsApi = client.api().jobsApi();
Map<String, List<String>> properties = new HashMap<>();
properties.put("ENV", Collections.singletonList("prod"));
// 必须是有参数任务
String jobName = "job_with_params";
IntegerResponse response = jobsApi.buildWithParameters("", jobName, properties);
System.out.println("buildWithParameters = " + response);
// 返回的value不为null,代表执行成功
if (response.value() != null) {
System.out.println("任务执行成功:" + jobName);
} else {
System.out.println("任务执行失败:" + response.errors());
}
}
创任务必须传入xml配置数据
public static void createJob() {
JobsApi jobsApi = client.api().jobsApi();
String configXml = "<?xml version='1.1' encoding='UTF-8'?>\n" +
"<flow-definition plugin=\"workflow-job@1385.vb_58b_86ea_fff1\">\n" +
" <actions>\n" +
" <org.jenkinsci.plugins.pipeline.modeldefinition.actions.DeclarativeJobAction plugin=\"pipeline-model-definition@2.2151.ve32c9d209a_3f\"/>\n" +
" <org.jenkinsci.plugins.pipeline.modeldefinition.actions.DeclarativeJobPropertyTrackerAction plugin=\"pipeline-model-definition@2.2151.ve32c9d209a_3f\">\n" +
" <jobProperties/>\n" +
" <triggers/>\n" +
" <parameters/>\n" +
" <options/>\n" +
" </org.jenkinsci.plugins.pipeline.modeldefinition.actions.DeclarativeJobPropertyTrackerAction>\n" +
" </actions>\n" +
" <description></description>\n" +
" <keepDependencies>false</keepDependencies>\n" +
" <properties/>\n" +
" <definition class=\"org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition\" plugin=\"workflow-cps@3826.v3b_5707fe44da_\">\n" +
" <script>pipeline {\n" +
" agent any\n" +
" environment {\n" +
" CC = 'clang'\n" +
" }\n" +
" stages {\n" +
" stage('Example') {\n" +
" environment {\n" +
" DEBUG_FLAGS = '-g'\n" +
" }\n" +
" steps {\n" +
" sh 'printenv'\n" +
" }\n" +
" }\n" +
" }\n" +
"}</script>\n" +
" <sandbox>true</sandbox>\n" +
" </definition>\n" +
" <triggers/>\n" +
" <disabled>false</disabled>\n" +
"</flow-definition>\n";
String newJobName = "java_api_02";
RequestStatus status = jobsApi.create("", newJobName, configXml);
if (status.value()) {
System.out.println("任务创建成功: " + newJobName);
} else {
System.out.println("任务创建失败: " + newJobName);
}
}
public static void delete() {
JobsApi jobsApi = client.api().jobsApi();
String jobName = "api05";
RequestStatus status = jobsApi.delete("", jobName);
System.out.println("delete = " + status);
if (status.value()){
System.out.println("任务删除成功: " + jobName);
}else{
System.out.println("任务删除失败: " + status.errors());
}
}
public static void disable() {
JobsApi jobsApi = client.api().jobsApi();
boolean flag = jobsApi.disable("", "api01");
System.out.println("disable = " + flag);
}
public static void enable() {
JobsApi jobsApi = client.api().jobsApi();
boolean flag = jobsApi.enable("", "api01");
System.out.println("enable = " + flag);
}
public static void lastBuildNumber() {
JobsApi jobsApi = client.api().jobsApi();
String jobName = "job_no_params";
Integer lastBuildNumber = jobsApi.lastBuildNumber("", jobName);
System.out.println("lastBuildNumber = " + lastBuildNumber);
}
public static void overallLoad() {
OverallLoad overallLoad = client.api().statisticsApi().overallLoad();
System.out.println("overallLoad = " + overallLoad);
}