Skip to content

Commit

Permalink
fix(cancelPreviousBuilds): only cancel builds older than current
Browse files Browse the repository at this point in the history
  • Loading branch information
jlegrone committed Jun 5, 2018
1 parent 701f3aa commit 7eb5dae
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions vars/cancelPreviousBuilds.groovy
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
#!/usr/bin/env groovy

def call() {
def jobname = env.JOB_NAME
def buildnum = env.BUILD_NUMBER.toInteger()
String jobname = env.JOB_NAME
int currentBuildNum = env.BUILD_NUMBER.toInteger()

def job = Jenkins.instance.getItemByFullName(jobname)
for (build in job.builds) {
if (!build.isBuilding()) {
continue;
// Only cancel builds that are ongoing and older than the current build
if (build.isBuilding() && currentBuildNum > build.getNumber().toInteger()) {
build.doStop();
echo "Build ${build.toString()} cancelled"
}
if (buildnum == build.getNumber().toInteger()) {
continue;
}
build.doStop();
echo "Build ${build.toString()} cancelled"
}
}

0 comments on commit 7eb5dae

Please sign in to comment.