Jenkins - abort running build if new one is started

前端 未结 9 2501
失恋的感觉
失恋的感觉 2020-12-04 15:51

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

9条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-04 16:19

    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().iterator().each{ 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 = { "interrupted by build #${build.getId()}" as String } as CauseOfInterruption 
        exec.interrupt(Result.ABORTED, cause)
      }
    }
    

    and in the interrupted job(s) there will be a log:

    Build was aborted
    interrupted by build #12
    Finished: ABORTED 
    

提交回复
热议问题