Jenkins - abort running build if new one is started

前端 未结 9 2507
失恋的感觉
失恋的感觉 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条回答
  •  Happy的楠姐
    2020-12-04 16:03

    Got it to work by having the following script in the Global Shared Library :

    import hudson.model.Result
    import jenkins.model.CauseOfInterruption.UserInterruption
    
    def killOldBuilds() {
      while(currentBuild.rawBuild.getPreviousBuildInProgress() != null) {
        currentBuild.rawBuild.getPreviousBuildInProgress().doKill()
      }
    }
    

    And calling it in my pipeline :

    @Library('librayName')
    def pipeline = new killOldBuilds()
    [...] 
    stage 'purge'
    pipeline.killOldBuilds()
    

    Edit : Depending on how strongly you want to kill the oldBuild, you can use doStop(), doTerm() or doKill() !

提交回复
热议问题