Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins - abort running build if new one is started

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 current branch if new one appears in same branch.

For example: I commit and push to branch feature1. Then BUILD_1 started in Jenkins. I make another commit and push to branch feature1 while BUILD_1 is still running. I want BUILD_1 to be aborted and to start BUILD_2.

I tried to use stage concurrency=x option, and stage-lock-milestone feature, but didn't manage to solve my problem.

Also I've read this thread Stopping Jenkins job in case newer one is started, but there is no solution for my problem.

Do you know any solution to this?

like image 286
kakty3 Avatar asked Nov 23 '16 09:11

kakty3


People also ask

How can I abort a running pipeline build if a new one is started?

While build #1 is running, if build #2 is started, it will create milestone1 and milestone2 in the project. Build #2 will immediately pass milestone1 and will therefore cause Build #1 to abort.

How do you abort a build in Jenkins?

BUILD ID URL/stop - aborts a Pipeline. BUILD ID URL/term - forcibly terminates a build (should only be used if stop does not work). BUILD ID URL/kill - hard kill a pipeline. This is the most destructive way to stop a pipeline and should only be used as a last resort.

How do you stop a running job in Jenkins?

Abort the job by clicking the red X next to the build progress bar. Click on "Pause/resume" on the build to pause. Click on "Pause/resume" again to resume the build.


2 Answers

With Jenkins script security many of the solutions here become difficult since they are using non-whitelisted methods.

With these milestone steps at the start of the Jenkinsfile, this is working for me:

def buildNumber = env.BUILD_NUMBER as int if (buildNumber > 1) milestone(buildNumber - 1) milestone(buildNumber) 

The result here would be:

  • Build 1 runs and creates milestone 1
  • While build 1 is running, build 2 fires. It has milestone 1 and milestone 2. It passes milestone 1, which causes build #1 to abort.
like image 109
Brandon Squizzato Avatar answered Sep 20 '22 02:09

Brandon Squizzato


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  
like image 32
daggett Avatar answered Sep 21 '22 02:09

daggett