8

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)

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

}  
4

3 回答 3

14

您应该能够通过以下方式禁用它们:

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

但是如果你想同时打印一些东西,你需要一个each

jenkins.instance.getView("Gradle Deploys").items.each { item ->
    println "\nJob: $item.name"
    item.disabled = true
}
于 2015-06-29T17:17:52.313 回答
6

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

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

但是如果你想同时打印一些东西,你需要一个 each

    jenkins = Hudson.instance

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

现在,如果您尝试从“脚本控制台”运行上述示例,它们可以完美运行,但如果您尝试将其创建/运行为 Scriptler 脚本(如下所示),则会出错。

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

在此处输入图像描述

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

在此处输入图像描述

要解决上述错误信息(如 Scriptler 脚本 - 窗口所示),您需要输入另一行(在顶部)。

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

//如果您通过“Scriptler Script”脚本方式运行脚本/代码,则需要以下行。//通过这种方式,您可以提示用户提供参数(例如:viewName)并使用它来禁用该视图中的作业。

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() }
于 2015-06-30T19:59:06.187 回答
1
Jenkins.instance.getView("Gradle Deploys").items*.disabled = true
于 2018-07-13T02:03:22.907 回答