-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsFile
23 lines (21 loc) · 868 Bytes
/
JenkinsFile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@NonCPS
def cancelPreviousBuilds() {
def jobName = env.JOB_NAME
def buildNumber = env.BUILD_NUMBER.toInteger()
/* Get job name */
def currentJob = Jenkins.instance.getItemByFullName(jobName)
/* Iterating over the builds for specific job */
for (def build : currentJob.builds) {
def listener = build.getListener()
def exec = build.getExecutor()
/* If there is a build that is currently running and it's not current build */
if (build.isBuilding() && build.number.toInteger() < buildNumber && exec != null) {
/* Then stop it */
exec.interrupt(
Result.ABORTED,
new CauseOfInterruption.UserInterruption("Aborted by #${currentBuild.number}")
)
println("Aborted previously running build #${build.number}")
}
}
}