Java调用Jenkins API获取任务构建归档文件
Centos清除wnTKYg 删除挖矿病毒 redis漏洞
基础API
示例移步另一篇文章
1、什么是Jenkins API
2、Jenkins API分类
①、JSON API
②、XML API
③、Python API
④、其他
3、使用Java调用Jenkins API
①、View
Ⅰ、创建View
Ⅱ、获取View
Ⅲ、获取View配置 XML 信息
Ⅳ、更新View
Ⅴ、删除View
②、Job
Ⅰ、创建Job
Ⅱ、更新Job
Ⅲ、获取Job基本信息
Ⅳ、获取Maven Job信息
Ⅴ、获取Job列表
Ⅵ、根据View名称获取Job列表
Ⅶ、查看Job XML信息
Ⅷ、无参数 Job build
Ⅸ、有参构建
Ⅹ、停止最后构建的 Job Build
ⅩⅠ、禁用 Job
ⅩⅡ、启用Job
ⅩⅢ、删除 Job
③、编译
Ⅰ、获取Build基本信息
Ⅱ、获取 Job 最后的 Build
Ⅲ、获取 Job 首次 Build
Ⅳ、根据 Job Build 编号获取编译信息
Ⅴ、获取全部 Job Build列表
Ⅵ、获取 Job 一定范围的 Build 列表
Ⅶ、获取Build详细信息
Ⅷ、获取 Build Log 日志信息
Ⅸ、获取正在执行构建任务的日志信息
④、其他
Ⅰ、获取主机信息
Ⅱ、重启Jenkins
Ⅲ、安全重启Jenkins
Ⅳ、安全结束Jenkins
Ⅴ、关闭连接
Ⅵ、根据 Label 查找代理节点信息
Ⅶ、判断Jenkins是否运行
Ⅷ、获取 Jenkins 插件信息
4、问题处理
①、HttpResponseException: Forbidden
Ⅰ、关闭跨域保护(高版本)
Ⅱ、携带Token建立连接
1、什么是Jenkins API
Jenkins 远程 API 能够通过 Http 协议远程调用相关命令操作 Jenkins 进行 Jenkins 视图、任务、插件、构建信息、任务日志信息、统计信息等,非常容易与其配合更好的完成 CI/CD 工作
2、Jenkins API分类
①、JSON API
浏览器打开http://JenkinsURL/api/json
格式化如下
{
"_class":"hudson.model.Hudson",
"assignedLabels":[
{
"name":"master"
}
],
"mode":"NORMAL",
"nodeDescription":"the master Jenkins node",
"nodeName":"",
"numExecutors":2,
"description":null,
"jobs":[
{
"_class":"hudson.maven.MavenModuleSet",
"name":"CenterServer",
"url":"http://192.168.225.130/job/CenterServer/",
"color":"blue"
},
{
"_class":"hudson.maven.MavenModuleSet",
"name":"HelloWorld",
"url":"http://192.168.225.130/job/HelloWorld/",
"color":"blue"
},
{
"_class":"hudson.maven.MavenModuleSet",
"name":"PMServer",
"url":"http://192.168.225.130/job/PMServer/",
"color":"blue"
}
],
"overallLoad":{
},
"primaryView":{
"_class":"hudson.model.AllView",
"name":"all",
"url":"http://192.168.225.130/"
},
"quietDownReason":null,
"quietingDown":false,
"slaveAgentPort":-1,
"unlabeledLoad":{
"_class":"jenkins.model.UnlabeledLoadStatistics"
},
"url":"http://192.168.225.130/",
"useCrumbs":true,
"useSecurity":true,
"views":[
{
"_class":"hudson.model.AllView",
"name":"all",
"url":"http://192.168.225.130/"
}
]
}
②、XML API
浏览器打开http://JenkinsURL/api/xml
<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.maven.MavenModuleSet">
<name>CenterServer</name>
<url>http://192.168.225.130/job/CenterServer/</url>
<color>blue</color>
</job>
<job _class="hudson.maven.MavenModuleSet">
<name>HelloWorld</name>
<url>http://192.168.225.130/job/HelloWorld/</url>
<color>blue</color>
</job>
<job _class="hudson.maven.MavenModuleSet">
<name>PMServer</name>
<url>http://192.168.225.130/job/PMServer/</url>
<color>blue</color>
</job>
<overallLoad/>
<primaryView _class="hudson.model.AllView">
<name>all</name>
<url>http://192.168.225.130/</url>
</primaryView>
<quietingDown>false</quietingDown>
<slaveAgentPort>-1</slaveAgentPort>
<unlabeledLoad _class="jenkins.model.UnlabeledLoadStatistics"/>
<url>http://192.168.225.130/</url>
<useCrumbs>true</useCrumbs>
<useSecurity>true</useSecurity>
<view _class="hudson.model.AllView">
<name>all</name>
<url>http://192.168.225.130/</url>
</view>
</hudson>
③、Python API
浏览器打开http://JenkinsURL/api/python
格式化如下
{
"_class":"hudson.model.Hudson",
"assignedLabels":[
{
"name":"master"
}
],
"mode":"NORMAL",
"nodeDescription":"the master Jenkins node",
"nodeName":"",
"numExecutors":2,
"description":None,
"jobs":[
{
"_class":"hudson.maven.MavenModuleSet",
"name":"CenterServer",
"url":"http://192.168.225.130/job/CenterServer/",
"color":"blue"
},
{
"_class":"hudson.maven.MavenModuleSet",
"name":"HelloWorld",
"url":"http://192.168.225.130/job/HelloWorld/",
"color":"blue"
},
{
"_class":"hudson.maven.MavenModuleSet",
"name":"PMServer",
"url":"http://192.168.225.130/job/PMServer/",
"color":"blue"
}
],
"overallLoad":{
},
"primaryView":{
"_class":"hudson.model.AllView",
"name":"all",
"url":"http://192.168.225.130/"
},
"quietDownReason":None,
"quietingDown":False,
"slaveAgentPort":-1,
"unlabeledLoad":{
"_class":"jenkins.model.UnlabeledLoadStatistics"
},
"url":"http://192.168.225.130/",
"useCrumbs":True,
"useSecurity":True,"views":[
{
"_class":"hudson.model.AllView",
"name":"all",
"url":"http://192.168.225.130/"
}
]
}
④、其他
根据以上三种API查询出来的信息中出现了几个任务的url如http://192.168.225.130/job/HelloWorld/,可以在在后面追加/api/xml访问查看对应Job的构建记录
根据提供了若干构建记录还可以查看构建的详细信息,直接访问提供的url即可http://192.168.225.130/job/helloworld/11/
3、使用Java调用Jenkins API
创建一个Jenkins API Demo工程
导入依赖
<dependencies>
<!-- https://mvnrepository.com/artifact/com.offbytwo.jenkins/jenkins-client -->
<dependency>
<groupId>com.offbytwo.jenkins</groupId>
<artifactId>jenkins-client</artifactId>
<version>0.3.8</version>
</dependency>
</dependencies>
创建Jenkins链接工具类JenkinsConnectFactory
package com.phz.jenkins_api_demo.factory;
import com.offbytwo.jenkins.JenkinsServer;
import com.offbytwo.jenkins.client.JenkinsHttpClient;
import java.net.URI;
import java.net.URISyntaxException;
/**
* @author PengHuAnZhi
* @ProjectName Jenkins API Demo
* @Description Jenkins连接工具类
* @time 2021/10/8 14:33
*/
public class JenkinsConnectFactory {
static final String JENKINS_URL = "http://192.168.225.130";
static final String JENKINS_USERNAME = "PengHuanZhi";
static final String JENKINS_PASSWORD = "123456789";
private static JenkinsHttpClient jenkinsHttpClient = null;
private static JenkinsServer jenkinsServer = null;
public static JenkinsHttpClient getClientInstance() {
if (jenkinsHttpClient == null) {
try {
jenkinsHttpClient = new JenkinsHttpClient(new URI(JENKINS_URL, JENKINS_USERNAME, JENKINS_PASSWORD), "PengHuanZhi", "11622ce96b51846c8658a6390253d87f7e");
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
return jenkinsHttpClient;
}
public static JenkinsServer getConnection() {
if (jenkinsServer == null) {
try {
jenkinsServer = new JenkinsServer(new URI(JENKINS_URL, JENKINS_USERNAME, JENKINS_PASSWORD), "PengHuanZhi", "11622ce96b51846c8658a6390253d87f7e");
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
return jenkinsServer;
}
}
①、View
Ⅰ、创建View
/**
* @description 创建View
* @author PengHuAnZhi
* @date 2021/10/8 17:20
*/
public void createView() {
try {
// 创建一个 xml 字符串,里面设置一个 view 描述信息
String xml = "<listView _class=\"hudson.model.ListView\">\n" +
"<description>用于测试的视图</description>\n" +
"</listView>";
JenkinsConnectFactory.getConnection().createView("test-view", xml);
} catch (IOException e) {
e.printStackTrace();
}
}
Ⅱ、获取View
/**
* @description 获取视图基本信息
* @author PengHuAnZhi
* @date 2021/10/8 17:20
*/
public void getView() {
try {
// 视图名
String viewName = "test-view";
// 获取视图基本信息
View view = JenkinsConnectFactory.getConnection().getView(viewName);
System.out.println(view.getName());
System.out.println(view.getUrl());
System.out.println(view.getDescription());
// 获取视图xml信息
String viewXml = JenkinsConnectFactory.getClientInstance().get("/view/" + viewName + "/api/xml");
System.out.println(viewXml);
} catch (IOException e) {
e.printStackTrace();
}
}
Ⅲ、获取View配置 XML 信息
/**
* @description 获取视图基本信息
* @author PengHuAnZhi
* @date 2021/10/8 17:20
*/
public void getViewConfig() {
try {
// 视图名
String viewName = "test-view";
// 获取视图配置xml信息
String viewConfigXml = JenkinsConnectFactory.getClientInstance().get("/view/" + viewName + "/config.xml");
System.out.println(viewConfigXml);
} catch (IOException e) {
e.printStackTrace();
}
}
Ⅳ、更新View
/**
* @description 更新视图信息
* @author PengHuAnZhi
* @date 2021/10/8 17:20
*/
public void updateView() {
try {
// 创建一个 xml 字符串,里面设置一个要修改的某些字段,具体xml可以到jenkins查看
// 例如,下面xml文件是从地址:https://Jenkins-IP/jenkins/view/test-view/config.xml 获取的
String xml = "<hudson.model.ListView>\n" +
"<name>test-view</name>\n" +
"<description>用于测试的视图1111</description>\n" +
"<filterExecutors>false</filterExecutors>\n" +
"<filterQueue>false</filterQueue>\n" +
"<properties class=\"hudson.model.View$PropertyList\"/>\n" +
"<jobNames>\n" +
"<comparator class=\"hudson.util.CaseInsensitiveComparator\"/>\n" +
"</jobNames>\n" +
"<jobFilters/>\n" +
"<columns>\n" +
"<hudson.views.StatusColumn/>\n" +
"<hudson.views.WeatherColumn/>\n" +
"<hudson.views.JobColumn/>\n" +
"<hudson.views.LastSuccessColumn/>\n" +
"<hudson.views.LastFailureColumn/>\n" +
"<hudson.views.LastDurationColumn/>\n" +
"<hudson.views.BuildButtonColumn/>\n" +
"<hudson.plugins.favorite.column.FavoriteColumn plugin=\"favorite@2.3.2\"/>\n" +
"</columns>\n" +
"<recurse>false</recurse>\n" +
"</hudson.model.ListView>";
JenkinsConnectFactory.getConnection().updateView("test-view", xml);
} catch (IOException e) {
e.printStackTrace();
}
}
Ⅴ、删除View
/**
* @description 删除视图
* @author PengHuAnZhi
* @date 2021/10/8 17:20
*/
public void deleteView() {
try {
String viewName = "test-view";
JenkinsConnectFactory.getClientInstance().post("/view/" + viewName + "/doDelete");
} catch (IOException e) {
e.printStackTrace();
}
}
②、Job
Ⅰ、创建Job
/**
* @description 创建Job
* @author PengHuAnZhi
* @date 2021/10/8 17:20
*/
public void createJob() {
try {
/**创建一个流水线任务,且设置一个简单的脚本**/
// 创建 Pipeline 脚本
String script = "node(){ \n" +
"echo 'hello world!' \n" +
"}";
// xml配置文件,且将脚本加入到配置中
String xml = "<flow-definition plugin=\"workflow-job@2.32\">\n" +
"<description>测试项目</description>\n" +
"<definition class=\"org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition\" plugin=\"workflow-cps@2.66\">\n" +
"<script>" + script + "</script>\n" +
"<sandbox>true</sandbox>\n" +
"</definition>\n" +
"</flow-definition>";
// 创建 Job
JenkinsConnectFactory.getConnection().createJob("test-job", xml);
} catch (IOException e) {
e.printStackTrace();
}
}
Ⅱ、更新Job
/**
* @description 更新Job 更改之前创建的无参数Job,更改其为参数Job
* @author PengHuAnZhi
* @date 2021/10/8 17:20
*/
public void updateJob() {
try {
/**
* 更改一个流水线任务,让一个无参数的任务变成带参数任务
*/
// 创建 Pipeline 脚本,用一个key变量
String script = "node(){ \n" +
"echo \"${key}\" \n" +
"}";
// xml配置文件,且将脚本加入到配置中
String xml = "<flow-definition plugin=\"workflow-job@2.32\">\n" +
"<actions/>\n" +
"<description>测试项目</description>\n" +
"<keepDependencies>false</keepDependencies>\n" +
"<properties>\n" +
"<hudson.model.ParametersDefinitionProperty>\n" +
"<parameterDefinitions>\n" +
"<hudson.model.StringParameterDefinition>\n" +
"<name>key</name>\n" +
"<description>用于测试的字符变量</description>\n" +
"<defaultValue>hello</defaultValue>\n" +
"<trim>false</trim>\n" +
"</hudson.model.StringParameterDefinition>\n" +
"</parameterDefinitions>\n" +
"</hudson.model.ParametersDefinitionProperty>\n" +
"</properties>\n" +
"<definition class=\"org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition\" plugin=\"workflow-cps@2.66\">\n" +
"<script>" + script + "</script>\n" +
"<sandbox>true</sandbox>\n" +
"</definition>\n" +
"<disabled>false</disabled>\n" +
"</flow-definition>";
// 创建 Job
JenkinsConnectFactory.getConnection().updateJob("test-job", xml);
} catch (IOException e) {
e.printStackTrace();
}
}
Ⅲ、获取Job基本信息
/**
* @description 获取 Job 基本信息
* @author PengHuAnZhi
* @date 2021/10/8 17:20
*/
public void getJob() {
try {
// 获取 Job 信息
JobWithDetails job = JenkinsConnectFactory.getConnection().getJob("test-job");
// 获取 Job 名称
System.out.println(job.getName());
// 获取 Job URL
System.out.println(job.getUrl());
// 获取 Job 下一个 build 编号
System.out.println(job.getNextBuildNumber());
// 获取 Job 显示的名称
System.out.println(job.getDisplayName());
// 输出 Job 描述信息
System.out.println(job.getDescription());
// 获取 Job 下游任务列表
System.out.println(job.getDownstreamProjects());
// 获取 Job 上游任务列表
System.out.println(job.getUpstreamProjects());
} catch (IOException e) {
e.printStackTrace();
}
}
Ⅳ、获取Maven Job信息
/**
* @description 获取 Maven Job 信息
* @author PengHuAnZhi
* @date 2021/10/8 17:20
*/
public void getMavenJob() {
try {
// 获取 Job 信息
MavenJobWithDetails job = JenkinsConnectFactory.getConnection().getMavenJob("HelloWorld");
// 获取 Job 名称
System.out.println(job.getName());
// 获取 Job URL
System.out.println(job.getUrl());
// 获取 Job 下一个 build 编号
System.out.println(job.getNextBuildNumber());
// 获取 Job 显示的名称
System.out.println(job.getDisplayName());
// 获取 Job 下游任务列表
System.out.println(job.getDownstreamProjects());
// 获取 Job 上游任务列表
System.out.println(job.getUpstreamProjects());
} catch (IOException e) {
e.printStackTrace();
}
}
\
Ⅴ、获取Job列表
/**
* @description 获取 Job 列表
* @author PengHuAnZhi
* @date 2021/10/8 17:20
*/
public void getJobList() {
try {
// 获取 Job 列表
Map<String, Job> jobs = JenkinsConnectFactory.getConnection().getJobs();
for (Job job : jobs.values()) {
System.out.println(job.getName());
}
} catch (IOException e) {
e.printStackTrace();
}
}
Ⅵ、根据View名称获取Job列表
/**
* @description 根据 View 名称获取 Job 列表
* @author PengHuAnZhi
* @date 2021/10/8 17:20
*/
public void getJobListByView() {
try {
// 获取 Job 列表
Map<String, Job> jobs = JenkinsConnectFactory.getConnection().getJobs("all");
for (Job job : jobs.values()) {
System.out.println(job.getName());
}
} catch (IOException e) {
e.printStackTrace();
}
}
Ⅶ、查看Job XML信息
/**
* @description 查看 Job 配置信息(XML)
* @author PengHuAnZhi
* @date 2021/10/8 17:20
*/
public void getJobConfig() {
try {
String xml = JenkinsConnectFactory.getConnection().getJobXml("test-job");
System.out.println(xml);
} catch (IOException e) {
e.printStackTrace();
}
}
Ⅷ、无参数 Job build
因为修改过本Job为有参Job,这里测试还需更改为无参job
/**
* @description 执行无参数 Job build
* @author PengHuAnZhi
* @date 2021/10/8 17:20
*/
public void buildJob() {
try {
JenkinsConnectFactory.getConnection().getJob("test-job").build();
} catch (IOException e) {
e.printStackTrace();
}
}
Ⅸ、有参构建
/**
* @description 执行带参数 Job build
* @author PengHuAnZhi
* @date 2021/10/8 17:20
*/
public void buildParamJob() {
try {
/**
* 例如,现有一个job,拥有一个字符参数"key"
* 现在对这个值进行设置,然后执行一个输出这个值的脚本
*/
// 设置参数值
Map<String, String> param = new HashMap<>();
param.put("key", "hello world!");
// 执行 build 任务
JenkinsConnectFactory.getConnection().getJob("test-job").build(param);
} catch (IOException e) {
e.printStackTrace();
}
}
Ⅹ、停止最后构建的 Job Build
/**
* @description 停止最后构建的 Job Build
* @author PengHuAnZhi
* @date 2021/10/8 17:20
*/
public void stopLastJobBuild() {
try {
// 获取最后的 build 信息
Build build = JenkinsConnectFactory.getConnection().getJob("HelloWorld").getLastBuild();
// 停止最后的 build
build.Stop();
} catch (IOException e) {
e.printStackTrace();
}
}
ⅩⅠ、禁用 Job
/**
* @description 禁用 Job
* @author PengHuAnZhi
* @date 2021/10/8 17:20
*/
public void disableJob() {
try {
JenkinsConnectFactory.getConnection().disableJob("test-job");
} catch (IOException e) {
e.printStackTrace();
}
}
ⅩⅡ、启用Job
/**
* 启用 Job
* @author PengHuAnZhi
* @date 2021/10/8 17:20
*/
public void enableJob() {
try {
JenkinsConnectFactory.getConnection().enableJob("test-job");
} catch (IOException e) {
e.printStackTrace();
}
}
ⅩⅢ、删除 Job
/**
* @description 删除 Job
* @author PengHuAnZhi
* @date 2021/10/8 17:20
*/
public void deleteJob() {
try {
JenkinsConnectFactory.getConnection().deleteJob("test-job");
} catch (IOException e) {
e.printStackTrace();
}
}
③、编译
Ⅰ、获取Build基本信息
/**
* @param build build对象
* @description 将Build内部信息打印
* @author PengHuAnZhi
* @date 2021/10/8 17:20
*/
private void printBuild(Build build) throws IOException {
System.out.println(build.getNumber());
System.out.println(build.getUrl());
System.out.println(build.getQueueId());
}
Ⅱ、获取 Job 最后的 Build
/**
* @description 获取 Job 最后的 Build
* @author PengHuAnZhi
* @date 2021/10/8 17:20
*/
public void getJobLastBuild() {
try {
// 获取 Job 信息
JobWithDetails job = JenkinsConnectFactory.getConnection().getJob("test-job");
// 获得最后编译信息
Build lastBuild = job.getLastBuild();
printBuild(lastBuild);
// 获取最后成功的编译信息
Build lastSuccessfulBuild = job.getLastSuccessfulBuild();
printBuild(lastSuccessfulBuild);
// 获取最后事变的编译信息
Build lastFailedBuild = job.getLastFailedBuild();
printBuild(lastFailedBuild);
// 获取最后完成的编译信息
Build lastCompletedBuild = job.getLastCompletedBuild();
printBuild(lastCompletedBuild);
// 获取最后稳定的编译信息
Build lastStableBuild = job.getLastStableBuild();
printBuild(lastStableBuild);
// 获取最后不稳定的编译信息
Build lastUnstableBuild = job.getLastUnstableBuild();
printBuild(lastUnstableBuild);
// 获取最后未成功的编译信息
Build lastUnsuccessfulBuild = job.getLastUnsuccessfulBuild();
printBuild(lastUnsuccessfulBuild);
} catch (IOException e) {
e.printStackTrace();
}
}
Ⅲ、获取 Job 首次 Build
/**
* @description 获取 Job 首次 Build
* @author PengHuAnZhi
* @date 2021/10/8 17:20
*/
public void getJobFirstBuild() {
try {
// 获取 Job 信息
JobWithDetails job = JenkinsConnectFactory.getConnection().getJob("test-job");
// 获得首次编译信息
Build firstBuild = job.getFirstBuild();
printBuild(firstBuild);
} catch (IOException e) {
e.printStackTrace();
}
}
Ⅳ、根据 Job Build 编号获取编译信息
/**
* @description 根据 Job Build 编号获取编译信息
* @author PengHuAnZhi
* @date 2021/10/8 17:20
*/
public void getJobByNumber() {
try {
// 获取 Job 信息
JobWithDetails job = JenkinsConnectFactory.getConnection().getJob("test-job");
// 根据
Build numberBuild = job.getBuildByNumber(1);
printBuild(numberBuild);
} catch (IOException e) {
e.printStackTrace();
}
}
Ⅴ、获取全部 Job Build列表
/**
* @description 获取全部 Job Build列表
* @author PengHuAnZhi
* @date 2021/10/8 17:20
*/
public void getJobBuildListAll() {
try {
// 获取 Job 信息
JobWithDetails job = JenkinsConnectFactory.getConnection().getJob("test-job");
// 获取全部 Build 信息
List<Build> builds = job.getAllBuilds();
for (Build build : builds) {
System.out.println(build.getNumber());
}
} catch (IOException e) {
e.printStackTrace();
}
}
Ⅵ、获取 Job 一定范围的 Build 列表
/**
* @description 获取 Job 一定范围的 Build 列表
* @author PengHuAnZhi
* @date 2021/10/8 17:20
*/
public void getJobBuildListRange() {
try {
// 获取 Job 信息
JobWithDetails job = JenkinsConnectFactory.getConnection().getJob("test-job");
// 设定范围
Range range = Range.build().from(0).to(2);
System.err.println(range.getRangeString());
// 获取一定范围的 Build 信息
List<Build> builds = job.getAllBuilds(range);
for (Build build : builds) {
System.out.println(build.getNumber());
}
} catch (IOException e) {
e.printStackTrace();
}
}
Ⅶ、获取Build详细信息
/**
* @description 获取 Build 详细信息
* @author PengHuAnZhi
* @date 2021/10/8 17:20
*/
public void getJobBuildDetailInfo() {
try {
// 获取 Job 信息
JobWithDetails job = JenkinsConnectFactory.getConnection().getJob("test-job");
// 这里用最后一次编译来示例
BuildWithDetails build = job.getLastBuild().details();
// 获取构建的显示名称
System.out.println(build.getDisplayName());
// 获取构建的参数信息
System.out.println(build.getParameters());
// 获取构建编号
System.out.println(build.getNumber());
// 获取构建结果,如果构建未完成则会显示为null
System.out.println(build.getResult());
// 获取执行构建的活动信息
System.out.println(build.getActions());
// 获取构建持续多少时间(ms)
System.out.println(build.getDuration());
// 获取构建开始时间戳
System.out.println(build.getTimestamp());
// 获取构建头信息,里面包含构建的用户,上游信息,时间戳等
List<BuildCause> buildCauses = build.getCauses();
for (BuildCause bc : buildCauses) {
System.out.println(bc.getUserId());
System.out.println(bc.getShortDescription());
System.out.println(bc.getUpstreamBuild());
System.out.println(bc.getUpstreamProject());
System.out.println(bc.getUpstreamUrl());
System.out.println(bc.getUserName());
}
} catch (IOException e) {
e.printStackTrace();
}
}
Ⅷ、获取 Build Log 日志信息
/**
* @description 获取 Build Log 日志信息
* @author PengHuAnZhi
* @date 2021/10/8 17:20
*/
public void getJobBuildLog() {
try {
// 获取 Job 信息
JobWithDetails job = JenkinsConnectFactory.getConnection().getJob("test-job");
// 这里用最后一次编译来示例
BuildWithDetails build = job.getLastBuild().details();
// 获取构建的日志,如果正在执行构建,则会只获取已经执行的过程日志
// Text格式日志
System.out.println(build.getConsoleOutputText());
// Html格式日志
System.out.println(build.getConsoleOutputHtml());
// 获取部分日志,一般用于正在执行构建的任务
ConsoleLog consoleLog = build.getConsoleOutputText(0);
// 获取当前日志大小
System.out.println(consoleLog.getCurrentBufferSize());
// 是否已经构建完成,还有更多日志信息
System.out.println(consoleLog.getHasMoreData());
// 获取当前截取的日志信息
System.out.println(consoleLog.getConsoleLog());
} catch (IOException e) {
e.printStackTrace();
}
}
Ⅸ、获取正在执行构建任务的日志信息
/**
* @description 获取正在执行构建任务的日志信息
* @author PengHuAnZhi
* @date 2021/10/8 17:20
*/
public void getBuildActiveLog() {
try {
// 这里用最后一次编译来示例
BuildWithDetails build = JenkinsConnectFactory.getConnection().getJob("test-job").getLastBuild().details();
// 当前日志
ConsoleLog currentLog = build.getConsoleOutputText(0);
// 输出当前获取日志信息
System.out.println(currentLog.getConsoleLog());
// 检测是否还有更多日志,如果是则继续循环获取
while (currentLog.getHasMoreData()) {
// 获取最新日志信息
ConsoleLog newLog = build.getConsoleOutputText(currentLog.getCurrentBufferSize());
// 输出最新日志
System.out.println(newLog.getConsoleLog());
currentLog = newLog;
// 睡眠1s
Thread.sleep(1000);
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
\
④、其他
Ⅰ、获取主机信息
/**
* @description 获取主机信息
* @author PengHuAnZhi
* @date 2021/10/8 17:20
*/
public void getComputerInfo() {
try {
Map<String, Computer> map = JenkinsConnectFactory.getConnection().getComputers();
for (Computer computer : map.values()) {
// 获取当前节点-节点名称
System.out.println(computer.details().getDisplayName());
// 获取当前节点-执行者数量
System.out.println(computer.details().getNumExecutors());
// 获取当前节点-执行者详细信息
List<Executor> executorList = computer.details().getExecutors();
// 查看当前节点-是否脱机
System.out.println(computer.details().getOffline());
// 获得节点的全部统计信息
LoadStatistics loadStatistics = computer.details().getLoadStatistics();
// 获取节点的-监控数据
Map<String, Map> monitorData = computer.details().getMonitorData();
//......
}
} catch (IOException e) {
e.printStackTrace();
}
}
Ⅱ、重启Jenkins
/**
* @description 重启 Jenkins
* @author PengHuAnZhi
* @date 2021/10/8 17:20
*/
public void restart() {
try {
JenkinsConnectFactory.getConnection().restart(true);
} catch (IOException e) {
e.printStackTrace();
}
}
Ⅲ、安全重启Jenkins
/**
* @description 安全重启 Jenkins
* @author PengHuAnZhi
* @date 2021/10/8 17:20
*/
public void safeRestart() {
try {
JenkinsConnectFactory.getConnection().safeRestart(true);
} catch (IOException e) {
e.printStackTrace();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
Ⅳ、安全结束Jenkins
/**
* @description 安全结束 Jenkins
* @author PengHuAnZhi
* @date 2021/10/8 17:20
*/
public void safeExit() {
try {
JenkinsConnectFactory.getConnection().safeExit(true);
} catch (IOException e) {
e.printStackTrace();
}
}
Ⅴ、关闭连接
/**
* @description 关闭 Jenkins 连接
* @author PengHuAnZhi
* @date 2021/10/8 17:20
*/
public void close() {
JenkinsConnectFactory.getConnection().close();
}
Ⅵ、根据 Label 查找代理节点信息
/**
* @description 根据 Label 查找代理节点信息
* @author PengHuAnZhi
* @date 2021/10/8 17:20
*/
public void getLabelNodeInfo() {
try {
LabelWithDetails labelWithDetails = JenkinsConnectFactory.getConnection().getLabel("jnlp-agent");
// 获取标签名称
System.out.println(labelWithDetails.getName());
// 获取 Cloud 信息
System.out.println(labelWithDetails.getClouds());
// 获取节点信息
System.out.println(labelWithDetails.getNodeName());
// 获取关联的 Job
System.out.println(labelWithDetails.getTiedJobs());
// 获取参数列表
System.out.println(labelWithDetails.getPropertiesList());
// 是否脱机
System.out.println(labelWithDetails.getOffline());
// 获取描述信息
System.out.println(labelWithDetails.getDescription());
} catch (IOException e) {
e.printStackTrace();
}
}
Ⅶ、判断Jenkins是否运行
/**
* @description 判断 Jenkins 是否运行
* @author PengHuAnZhi
* @date 2021/10/8 17:20
*/
public void isRunning() {
boolean isRunning = JenkinsConnectFactory.getConnection().isRunning();
System.out.println(isRunning);
}
Ⅷ、获取 Jenkins 插件信息
/**
* @description 获取 Jenkins 插件信息
* @author PengHuAnZhi
* @date 2021/10/8 17:20
*/
public void getPluginInfo() {
try {
PluginManager pluginManager = JenkinsConnectFactory.getConnection().getPluginManager();
// 获取插件列表
List<Plugin> plugins = pluginManager.getPlugins();
for (Plugin plugin : plugins) {
// 插件 wiki URL 地址
System.out.println(plugin.getUrl());
// 版本号
System.out.println(plugin.getVersion());
// 简称
System.out.println(plugin.getShortName());
// 完整名称
System.out.println(plugin.getLongName());
// 是否支持动态加载
System.out.println(plugin.getSupportsDynamicLoad());
// 插件依赖的组件
System.out.println(plugin.getDependencies());
}
} catch (IOException e) {
e.printStackTrace();
}
}
4、问题处理
①、HttpResponseException: Forbidden
这种跨域问题有两种处理办法,第一种就是关闭Jenkins的跨域保护
Ⅰ、关闭跨域保护(高版本)
笔者Jenkins服务放在了一台CentOS8虚拟机中
vim /etc/sysconfig/jenkins
1
找到JENKINS_JAVA_OPTIONS配置
在后面追加如下内容
-Dhudson.security.csrf.GlobalCrumbIssuerConfiguration.DISABLE_CSRF_PROTECTION=true
1
保存退出重启Jenkins服务
systemctl restart jenkins.service
1
查看配置生效
Ⅱ、携带Token建立连接
进入用户管理页面,新增一个Token
在创建JenkinsHttpClient和JenkinsServer的时候添加该Token,如下
jenkinsHttpClient = new JenkinsHttpClient(new URI(JENKINS_URL, JENKINS_USERNAME, JENKINS_PASSWORD), "PengHuanZhi", "11622ce96b51846c8658a6390253d87f7e");
jenkinsServer = new JenkinsServer(new URI(JENKINS_URL, JENKINS_USERNAME, JENKINS_PASSWORD), "PengHuanZhi", "11622ce96b51846c8658a6390253d87f7e");
准备工作
/**
* @author PengHuAnZhi
* @ProjectName Jenkins API Demo
* @Description Jenkins连接工具类
* @time 2021/10/8 14:33
*/
public class JenkinsConnectFactory {
private static final String JENKINS_URL = "http://192.168.225.130";
private static final String JENKINS_USERNAME = "PengHuanZhi";
static final String JENKINS_PASSWORD = "123456789";
private static JenkinsHttpClient jenkinsHttpClient = null;
private static JenkinsServer jenkinsServer = null;
public static JenkinsHttpClient getClientInstance() {
if (jenkinsHttpClient == null) {
try {
jenkinsHttpClient = new JenkinsHttpClient(new URI(JENKINS_URL, JENKINS_USERNAME, JENKINS_PASSWORD), JENKINS_USERNAME, "11622ce96b51846c8658a6390253d87f7e");
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
return jenkinsHttpClient;
}
public static JenkinsServer getConnection() {
if (jenkinsServer == null) {
try {
jenkinsServer = new JenkinsServer(new URI(JENKINS_URL, JENKINS_USERNAME, JENKINS_PASSWORD), JENKINS_USERNAME, "11622ce96b51846c8658a6390253d87f7e");
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
return jenkinsServer;
}
}
private final JenkinsServer server = JenkinsConnectFactory.getConnection();
private final JenkinsHttpClient client = JenkinsConnectFactory.getClientInstance();
获取非Maven项目构建后生成的Artifact文件
/**
* @param jobName 任务名称
* @param buildNumber 构建号
* @description 根据任务名称获取其某次构建产生的文件
* @author PengHuAnZhi
* @date 2021/10/13 10:27
*/
public void getJobBuildArtifact(String jobName, Integer buildNumber) {
try {
JobWithDetails job = server.getJob(jobName);
List<Build> builds = job.getBuilds().stream().filter(build -> build.getNumber() == buildNumber).collect(Collectors.toList());
Build build;
if (builds.size() != 1) {
throw new Exception("获取构建出错");
} else {
build = builds.get(0);
}
List<Artifact> artifacts = build.details().getArtifacts();
Artifact artifact = artifacts.get(0);
InputStream inputStream = build.details().downloadArtifact(artifact);
String fileName = artifact.getFileName();
File file = new File("C:\\Users\\Administrator\\Desktop\\" + fileName);
//当前模拟的是一个txt文件,所以直接用高级字符流输出到文件了,对于文件类型不确定一般使用字节流好点吧我觉得
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
String str = reader.readLine();
while (str != null) {
writer.write(str);
str = reader.readLine();
}
inputStream.close();
reader.close();
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
获取Maven项目构建后自动归集至Module Builds列表中的Artifact文件
/**
* @param jobName 任务名称
* @param buildNumber 构建号
* @description Maven和普通项目不同,构建产生的文件输出位置为当前module列表下
* @author PengHuAnZhi
* @date 2021/10/13 15:11
*/
public void getMavenJobBuildArtifact(String jobName, Integer buildNumber) {
try {
MavenJobWithDetails mavenJob = server.getMavenJob(jobName);
MavenBuild mavenBuild;
List<MavenBuild> builds = mavenJob.getBuilds().stream().filter(build -> build.getNumber() == buildNumber).collect(Collectors.toList());
if (builds.size() != 1) {
throw new Exception("获取构建出错");
} else {
mavenBuild = builds.get(0);
}
MavenModule mavenModule = mavenBuild.getMavenModule();
List<MavenModuleRecord> moduleRecords = mavenModule.getModuleRecords();
for (MavenModuleRecord record : moduleRecords) {
MavenArtifact mainArtifact = record.getMainArtifact();
//URL拼接
String urlPath = record.getUrl().replace("mavenArtifacts", "artifact") + mainArtifact.getGroupId() + "/" + mainArtifact.getArtifactId() + "/" + mainArtifact.getVersion() + "/" + mainArtifact.getCanonicalName();
URI uri = new URI(urlPath);
InputStream inputStream = client.getFile(uri);
File file = new File("C:\\Users\\Administrator\\Desktop\\" + mainArtifact.getCanonicalName());
byte[] buffer = new byte[inputStream.available()];
FileOutputStream outputStream = new FileOutputStream(file);
int ch;
while ((ch = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, ch);
}
inputStream.close();
outputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
更多推荐
所有评论(0)