jenkins的api操作
2021-04-13 20:28
标签:lag tar string ash blog amp == art basic jenkins-client 底层是使用 HttpClient HttpPost 发送post 请求实现的,是将 REST API 封装了一下 new JenkinsServer() 底层 使用了HttpClient 底层代码如下 number: 构建号 jobName: job 名称 通过获取最后的成功,失败的构建号,来判断是否相等 通过获取构建的最终的结果来判断最终的结果 通过上面的调用Jenkins-Client 相关的函数,可以获取相关的Jenkins的操作,如 日志,构建时间,job构建时长,构建号,是否成功,是否正在构建等等, 也发现一些问题 如 如果Jenkins中使用了build with parameters 插件 在使用 check box 选择多个参数的话,通过Jenkins-client 传参是 无法实现的 jenkins的api操作 标签:lag tar string ash blog amp == art basic 原文地址:https://www.cnblogs.com/clovejava/p/8971354.htmljenkins-client 实现方式
添加依赖
连接服务器
public synchronized static JenkinsServer getInstance() {
if (jenkinsServer == null) {
try {
jenkinsServer = new JenkinsServer(new URI(EnvParam.getInstance().getJenkinsURL()), EnvParam.getInstance().getJenkinsUserId(),
EnvParam.getInstance().getJenkinsToken());
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
return jenkinsServer;
}
BasicCredentialsProvider provider = new BasicCredentialsProvider();
AuthScope scope = new AuthScope(uri.getHost(), uri.getPort(), "realm");
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username,
password);
provider.setCredentials(scope, credentials);
builder.setDefaultCredentialsProvider(provider);
builder.addInterceptorFirst(new PreemptiveAuth());
if (StringUtils.isNotBlank(username)) {
this.localContext = new BasicHttpContext();
this.localContext.setAttribute("preemptive-auth", new BasicScheme());
}
获取所有的job信息
JenkinsServer jenkins = getInstance();
Map
jenkins.getJobs(); 返回的是以jobName为key的map对象
获取某次构建的日志信息
public static String getJobLog(int buildNumber, String jobName) throws IOException {
JenkinsServer jenkins = getInstance();
JobWithDetails job = jenkins.getJob(jobName);
JobWithDetails details = job.details();
Build buildByNumber = details.getBuildByNumber(buildNumber);
BuildWithDetails details2 = buildByNumber.details();
String outputText = details2.getConsoleOutputText();
return outputText;
}
获取某次构建的开始时间和持续时间
public static Map
获取最后一次构建是否成功
public static boolean isSuccess(String jobName, int number) throws IOException {
JenkinsServer jenkins = getInstance();
Map
返回结果:SUCCESS, FAILUREpublic static String isSuccess(String jobName, int number) throws IOException {
JenkinsServer jenkins = getInstance();
Map
判断job是否执行完
public static boolean isFinished(int number, String jobName) {
boolean isBuilding = false;
if (number ) {
throw new IllegalArgumentException("jodId must greater than 0!");
}
try {
JenkinsServer jenkins = getInstance();
Map
Jenkins中配置了view,并且view下面还有view,还有view,Jenkins-client 只能获取两层view,第三层的view获取不到