jenkins的api操作
Posted beketz
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jenkins的api操作相关的知识,希望对你有一定的参考价值。
jenkins-client 实现方式
jenkins-client 底层是使用 HttpClient HttpPost 发送post 请求实现的,是将 REST API 封装了一下
添加依赖
<!-- jenkins --> <dependency> <groupId>com.offbytwo.jenkins</groupId> <artifactId>jenkins-client</artifactId> <version>0.3.7</version> </dependency>
连接服务器
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; }
new JenkinsServer() 底层 使用了HttpClient 底层代码如下
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<String, Job> jobs = jenkins.getJobs();
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<String, Long> getStartTImeAndEndTime(String jobName, int number) throws IOException { JenkinsServer jenkins = getInstance(); Map<String, Job> jobs = jenkins.getJobs(); JobWithDetails job = jobs.get(jobName).details(); Build buildByNumber = job.getBuildByNumber(number); long startTime = buildByNumber.details().getTimestamp(); long duration = buildByNumber.details().getDuration(); Map<String, Long> data = new HashMap<>(); data.put("startTime", startTime); data.put("duration", duration); return data; }
获取最后一次构建是否成功
number: 构建号 jobName: job 名称 通过获取最后的成功,失败的构建号,来判断是否相等
public static boolean isSuccess(String jobName, int number) throws IOException { JenkinsServer jenkins = getInstance(); Map<String, Job> jobs = jenkins.getJobs(); JobWithDetails job = jobs.get(jobName).details(); int LastSuccessfulNumber = job.getLastSuccessfulBuild().getNumber(); int LastUnsuccessfulNumber = job.getLastUnsuccessfulBuild().getNumber(); boolean flag = false; if (LastSuccessfulNumber == number) { flag = true; } if (LastUnsuccessfulNumber == number) { flag = false; } return flag; }
通过获取构建的最终的结果来判断最终的结果
返回结果:SUCCESS, FAILURE
public static String isSuccess(String jobName, int number) throws IOException { JenkinsServer jenkins = getInstance(); Map<String, Job> jobs = jenkins.getJobs(); JobWithDetails job = jobs.get(jobName).details(); Build buildByNumber = job.getBuildByNumber(number); BuildWithDetails details = buildByNumber.details(); return details.getResult().toString(); }
判断job是否执行完
public static boolean isFinished(int number, String jobName) { boolean isBuilding = false; if (number <= 0) { throw new IllegalArgumentException("jodId must greater than 0!"); } try { JenkinsServer jenkins = getInstance(); Map<String, Job> jobs = jenkins.getJobs(); JobWithDetails job = jobs.get(jobName).details(); Build buildByNumber = job.getBuildByNumber(number); if (null != buildByNumber) { BuildWithDetails details = buildByNumber.details(); if (null != details) { isBuilding = details.isBuilding(); } else { isBuilding = true; } } else { isBuilding = true; } return !isBuilding; } catch (Exception e) { e.printStackTrace(); } finally { } return false; }
通过上面的调用Jenkins-Client 相关的函数,可以获取相关的Jenkins的操作,如 日志,构建时间,job构建时长,构建号,是否成功,是否正在构建等等, 也发现一些问题 如
Jenkins中配置了view,并且view下面还有view,还有view,Jenkins-client 只能获取两层view,第三层的view获取不到
如果Jenkins中使用了build with parameters 插件 在使用 check box 选择多个参数的话,通过Jenkins-client 传参是 无法实现的
以上是关于jenkins的api操作的主要内容,如果未能解决你的问题,请参考以下文章
Jenkins+Python调用阿里云API实现发布自动修改SLB权重
python 判断文件是否存在和删除文件的api (其中判断文件在不在让想起这个可以强兼容jenkins工作目录那个问题)