1.引入jenkins工具包
代码语言:java
复制 <dependency>
<groupId>com.offbytwo.jenkins</groupId>
<artifactId>jenkins-client</artifactId>
<version>0.3.8</version>
</dependency>
2.终止正在构建的任务
代码语言:java
复制 JenkinsHttpClient jenkinsHttpClient = new JenkinsHttpClient(new URI(url), username, password);
JenkinsServer jenkinsServer = new JenkinsServer(jenkinsHttpClient);
Build build = jenkinsServer.getJob(jobName).getBuildByNumber(buildNumber);
build.Stop();
3.终止构建等待队列的任务
代码语言:java
复制 JenkinsHttpClient jenkinsHttpClient = new JenkinsHttpClient(new URI(url), username, password);
Queue queue = jenkinsHttpClient.get("queue/?depth=1", Queue.class);
queue.getItems().forEach(item -> {
try {
jenkinsHttpClient.post("queue/cancelItem?id=" item.getId());
} catch (IOException e) {
throw new RuntimeException(e);
}
});