我正在尝试建立一个脚本来杀死/中止所有具有特定名称的Jenkins作业。我在查找有关Jenkins类及其包含的内容的文档时遇到了麻烦。
我知道有可用的插件,但已指示我不要使用它们。否则,我在这里(How to stop an unstoppable zombie job on Jenkins without restarting the server?,(Cancel queued builds and aborting executing builds using Groovy for Jenkins)提到了一些半相关的问题,我试图对其中的一些代码进行重做,但是并不能完全导致失业:
import hudson.model.*
def jobList = Jenkins.instance.queue
jobList.items.findAll { it.task.name.contains('searchTerm') }.each { jobList.kill(it.task) }
我也尝试了以下方法:
def jobname = ""
def buildnum = 85
def job = Jenkins.instance.getItemByFullName(jobname)
for (build in job.builds) {
if (buildnum == build.getNumber().toInteger()){
if (build.isBuilding()){
build.doStop();
build.doKill();
}
}
}
第一个脚本没有执行繁重的工作,而第二个脚本则抛出NullPointerException:
java.lang.NullPointerException: Cannot get property 'builds' on null object
答案 0 :(得分:0)
我设法使其工作;我的第二个例子没有用,因为我脑子发闷了,而我正在测试的工作没有构建。 :(
def searchTerm = ""
def matchedJobs = Jenkins.instance.items.findAll { job ->
job.name.contains(searchTerm)
def desiredState = "stop"
if (desiredState.equals("stop")) {
println "Stopping all current builds ${job.name}"
for (build in job.builds) {
if (build.isBuilding()){
build.doStop();
println build.name + " successfully stopped!"
}
}
}