【问题标题】:Disable all Jenkins jobs from a given Jenkins View / Tab从给定的 Jenkins 视图/选项卡中禁用所有 Jenkins 作业
【发布时间】:2015-06-29 16:52:17
【问题描述】:

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

我尝试了以下 groovy 语法,首先只在给定视图中显示所有作业,但它出错了。

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()”函数即可。

如果我使用下面的代码,它会做我想要的,但我正在寻找一个单一的班轮。

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

}  

【问题讨论】:

    标签: groovy jenkins gradle jenkins-cli jenkins-scriptler


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 如果我不想显示/回显任何内容,是否可以在第一个命令(单行)中使用每个?
    • 你不应该需要第一个命令应该按原样禁用所有项目
    • 我已经更新了答案以支持“脚本控制台”和“脚本程序”运行脚本的方式。否则,当我尝试将代码(在 Jenkins 的脚本控制台窗口中成功运行)作为 Scriptler 脚本方式时,它给了我一个错误。
    • 你看到那里有错误吗?
    【解决方案2】:

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

    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() }
    

    【讨论】:

      【解决方案3】:
      Jenkins.instance.getView("Gradle Deploys").items*.disabled = true
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-08-06
        • 1970-01-01
        • 1970-01-01
        • 2011-09-10
        • 2015-12-03
        • 2021-11-22
        • 1970-01-01
        相关资源
        最近更新 更多