Jenkins - abort running build if new one is started

前端 未结 9 2520
失恋的感觉
失恋的感觉 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:08

    Is there a way to abort the build just from one given branch and allow other builds from a different branch to run at the same job. For example, let's say I have Branch1 and Branch2 running in a job. I want to be able to abort all currently executing build from Branch1 except the latest one, at the same time Branch2 starts executing a build and I want Branch2 to be able to execute their build since it's a different branch. Is it possible?

    To clarify; We have many branches so we cannot just prevent concurrent builds, cancel the last as soon as the next starts, etc. Whatever method is used must specifically check if the branch already has a job running for it, not if the job, in general, is already running. Running the code is execute system groovy in Jenkins.

    import hudson.model.Result
    import jenkins.model.CauseOfInterruption
    import groovy.transform.Field
    import jenkins.model.*
    
    build.getProject()._getRuns().iterator().each{ run ->
    def exec = run.getExecutor()
    
     //from each run get the branch_name and put it into the list, add it to the list
     def branchName = build.environment.get("GIT_BRANCH")
     def list = []
     list.add(branchName)
    
      for (i = 0; i 

提交回复
热议问题