34
src/tech/avroid/jenkins/Jenkins.groovy
Normal file
34
src/tech/avroid/jenkins/Jenkins.groovy
Normal file
@@ -0,0 +1,34 @@
|
||||
package com.ncloudtech.jenkins
|
||||
|
||||
import hudson.model.Result
|
||||
import jenkins.model.CauseOfInterruption
|
||||
import jenkins.model.Jenkins
|
||||
|
||||
/**
|
||||
* Class is for working with Jenkins
|
||||
*/
|
||||
class Jenkins implements Serializable {
|
||||
|
||||
// See https://www.baeldung.com/java-serial-version-uid
|
||||
private static final long serialVersionUID = 1L
|
||||
|
||||
/**
|
||||
* Method cancel previous builds
|
||||
* @param jobName String - name of currentJob
|
||||
* @param buildNumber int - build number of current job
|
||||
* @param currentBuild current build information
|
||||
*/
|
||||
static void cancelPreviousBuilds(String jobName, int buildNumber, def currentBuild) {
|
||||
def currentJob = jenkins.model.Jenkins.instance.getItemByFullName(jobName)
|
||||
|
||||
for (def build : currentJob.builds) {
|
||||
def exec = build.getExecutor()
|
||||
if (build.isBuilding() && build.number.toInteger() < buildNumber && exec != null) {
|
||||
exec.interrupt(
|
||||
Result.ABORTED,
|
||||
new CauseOfInterruption.UserInterruption("Job aborted by #${currentBuild.number}")
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user