I wanted to take some action based on all the concurrent running job displayName before starting a new run of the same pipeline job.
Could not get any satisfactory reference, if anyone has tried something similar please share.
I wanted to take some action based on all the concurrent running job displayName before starting a new run of the same pipeline job.
Could not get any satisfactory reference, if anyone has tried something similar please share.
Could able to write the following code to achieve my goal, it's working absolutely fine. Catch is to use same Displayname format in all the jobs and identifying same in the loop. This piece of code is written inside shared library vars folder example: vars/abortPreviousWorklow.groovy.
import hudson.model.Result
import hudson.model.Run
import jenkins.model.CauseOfInterruption.UserInterruption
def call(){
currentDisplayName = env.GERRIT_PROJECT+' '+env.GERRIT_BRANCH+' '+env.GERRIT_CHANGE_NUMBER
Run previousBuild = currentBuild.rawBuild.getPreviousBuildInProgress()
while (previousBuild != null) {
if (previousBuild.isInProgress()) {
echo ">>DisplayName of previous Build : ${previousBuild.displayName}"
if(previousBuild.displayName.find(currentDisplayName)) {
def executor = previousBuild.getExecutor()
if (executor != null) {
echo ">> Aborting older build #${previousBuild.number}"
executor.interrupt(Result.ABORTED, new UserInterruption(
"Aborted by newer build #${currentBuild.number}"
))
}
}
}
previousBuild = previousBuild.getPreviousBuildInProgress()
}
}