Skip to main content
added 2 characters in body
Source Link

Based on the idea by @C4stor I have made this working and improved version (careful his version doesn't work...) I find it more readable from @daggett 's version

import hudson.model.Result import hudson.model.Run import jenkins.model.CauseOfInterruption.UserInterruption def abortPreviousBuilds() { Run previousBuild = currentBuild.rawBuild.getPreviousBuildInProgress() while (previousBuild != null) { if (previousBuild.isInProgress()) { def executor = previousBuild.getExecutor() if (executor != null) { echo ">> Aborting older build #${previousBuild.number}" executor.interrupt(Result.ABORTED, new UserInterruption( "Aborted by newer build #${currentBuild.number}" )) } } previousBuild = previousBuild.getPreviousBuildInProgress() } } 

Based on the idea by @C4stor I have made this working and improved version (careful his version doesn't work...)

import hudson.model.Result import hudson.model.Run import jenkins.model.CauseOfInterruption.UserInterruption def abortPreviousBuilds() { Run previousBuild = currentBuild.rawBuild.getPreviousBuildInProgress() while (previousBuild != null) { if (previousBuild.isInProgress()) { def executor = previousBuild.getExecutor() if (executor != null) { echo ">> Aborting older build #${previousBuild.number}" executor.interrupt(Result.ABORTED, new UserInterruption( "Aborted by newer build #${currentBuild.number}" )) } } previousBuild = previousBuild.getPreviousBuildInProgress() } } 

Based on the idea by @C4stor I have made this improved version... I find it more readable from @daggett 's version

import hudson.model.Result import hudson.model.Run import jenkins.model.CauseOfInterruption.UserInterruption def abortPreviousBuilds() { Run previousBuild = currentBuild.rawBuild.getPreviousBuildInProgress() while (previousBuild != null) { if (previousBuild.isInProgress()) { def executor = previousBuild.getExecutor() if (executor != null) { echo ">> Aborting older build #${previousBuild.number}" executor.interrupt(Result.ABORTED, new UserInterruption( "Aborted by newer build #${currentBuild.number}" )) } } previousBuild = previousBuild.getPreviousBuildInProgress() } } 
added 444 characters in body
Source Link

Based on the idea by @C4stor I have made this working and improved version: (careful his version doesn't work...)

import hudson.model.Result import hudson.model.Run import jenkins.model.CauseOfInterruption.UserInterruption def killOldBuildsabortPreviousBuilds(Run build) { ifRun (buildpreviousBuild === nullcurrentBuild.rawBuild.getPreviousBuildInProgress() {   while (previousBuild != null) return{ }  if (buildpreviousBuild.isInProgress()) { build def executor = previousBuild.doStopgetExecutor() }  def previousBuild if (executor != null) {  echo ">> Aborting older build #${previousBuild.getPreviousBuildInProgress()number}" killOldBuilds executor.interrupt(previousBuildResult.ABORTED, new UserInterruption( "Aborted by newer build #${currentBuild.number}" ))  }  } killOldBuilds(currentBuild.rawBuild previousBuild = previousBuild.getPreviousBuildInProgress()) } } 

Based on the idea by @C4stor I have made this working version:

import hudson.model.Run def killOldBuilds(Run build) { if (build == null) { return }  if (build.isInProgress()) { build.doStop() }  def previousBuild = build.getPreviousBuildInProgress() killOldBuilds(previousBuild) } killOldBuilds(currentBuild.rawBuild.getPreviousBuildInProgress()) 

Based on the idea by @C4stor I have made this working and improved version (careful his version doesn't work...)

import hudson.model.Result import hudson.model.Run import jenkins.model.CauseOfInterruption.UserInterruption def abortPreviousBuilds() { Run previousBuild = currentBuild.rawBuild.getPreviousBuildInProgress()   while (previousBuild != null) { if (previousBuild.isInProgress()) {  def executor = previousBuild.getExecutor()  if (executor != null) {  echo ">> Aborting older build #${previousBuild.number}"  executor.interrupt(Result.ABORTED, new UserInterruption( "Aborted by newer build #${currentBuild.number}" ))  }  }  previousBuild = previousBuild.getPreviousBuildInProgress() } } 
Source Link

Based on the idea by @C4stor I have made this working version:

import hudson.model.Run def killOldBuilds(Run build) { if (build == null) { return } if (build.isInProgress()) { build.doStop() } def previousBuild = build.getPreviousBuildInProgress() killOldBuilds(previousBuild) } killOldBuilds(currentBuild.rawBuild.getPreviousBuildInProgress())