Last active
May 5, 2020 16:29
-
-
Save alipandidan/869a4337b2dce929ad93b5d7729dfb2b to your computer and use it in GitHub Desktop.
Abort previous Jenkins build
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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