从给定的Jenkins视图/选项卡中禁用所有Jenkins作业 [英] Disable all Jenkins jobs from a given Jenkins View / Tab

查看:520
本文介绍了从给定的Jenkins视图/选项卡中禁用所有Jenkins作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在詹金斯创建的一个名为"Gradle Deploys"的视图中,有大约100-120个工作.如何仅从给定的视图"/选项卡"禁用Jenkins的所有作业.

I have around 100-120 jobs in one of the view called "Gradle Deploys" that I created in Jenkins. How can I disable all the jobs from Jenkins only from a given View / tab.

我尝试了以下常规语法,首先只显示给定视图中的所有作业,但出错了.

I tried the following groovy syntax to first just show all the jobs in a given view but it errors out.

jenkins = Hudson.instance

//The following works actually but gives a lot of info.
//println "----" + jenkins.instance.getView("Gradle Deploys").items

println "----" + jenkins.instance.getView("Gradle Deploys").items.each.getItems().print(it)

一旦获得给定视图中的职位名称列表,我只需要在上述命令中使用".disable()"函数即可.

Once I get the list of just job names in a given view, I just have to use ".disable()" function in the above command and it'll work.

如果我使用下面的代码,它可以满足我的要求,但是我正在寻找一根衬板.

If I use the following code, it does what I want, but I'm looking for a one liner.

for (item in jenkins.instance.getView("Gradle Deploys").items) {
   println("\nJob: $item.name")
   item.disabled=true

}  

推荐答案

感谢Tim的解决方案.我正在进一步添加/增强它:

Thanks to Tim for his solution. I'm adding/enhancing it a bit further:

jenkins.instance.getView("Gradle Deploys").items*.disabled = true

但是,如果您想同时打印出一些东西,则需要每张纸

But if you want to print something out at the same time, you'll need an each

    jenkins = Hudson.instance

    jenkins.instance.getView("Gradle Deploys").items.each { item ->
    println "\nJob: $item.name"
    item.disabled = true
}

现在,如果您尝试从脚本控制台"运行上述示例,则它们完美地工作,但是如果您尝试将其作为Scriptler脚本创建/运行,则会出错(如下所示).

Now, the above examples works perfectly if you try to run them from "Script Console" but it errors out if you try to create/run it as a Scriptler script (as shown below).

请参阅:上面的代码在Jenkins的脚本控制台"视图中有效(当您单击管理Jenkins">脚本控制台"时).为此,您可能需要安装插件.

See: The above code works in Script Console view in Jenkins (When you click Manage Jenkins > Script Console). To get this, you may need the plugin installed.

现在,当我尝试创建脚本脚本并以这种方式运行该脚本时,该脚本无法正常工作.这需要安装Scriptler插件.

Now, the same script didn't work when I tried to create a Scripter Script and ran it that way. This requires Scriptler Plugin installed.

要解决以上错误消息(如Scriptler Script-窗口中所示),您需要输入另一行(在顶部).

To Solve the above error message (as shown in the Scriptler Script - window), you need to enter another line (at top).

最终脚本看起来像(注意:viewName变量的值将由Scriptler参数提供,它将覆盖脚本本身中提到的所有内容):

Final script looks like (NOTE: Value of viewName variable will be provided by the Scriptler parameter and it'll overwrite whatever you'll mention in the script itself):

//如果通过脚本脚本"脚本方式运行脚本/代码,则以下行是必需的. //通过这种方式,您可以提示用户提供参数(例如,viewName),并使用它仅在该视图中禁用作业.

//The following line is necessary if you are running the script/code via a "Scriptler Script" script way. //This way you can prompt a user to provide parameters (for ex: viewName) and use it to disable job in that view only.

import hudson.model.*

jenkins = Hudson.instance

println ""
println "--- Disabling all jobs in view: ${viewName}"
println ""

jenkins.instance.getView(viewName).items*.disabled = true

//Now the above will disable it but you still need to save it. Otherwise, you'll loose your changes (of disabling the jobs) after each Jenkins restart.
jenkins.instance.getView(viewName).items.each { item -> item.save() }

这篇关于从给定的Jenkins视图/选项卡中禁用所有Jenkins作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆