Stopping Jenkins job in case newer one is started

后端 未结 3 1701
难免孤独
难免孤独 2021-01-04 21:12

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

3条回答
  •  半阙折子戏
    2021-01-04 21:44

    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 
    

提交回复
热议问题