From 3d332e009b123438a5e988742be769784a1d5556 Mon Sep 17 00:00:00 2001 From: Pedro Pombeiro Date: Mon, 21 Jan 2019 19:19:32 +0100 Subject: [PATCH] Add logic to cancel older running builds in Jenkins https://www.pivotaltracker.com/story/show/163367849 Signed-off-by: Pedro Pombeiro --- ci/Jenkinsfile.android | 2 +- ci/Jenkinsfile.ios | 2 +- ci/Jenkinsfile.linux | 3 ++- ci/Jenkinsfile.macos | 3 ++- ci/Jenkinsfile.windows | 3 ++- ci/common.groovy | 23 ++++++++++++++++++++++- 6 files changed, 30 insertions(+), 6 deletions(-) diff --git a/ci/Jenkinsfile.android b/ci/Jenkinsfile.android index 3c71c3966c..0b50980924 100644 --- a/ci/Jenkinsfile.android +++ b/ci/Jenkinsfile.android @@ -12,7 +12,6 @@ pipeline { options { timestamps() - disableConcurrentBuilds() /* Prevent Jenkins jobs from running forever */ timeout(time: 35, unit: 'MINUTES') /* Limit builds retained */ @@ -59,6 +58,7 @@ pipeline { mobile = load 'ci/mobile.groovy' cmn = load 'ci/common.groovy' print "Running ${cmn.getBuildType()} build!" + cmn.abortPreviousRunningBuilds() /* Read the valid NodeJS version */ env.NODE_VERSION = cmn.getToolVersion('node') /* Cleanup and Prep */ diff --git a/ci/Jenkinsfile.ios b/ci/Jenkinsfile.ios index dd277cb670..675bd95c8f 100644 --- a/ci/Jenkinsfile.ios +++ b/ci/Jenkinsfile.ios @@ -11,7 +11,6 @@ pipeline { options { timestamps() - disableConcurrentBuilds() /* Prevent Jenkins jobs from running forever */ timeout(time: 45, unit: 'MINUTES') /* Limit builds retained */ @@ -40,6 +39,7 @@ pipeline { mobile = load 'ci/mobile.groovy' cmn = load 'ci/common.groovy' print "Running ${cmn.getBuildType()} build!" + cmn.abortPreviousRunningBuilds() /* Read the valid NodeJS version */ env.NODE_VERSION = cmn.getToolVersion('node') /* Cleanup and Prep */ diff --git a/ci/Jenkinsfile.linux b/ci/Jenkinsfile.linux index c310e84a98..470655d0cc 100644 --- a/ci/Jenkinsfile.linux +++ b/ci/Jenkinsfile.linux @@ -24,7 +24,6 @@ pipeline { options { timestamps() - disableConcurrentBuilds() /* Prevent Jenkins jobs from running forever */ timeout(time: 35, unit: 'MINUTES') /* Limit builds retained */ @@ -58,6 +57,8 @@ pipeline { /* Necessary to load methods */ desktop = load 'ci/desktop.groovy' cmn = load 'ci/common.groovy' + print "Running ${cmn.getBuildType()} build!" + cmn.abortPreviousRunningBuilds() /* Read the valid NodeJS version */ env.NODE_VERSION = cmn.getToolVersion('node') /* Cleanup and Prep */ diff --git a/ci/Jenkinsfile.macos b/ci/Jenkinsfile.macos index f8d6ae61e9..a1d8a14d4e 100644 --- a/ci/Jenkinsfile.macos +++ b/ci/Jenkinsfile.macos @@ -11,7 +11,6 @@ pipeline { options { timestamps() - disableConcurrentBuilds() /* Prevent Jenkins jobs from running forever */ timeout(time: 25, unit: 'MINUTES') /* Limit builds retained */ @@ -40,6 +39,8 @@ pipeline { /* Necessary to load methods */ desktop = load 'ci/desktop.groovy' cmn = load 'ci/common.groovy' + print "Running ${cmn.getBuildType()} build!" + cmn.abortPreviousRunningBuilds() /* Read the valid NodeJS version */ env.NODE_VERSION = cmn.getToolVersion('node') /* Cleanup and Prep */ diff --git a/ci/Jenkinsfile.windows b/ci/Jenkinsfile.windows index faf9693db8..7dfb291b06 100644 --- a/ci/Jenkinsfile.windows +++ b/ci/Jenkinsfile.windows @@ -24,7 +24,6 @@ pipeline { options { timestamps() - disableConcurrentBuilds() /* Prevent Jenkins jobs from running forever */ timeout(time: 45, unit: 'MINUTES') /* Limit builds retained */ @@ -61,6 +60,8 @@ pipeline { /* Necessary to load methods */ desktop = load 'ci/desktop.groovy' cmn = load 'ci/common.groovy' + print "Running ${cmn.getBuildType()} build!" + cmn.abortPreviousRunningBuilds() /* Read the valid NodeJS version */ env.NODE_VERSION = cmn.getToolVersion('node') /* Cleanup and Prep */ diff --git a/ci/common.groovy b/ci/common.groovy index a0cea62652..05158f6ba7 100644 --- a/ci/common.groovy +++ b/ci/common.groovy @@ -1,4 +1,7 @@ import groovy.json.JsonBuilder +import hudson.model.Result +import hudson.model.Run +import jenkins.model.CauseOfInterruption.UserInterruption def version() { return readFile("${env.WORKSPACE}/VERSION").trim() @@ -29,6 +32,25 @@ def getBuildType() { return params.BUILD_TYPE } +@NonCPS +def abortPreviousRunningBuilds() { + 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( + "newer build #${currentBuild.number}" + )) + } + } + + previousBuild = previousBuild.getPreviousBuildInProgress() + } +} + def buildBranch(name = null, buildType = null) { /* default to current build type */ buildType = buildType ? buildType : getBuildType() @@ -241,7 +263,6 @@ def gitHubNotifyFull(urls) { gitHubNotify(msg) } - def gitHubNotifyPRFailure() { def d = ":small_orange_diamond:" def msg = "#### :x: "