jenkins 如何通过api的方式停止构建任务

2022-11-07 14:14:44 浏览数 (1)

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);
            }
        });

0 人点赞