首页 > 解决方案 > 如何在jenkins groovy中并行运行方法

问题描述

我有 jenkins 共享库,其中包含内部方法(我们称之为 A())

目前 A() 方法按顺序运行 10-100 次。

我想与预定义的批量编号 (10) 并行运行 A()

我没有找到通过 groovy 实现它的方法,Jenkins 的一些方法需要批准。

在不需要任何詹金斯批准的情况下,通过 groovy (不是詹金斯的并行步骤)来做到这一点的方法是什么?

标签: jenkinsgroovyjenkins-pipelinejenkins-groovy

解决方案


我不知道您是否需要此权限,但请尝试一下:

def A = {
    println "hello from A($it)"
    Thread.sleep(Math.random() * 2000 as long)
    println "hello from A($it)"
}

def threads = (0..5).collect { counter ->
    Thread.start { A(counter) }
}

threads.each { it.join() }

我放了println一些睡眠来演示并行运行的方法。您可以根据需要更换身体A{}


推荐阅读