Jenkins - abort running build if new one is started

前端 未结 9 2519
失恋的感觉
失恋的感觉 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-04 16:06

    If anybody needs it in Jenkins Pipeline Multibranch, it can be done in Jenkinsfile like this:

    def abortPreviousRunningBuilds() {
      def hi = Hudson.instance
      def pname = env.JOB_NAME.split('/')[0]
    
      hi.getItem(pname).getItem(env.JOB_BASE_NAME).getBuilds().each{ build ->
        def exec = build.getExecutor()
    
        if (build.number != currentBuild.number && exec != null) {
          exec.interrupt(
            Result.ABORTED,
            new CauseOfInterruption.UserInterruption(
              "Aborted by #${currentBuild.number}"
            )
          )
          println("Aborted previous running build #${build.number}")
        } else {
          println("Build is not running or is current build, not aborting - #${build.number}")
        }
      }
    }
    

提交回复
热议问题