如何在脚本中分配“多项目节流类别”

时间:2013-12-22 20:22:18

标签: groovy jenkins

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

2 个答案:

答案 0 :(得分:0)

我发现这个工作

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)

答案 1 :(得分:0)

就地修改类别对我来说不起作用。相反,我必须创建一个新的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()
}