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?
If anybody needs it in Jenkins Pipeline Multibranch, it can be done in Jenkinsfile like this:
Got it to work by having the following script in the Global Shared Library :
And calling it in my pipeline :
Edit : Depending on how strongly you want to kill the oldBuild, you can use doStop(), doTerm() or doKill() !
enable job parallel run for your project with
Execute concurrent builds if necessary
use
execute system groovy script
as a first build step:and in the interrupted job(s) there will be a log:
Based on the idea by @C4stor I have made this improved version... I find it more readable from @daggett 's version