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?
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.
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.
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.
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:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With