Stopping Jenkins job in case newer one is started

巧了我就是萌 提交于 2019-12-19 00:56:08

问题


Is it possible to specify that, in case on job (A) is triggered more than once, that previous jobs are removed from queue, and only latest one is left in queue or started if there are enough free slots?

Thanks in advance!


回答1:


You can do it with a system Groovy Script run via Groovy Script Plugin. Such a script has direct access to Jenkins via its programming API. I do not see any other way.




回答2:


use execute system groovy script 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)
  }
}

//just for test do something long...
Thread.sleep(10000)

and in the interrupted job there will be a log:

Build was aborted
interrupted by build #12
Finished: ABORTED 



回答3:


I don't think this problem can be solved perfectly by only adding a groovy script before build. If the new job come in while the older has already been building(it can't check the queue, since its pre-build part has passed), the new one still has to wait.

As I see, to achieve a preemptive job of jenkins, we need create another oversee job. This job should check that if there are new builds in the queue regularly and terminate older one if necessary.

But create a oversee job seems complex. I also expect a perfect solution.



来源:https://stackoverflow.com/questions/10280846/stopping-jenkins-job-in-case-newer-one-is-started

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!