詹金斯 - 中止运行生成,如果新的启动 [英] Jenkins - abort running build if new one is started

查看:226
本文介绍了詹金斯 - 中止运行生成,如果新的启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Jenkins和Multibranch Pipeline。我为每个活跃的git分支都有一份工作。
通过push git仓库触发新的构建。我想要的是在当前分支中中止正在运行的构建,如果新分支出现在同一分支中。



例如:我提交并推送到 feature1 。然后在Jenkins开始 BUILD_1 >。我做了另一个提交并推送到分支 feature1 ,同时 BUILD_1 仍在运行。我希望 BUILD_1 被中止并启动 BUILD_2

我尝试使用 stage concurrency = x 选项, stage-lock-milestone 功能,但并未解决我的问题。



我也读过这个主题停止Jenkins的工作,以防万一开始,但没有解决我的问题。



你知道这个解决方案吗?使用为您的项目启用作业并行运行(必要时执行并发构建)
$ b

使用执行系统groovy脚本作为第一个构建步骤:

import hudson.model.Result
import jenkins.model.CauseOfInterruption

// iterate通过当前项目运行
build.getProject()._ getRu纳秒()各{ID,游程>。
def exec = run.getExecutor()
//如果运行不是当前版本,并且执行器(正在运行),则停止它
if(run!= build&& (){
//准备中断原因
def cause = new CauseOfInterruption(){
public String getShortDescription(){
returnbuild by build#$ {build.getId()}
}
}
exec.interrupt(Result.ABORTED,cause)
}
}

并且在被打断的工作中会有一个日志:

 生成被中止
被生成#12打断
完成:ABORTED


I use Jenkins and Multibranch Pipeline. I have a job for each active git branch. New build is triggered by push in git repository. What I want is to abort running builds in current branch if new one appears in same branch.

For example: I commit and push to branch feature1. Then BUILD_1 started in Jenkins. I make another commit and push to branch feature1 while BUILD_1 is still running. I want BUILD_1 to be aborted and to start BUILD_2.

I tried to use stage concurrency=x option, and stage-lock-milestone feature, but didn't manage to solve my problem.

Also I've read this thread Stopping Jenkins job in case newer one is started, but there is no solution for my problem.

Do you know any solution to this?

解决方案

enable job parallel run for your project with Execute concurrent builds if necessary

use execute system groovy script as a first build step:

import hudson.model.Result
import jenkins.model.CauseOfInterruption

//iterate through current project runs
build.getProject()._getRuns().each{id,run->
  def exec = run.getExecutor()
  //if the run is not a current build and it has executor (running) then stop it
  if( run!=build && exec!=null ){
    //prepare the cause of interruption
    def cause = new CauseOfInterruption(){ 
      public String getShortDescription(){
        return "interrupted by build #${build.getId()}"
      }
    }
    exec.interrupt(Result.ABORTED, cause)
  }
}

and in the interrupted job there will be a log:

Build was aborted
interrupted by build #12
Finished: ABORTED 

这篇关于詹金斯 - 中止运行生成,如果新的启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆