From 24b8b221a432edb81ac065660b378aeb08ae073c Mon Sep 17 00:00:00 2001 From: Rishabh Singh Date: Thu, 12 Sep 2024 13:32:16 -0700 Subject: [PATCH] add script to terminate stale jobs Signed-off-by: Rishabh Singh --- vars/abortStaleJenkinsJobs.groovy | 63 +++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 vars/abortStaleJenkinsJobs.groovy diff --git a/vars/abortStaleJenkinsJobs.groovy b/vars/abortStaleJenkinsJobs.groovy new file mode 100644 index 00000000..30cde7f9 --- /dev/null +++ b/vars/abortStaleJenkinsJobs.groovy @@ -0,0 +1,63 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +/** Library to fetch failing tests at the end of gradle-check run and index the results in an OpenSearch cluster. + * + * @param Map args = [:] args A map of the following parameters + * @param args.jobName - The name of the jenkins job. + * @param args.lookupTime - Fetch builds from past N hours for the job, defaults to 6 hours. + */ + +import java.time.Instant +import java.time.temporal.ChronoUnit +import jenkins.model.Jenkins +import hudson.model.Result + +void call(Map args = [:]) { + String jobName = args.jobName.toString() + long lookupTime = isNullOrEmpty(args.lookupTime.toString()) ? 6 : Long.parseLong(args.lookupTime.toString()) + + if (isNullOrEmpty(jobName)) { + throw new IllegalArgumentException("Error: jobName is null or empty") + } + + def currentBuildNumber = currentBuild.number + def currentBuildDescription = currentBuild.description + def endTime = Instant.now() + def startTime = endTime.minus(lookupTime, ChronoUnit.HOURS) + def startMillis = startTime.toEpochMilli() + def endMillis = endTime.toEpochMilli() + + // Add sleep to let job-id get assigned to queued jobs when triggered via generic webhook url + sleep(15) + + def currentJob = Jenkins.instance.getItemByFullName(jobName) + + //Fetch all builds for the job based on look up time provided + def builds = currentJob.getBuilds().byTimestamp(startMillis,endMillis) + for (build in builds) { + if (build.isBuilding() && currentBuildNumber > build.number && currentBuildDescription == build.description) { + try { + build.doStop() + println "Aborted build #${build.number} for ${build.description}" + } + catch (Exception e) { + if (build.result == Result.ABORTED) { + println "Build #${build.number} was already aborted, possibly by someone else" + } + else { + println "Failed to abort build #${build.number}: ${e.message}" + } + } + } + } +} + + +boolean isNullOrEmpty(String str) { return (str == 'Null' || str == null || str.allWhitespace || str.isEmpty()) }