停止Jenkins工作,以防更新的工作

Dej*_*ges 8 jenkins jenkins-plugins

如果在作业(A)被多次触发的情况下,是否可以指定先前的作业从队列中删除,并且只有最新的作业留在队列中或者如果有足够的空闲插槽则启动?

提前致谢!

dag*_*ett 5

使用execute system groovy script步骤:

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)
Run Code Online (Sandbox Code Playgroud)

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

Build was aborted
interrupted by build #12
Finished: ABORTED 
Run Code Online (Sandbox Code Playgroud)


mal*_*cot 3

您可以使用通过Groovy Script Plugin运行的系统 Groovy 脚本来完成此操作。这样的脚本可以通过其编程 API直接访问 Jenkins 。我没有看到任何其他方式。

  • `http://[jenkins_server]/script` 有一个脚本控制台,它对于试验和调试 Groovy 脚本非常有用。 (2认同)
  • @DejanMenges 你设法解决这个问题了吗?你能展示一下你的脚本吗? (2认同)