对于使用Groovy系统脚本的Jenkins,有没有办法轻松搜索构建队列和某些条件的执行构建列表(特别是匹配某些条件的参数)然后杀死/取消它们?
我似乎无法找到任何方法来做到这一点,但似乎它应该是可能的。
答案 0 :(得分:57)
我自己没有测试过,但是看看API它应该可以通过以下方式进行测试:
import hudson.model.*
def q = Jenkins.instance.queue
q.items.findAll { it.task.name.startsWith('my') }.each { q.cancel(it.task) }
相关API链接:
答案 1 :(得分:19)
我知道这是一个老问题,但Google指出了这个问题。此处显示的脚本仅从队列中删除作业,并且不会停止运行构建。以下脚本只删除队列中的所有内容并终止所有正在运行的构建:
import java.util.ArrayList
import hudson.model.*;
// Remove everything which is currently queued
def q = Jenkins.instance.queue
for (queued in Jenkins.instance.queue.items) {
q.cancel(queued.task)
}
// stop all the currently running jobs
for (job in Jenkins.instance.items) {
stopJobs(job)
}
def stopJobs(job) {
if (job in com.cloudbees.hudson.plugins.folder.Folder) {
for (child in job.items) {
stopJobs(child)
}
} else if (job in org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject) {
for (child in job.items) {
stopJobs(child)
}
} else if (job in org.jenkinsci.plugins.workflow.job.WorkflowJob) {
if (job.isBuilding()) {
for (build in job.builds) {
build.doKill()
}
}
}
}
答案 2 :(得分:4)
无法添加评论,但截至今天最新的詹金斯,安德烈的脚本(不错)需要另一个导入工作。执行系统Groovy脚本。
Jenkins错误并提到了失踪的班级。我提到了提到这个问题的网址:
//import hudson.model.*
// per http://stackoverflow.com/questions/17429050/running-groovy-command-from-jenkins-using-groovy-script-plugin
// requires this now
import jenkins.model.Jenkins
def q = Jenkins.instance.queue
q.items.findAll { it.task.name.startsWith('my') }.each { q.cancel(it.task) }
答案 3 :(得分:3)
这是我的解决方案,如果您只想从构建队列中运行同一项目的最新作业并取消其他作业:
def q = Jenkins.instance.queue
//Find items in queue that match <project name>
def queue = q.items.findAll { it.task.name.startsWith('sample_project') }
//get all jobs id to list
def queue_list = []
queue.each { queue_list.add(it.getId()) }
//sort id's, remove last one - in order to keep the newest job, cancel the rest
queue_list.sort().take(queue_list.size() - 1).each { q.doCancelItem(it) }
答案 4 :(得分:2)
使用jenkins groovy postbuild插件:
我认为这将是一个时髦的脚本:
import hudson.model.*
def q = jenkins.model.Jenkins.getInstance().getQueue()
def items = q.getItems()
for (i=0;i<items.length;i++){
if(items[i].task.getName() == "job_name"){
items[i].doCancelQueue()
}
}
答案 5 :(得分:1)
经过一番调查,我想出了这段代码,对我来说绝对不错。它会清除队列,并中止当前正在执行的所有作业。
先决条件:
import jenkins.model.Jenkins
import hudson.*
import hudson.model.*
import jenkins.*
//Remove everything which is currently queued
Jenkins.instance.queue.clear()
def buildingJobs = Jenkins.instance.getAllItems(Job.class).findAll
{
it.isBuilding()
}
buildingJobs.each{
def jobName = it.toString()
def val = jobName.split("\\[|\\]")
//'Abort jobs' is the name of the job I have created, and I do not want it to abort itself.
if((val[1].trim())!='Abort jobs'){
def job = Jenkins.instance.getItemByFullName(val[1].trim())
for (build in job.builds) {
if (build.isBuilding()){
println(build)
build.doStop();
}
}
}
}
答案 6 :(得分:0)
要控制作业构建队列,您还可以使用此插件: https://wiki.jenkins-ci.org/display/JENKINS/Block+queued+job+plugin
答案 7 :(得分:0)
我已经通过Igor Zilberman对代码段进行了扩展,以便当队列中的作业具有相同原因(当您将鼠标悬停在构建中的作业上时看到的内容)时,它也会中止正在运行的作业队列,只看第一行)。我正在使用构建步骤“执行系统Groovy脚本”来运行它。
import hudson.model.Result
import jenkins.model.CauseOfInterruption
import jenkins.model.*;
[ // setup job names here
'my-jobname-here'
].each {jobName ->
def queue = Jenkins.instance.queue
def q = queue.items.findAll { it.task.name.equals(jobName) }
def r = [:]
def projs = jenkins.model.Jenkins.instance.items.findAll { it.name.equals(jobName) }
projs.each{p ->
x = p._getRuns()
x.each{id, y ->
r.put(id, y)
}
}
TreeMap queuedMap = [:]
TreeMap executingMap = [:]
q.each{i->
queuedMap.put(i.getId(), i.getCauses()[0].getShortDescription()) //first line
}
r.each{id, run->
def exec = run.getExecutor()
if(exec != null){
executingMap.put(id, run.getCauses()[0].getShortDescription()) //first line
}
}
println("Queued:")
queuedMap.each{ k, v -> println "${k}:${v}" }
println("Executing:")
executingMap.each{ k, v -> println "${k}:${v}" }
// First, if there is more than one queued entry, cancel all but the highest one.
// Afterwards, if there is a queued entry, cancel the running ones
def queuedNames = queuedMap.values();
queuedNames.each{n ->
def idsForName = []
queuedMap.each{ id, name ->
if(name.equals(n)){
idsForName.add(id)
}
}
if (idsForName.size() > 1){
println("Cancelling queued job: "+n)
}
// remove all but the latest from queue
idsForName.sort().take(idsForName.size() - 1).each { queue.doCancelItem(it) }
}
executingMap.each{ id, name ->
if(queuedMap.values().contains(name)){
r.each{rid, run->
if (id == rid){
def exec = run.getExecutor()
if(exec != null){
println("Aborting running job: "+id+": "+name)
exec.interrupt(Result.ABORTED)
}
}
}
}
}
}
return "Done"
答案 8 :(得分:0)
引荐来源:https://xanderx.com/post/cancel-all-queued-jenkins-jobs/
在Manage Jenkins>脚本控制台中运行它:
Jenkins.instance.queue.clear()
答案 9 :(得分:0)
导入 hudson.model.*
定义队列 = Hudson.instance.queue
println "队列包含 ${queue.items.length} 个项目"
queue.clear()
println "队列已清"
将此粘贴到“脚本控制台”中 - jenkins > Manage Jenkins > Script Console 并点击运行。 这有助于清除队列并显示已清除的数字。