Skip to content

Instantly share code, notes, and snippets.

@alipandidan
Last active May 5, 2020 16:29
Show Gist options
  • Save alipandidan/869a4337b2dce929ad93b5d7729dfb2b to your computer and use it in GitHub Desktop.
Save alipandidan/869a4337b2dce929ad93b5d7729dfb2b to your computer and use it in GitHub Desktop.
Abort previous Jenkins build
import hudson.model.Result
import hudson.model.Run
import jenkins.model.CauseOfInterruption.UserInterruption
def call() {
// https://stackoverflow.com/a/49901413/4763512
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()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment