Add logic to cancel older running builds in Jenkins

https://www.pivotaltracker.com/story/show/163367849
Signed-off-by: Pedro Pombeiro <pombeirp@users.noreply.github.com>
This commit is contained in:
Pedro Pombeiro 2019-01-21 19:19:32 +01:00
parent a14076def7
commit 3d332e009b
No known key found for this signature in database
GPG Key ID: A65DEB11E4BBC647
6 changed files with 30 additions and 6 deletions

View File

@ -12,7 +12,6 @@ pipeline {
options { options {
timestamps() timestamps()
disableConcurrentBuilds()
/* Prevent Jenkins jobs from running forever */ /* Prevent Jenkins jobs from running forever */
timeout(time: 35, unit: 'MINUTES') timeout(time: 35, unit: 'MINUTES')
/* Limit builds retained */ /* Limit builds retained */
@ -59,6 +58,7 @@ pipeline {
mobile = load 'ci/mobile.groovy' mobile = load 'ci/mobile.groovy'
cmn = load 'ci/common.groovy' cmn = load 'ci/common.groovy'
print "Running ${cmn.getBuildType()} build!" print "Running ${cmn.getBuildType()} build!"
cmn.abortPreviousRunningBuilds()
/* Read the valid NodeJS version */ /* Read the valid NodeJS version */
env.NODE_VERSION = cmn.getToolVersion('node') env.NODE_VERSION = cmn.getToolVersion('node')
/* Cleanup and Prep */ /* Cleanup and Prep */

View File

@ -11,7 +11,6 @@ pipeline {
options { options {
timestamps() timestamps()
disableConcurrentBuilds()
/* Prevent Jenkins jobs from running forever */ /* Prevent Jenkins jobs from running forever */
timeout(time: 45, unit: 'MINUTES') timeout(time: 45, unit: 'MINUTES')
/* Limit builds retained */ /* Limit builds retained */
@ -40,6 +39,7 @@ pipeline {
mobile = load 'ci/mobile.groovy' mobile = load 'ci/mobile.groovy'
cmn = load 'ci/common.groovy' cmn = load 'ci/common.groovy'
print "Running ${cmn.getBuildType()} build!" print "Running ${cmn.getBuildType()} build!"
cmn.abortPreviousRunningBuilds()
/* Read the valid NodeJS version */ /* Read the valid NodeJS version */
env.NODE_VERSION = cmn.getToolVersion('node') env.NODE_VERSION = cmn.getToolVersion('node')
/* Cleanup and Prep */ /* Cleanup and Prep */

View File

@ -24,7 +24,6 @@ pipeline {
options { options {
timestamps() timestamps()
disableConcurrentBuilds()
/* Prevent Jenkins jobs from running forever */ /* Prevent Jenkins jobs from running forever */
timeout(time: 35, unit: 'MINUTES') timeout(time: 35, unit: 'MINUTES')
/* Limit builds retained */ /* Limit builds retained */
@ -58,6 +57,8 @@ pipeline {
/* Necessary to load methods */ /* Necessary to load methods */
desktop = load 'ci/desktop.groovy' desktop = load 'ci/desktop.groovy'
cmn = load 'ci/common.groovy' cmn = load 'ci/common.groovy'
print "Running ${cmn.getBuildType()} build!"
cmn.abortPreviousRunningBuilds()
/* Read the valid NodeJS version */ /* Read the valid NodeJS version */
env.NODE_VERSION = cmn.getToolVersion('node') env.NODE_VERSION = cmn.getToolVersion('node')
/* Cleanup and Prep */ /* Cleanup and Prep */

View File

@ -11,7 +11,6 @@ pipeline {
options { options {
timestamps() timestamps()
disableConcurrentBuilds()
/* Prevent Jenkins jobs from running forever */ /* Prevent Jenkins jobs from running forever */
timeout(time: 25, unit: 'MINUTES') timeout(time: 25, unit: 'MINUTES')
/* Limit builds retained */ /* Limit builds retained */
@ -40,6 +39,8 @@ pipeline {
/* Necessary to load methods */ /* Necessary to load methods */
desktop = load 'ci/desktop.groovy' desktop = load 'ci/desktop.groovy'
cmn = load 'ci/common.groovy' cmn = load 'ci/common.groovy'
print "Running ${cmn.getBuildType()} build!"
cmn.abortPreviousRunningBuilds()
/* Read the valid NodeJS version */ /* Read the valid NodeJS version */
env.NODE_VERSION = cmn.getToolVersion('node') env.NODE_VERSION = cmn.getToolVersion('node')
/* Cleanup and Prep */ /* Cleanup and Prep */

View File

@ -24,7 +24,6 @@ pipeline {
options { options {
timestamps() timestamps()
disableConcurrentBuilds()
/* Prevent Jenkins jobs from running forever */ /* Prevent Jenkins jobs from running forever */
timeout(time: 45, unit: 'MINUTES') timeout(time: 45, unit: 'MINUTES')
/* Limit builds retained */ /* Limit builds retained */
@ -61,6 +60,8 @@ pipeline {
/* Necessary to load methods */ /* Necessary to load methods */
desktop = load 'ci/desktop.groovy' desktop = load 'ci/desktop.groovy'
cmn = load 'ci/common.groovy' cmn = load 'ci/common.groovy'
print "Running ${cmn.getBuildType()} build!"
cmn.abortPreviousRunningBuilds()
/* Read the valid NodeJS version */ /* Read the valid NodeJS version */
env.NODE_VERSION = cmn.getToolVersion('node') env.NODE_VERSION = cmn.getToolVersion('node')
/* Cleanup and Prep */ /* Cleanup and Prep */

View File

@ -1,4 +1,7 @@
import groovy.json.JsonBuilder import groovy.json.JsonBuilder
import hudson.model.Result
import hudson.model.Run
import jenkins.model.CauseOfInterruption.UserInterruption
def version() { def version() {
return readFile("${env.WORKSPACE}/VERSION").trim() return readFile("${env.WORKSPACE}/VERSION").trim()
@ -29,6 +32,25 @@ def getBuildType() {
return params.BUILD_TYPE 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) { def buildBranch(name = null, buildType = null) {
/* default to current build type */ /* default to current build type */
buildType = buildType ? buildType : getBuildType() buildType = buildType ? buildType : getBuildType()
@ -241,7 +263,6 @@ def gitHubNotifyFull(urls) {
gitHubNotify(msg) gitHubNotify(msg)
} }
def gitHubNotifyPRFailure() { def gitHubNotifyPRFailure() {
def d = ":small_orange_diamond:" def d = ":small_orange_diamond:"
def msg = "#### :x: " def msg = "#### :x: "