【问题标题】:How to assign "Multi-Project Throttle Category" in a script如何在脚本中分配“多项目节流类别”
【发布时间】:2017-04-04 16:47:10
【问题描述】:

我正在使用带有 Throttle Concurrent Builds 插件的 Jenkins,以确保在测试作业中独占访问 USB 设备。我使用参数化作业,带有一个名为 MODE 的参数。对于某些 MODE 值,测试使用 USB 设备,而对于其他 MODE 值,测试不使用 USB 设备。 我正在编写一个用于运行测试的 Groovy 脚本。 是否可以在脚本中分配“多项目油门类别”,以便我可以根据我的 MODE 参数的值来分配它? 谢谢

【问题讨论】:

    标签: groovy jenkins


    【解决方案1】:

    就地修改类别对我不起作用。相反,我必须创建一个新的 ThrottleJobProperty 并添加它:

    ThrottleJobProperty jobProperty = item.getProperty(ThrottleJobProperty)
    
    println("ThrottleJobProperty of " + item.name + " has categories: " + jobProperty?.categories)
    String category = "long-running"
    if (!jobProperty?.categories?.contains(category)) {
        if (jobProperty != null) item.removeProperty(jobProperty)
    
        List<String> categories = jobProperty != null ?
                new ArrayList<String>(jobProperty.categories) :
                new ArrayList<String>()
        categories.add(category)
        jobProperty = new ThrottleJobProperty(
                /*maxConcurrentPerNode:*/ 0,
                /*maxConcurrentTotal:*/ 0,
                /*categories:*/ categories,
                /*throttleEnabled:*/ true,
                /*throttleOption:*/ 'category',
                /*limitOneJobWithMatchingParams:*/ false,
                /*paramsToUseForLimit:*/ '',
                /*matrixOptions:*/ null
        )
        item.addProperty(jobProperty)
        println("Assigning ThrottleJobProperty.categories for " + item.name + ": " + jobProperty?.categories)
        item.save()
    }
    

    【讨论】:

    • 我尝试运行它,但找不到 ThrottleJobProperty 的构造函数。你现在为什么?
    • 很可能在您的插件版本中有所不同。查看可用的构造函数并尝试适应。
    • 谢谢你!这个细节我没注意
    【解决方案2】:

    我发现这行得通

    tjp = myjob.getProperty(hudson.plugins.throttleconcurrents.ThrottleJobProperty)
    
    // see what we got
    if(tjp != null) {
        println("--- Throttle concurrents for " + myjob.name + " ---")
    
        try {
            println "Got this: " + tjp.categories + " items " + tjp.categories.size
        } catch(Exception e) {
            println(tjp.categories)
        }
    }
    
    // change the first one
    tjp.categories[0] = "myCategory"
    
    // update job properties
    myjob.addProperty(tjp)
    

    【讨论】:

    • 这会修改作业配置还是构建属性?是否在 Throttle Concurrent Builds 插件阻止其执行之前生效?还是它在外部修改作业然后触发它的构建?我想要一个参数化作业,其中参数本质上是多项目油门类别,我可以在其中运行具有相同和不同参数的多个作业,以及具有不同参数的相同作业,并且每个类别都会适当地阻止(类似于您的用例:通过 USB 连接的不同设备)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-13
    • 2016-04-02
    • 2012-08-09
    • 1970-01-01
    • 2022-07-06
    • 2014-12-23
    • 1970-01-01
    相关资源
    最近更新 更多