【问题标题】:Jenkins Script to Restrict all builds to a given node将所有构建限制在给定节点的 Jenkins 脚本
【发布时间】:2023-03-15 03:40:01
【问题描述】:

我们最近为我们的 Jenkins 构建环境添加了第二个从节点,该环境运行不同的操作系统(Linux 而不是 Windows),用于特定构建。不出所料,这意味着我们需要通过“限制此项目可以运行的位置”设置来限制构建。但是,我们有很多构建(100 多个),因此单击它们以手动更改此设置的前景并不让我兴奋。

有人可以通过 Jenkins 脚本控制台提供一个 groovy 脚本来实现这一点吗?我过去曾使用过类似的脚本来更改其他设置,但我找不到此特定设置的任何参考。

【问题讨论】:

    标签: groovy jenkins hudson


    【解决方案1】:

    根据以前的脚本和 Jenkins 源代码,我设法为自己找出了脚本。脚本如下:

    import hudson.model.*
    import hudson.model.labels.*
    import hudson.maven.*
    import hudson.tasks.*
    import hudson.plugins.git.*
    
    hudsonInstance = hudson.model.Hudson.instance
    allItems = hudsonInstance.allItems
    buildableItems = allItems.findAll{ job -> job instanceof BuildableItemWithBuildWrappers }
    
    buildableItems.each { item ->
      boolean shouldSave = false
      item.allJobs.each { job ->
        job.assignedLabel = new LabelAtom('windows-x86') 
      }
    }
    

    用您需要的节点标签替换“windows-x86”。如有必要,您还可以根据 item.name 进行有条件的更改以过滤掉一些工作。

    【讨论】:

      【解决方案2】:

      你可以试试Jenkins Job-DSL plugin

      这将允许您创建一个工作来更改您的其他工作。这是通过在基于 groovy 的 DSL 中提供构建步骤来修改其他作业来实现的。

      这里的这个会为作业“xxxx”添加一个标签。我用工作本身作为模板有点作弊。

      job{
        using 'xxxx'
        name 'xxxx'
        label 'Linux'
      }
      

      如果你们中的一些工作是不同类型的,你可能需要调整它

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-14
        • 1970-01-01
        • 1970-01-01
        • 2019-07-14
        • 1970-01-01
        • 2018-10-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多