Skip to content

Commit

Permalink
ci(fix): Cancelling previous builds (#16569)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChathurindaRanasinghe authored Feb 26, 2025
1 parent 0f2ca59 commit 1757be4
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions scripts/jenkins/jenkinsfiles/Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,27 @@ try {
def cancelPreviousBuilds() {
echo "###### Cancelling all previous builds ######"
def hi = Hudson.instance
def pname = env.JOB_NAME.split('/')[0]
def jobNameParts = env.JOB_NAME.tokenize('/')
def pname = jobNameParts[0]

hi.getItem(pname).getItem(env.JOB_BASE_NAME).getBuilds().each{ build ->
def item = hi.getItem(pname)
// if called from multibranch pipeline/folder etc., we need to get the builds of the leaf item
if (jobNameParts.size() > 1) {
// remove the first element, because that has been already used to get the root item
jobNameParts.remove(0)
for (part in jobNameParts) {
item = item.getItem(part)
}
}

item.getBuilds().each{ build ->
def exec = build.getExecutor()

if (build.number != currentBuild.number && exec != null) {
if (build.number < currentBuild.number && exec != null) {
exec.doStop()
println("Aborted previous running build #${build.number}")
} else {
println("Build is not running or is current build, not aborting - #${build.number}")
}
}
}

0 comments on commit 1757be4

Please sign in to comment.