我们首先需要知道使用哪个依赖,这在编程中比较重要!
<dependency>
<groupId>com.offbytwo.jenkins</groupId>
<artifactId>jenkins-client</artifactId>
<version>0.4.0-HACKED</version>
<exclusions>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
</exclusions>
</dependency>
这是我在项目组所用到的依赖,但可能会发现在远程仓库中只有0.3.8的版本,而没有0.4.0的。这个包就是在下面的地址中,利用 maven进行install到本地仓库。
https://github.com/WildSaCk/java-client-api
接下来就是如何使用这个包:
Controller:
@ApiOperation("获取变更记录")
@RequestMapping("showUpdateRecording")
public List<BuildChangeSetItem> showUpdateRecording(@RequestBody String historyId) {
return devopsJenkinsJobHistoryService.showUpdateRecording(historyId);
}
Service:
import com.offbytwo.jenkins.model.*;
……
public List<BuildChangeSetItem> showUpdateRecording(String historyId) {
DevopsJenkinsJobHistory history = devopsJenkinsJobHistoryMapper.selectByPrimaryKey(historyId);
try {
List<BuildChangeSetItem> list = new ArrayList<>();
JenkinsServer jenkinsServer = devopsJenkinsService.getJenkinsServer();//此处为Jenkins服务连接
JobWithDetails job = jenkinsServer.getJob(history.getJobName());
for (Build tmpBuild : job.getAllBuilds()) {
if (history.getOrderNum().equals(tmpBuild.getNumber())) {
List<BuildChangeSet> changeSets = tmpBuild.details().getChangeSets();
for (BuildChangeSet buildChangeSet : changeSets) {
list = buildChangeSet.getItems();
}
break;
}
}
return list;
} catch (Exception e) {
e.printStackTrace();
LogUtil.error("查询变更记录失败!" + e.getMessage());
}
return null;
}