先决条件:
1/ 我有 4 个节点连接到一个标签,如下所示。 所有节点都连接到 Jenkins 服务器,启动并运行。
2/ 我的詹金斯管道如下所示,其中我们有三个并行阶段,即 sanitylist1、sanitylist2、sanitylist3
3/我的常规脚本如下
def sanitylist1 = script1.sh, script2.sh, script3.sh //All these are read from string parameter
def sanitylist2 = script4.sh, script5.sh, script6.sh
def sanitylist3 = script7.sh, script8.sh, script9.sh
stage('Santiy tests') {
parallel {
stage('SanityList1') {
options {
timestamps()
timeout(time: 6, unit: 'HOURS')
}
agent {
node {
label 'Autolabel'
customWorkspace 'C:/git_repository'
}
}
steps {
script {
for (i in sanitylist1) {
echo "running : ${i}"
bat "test.bat ${i}" //takes one script from sanitylist1 and replays
sleep(10)
}
}
}
}
stage('SanityList2') {
options {
timestamps()
timeout(time: 6, unit: 'HOURS')
}
agent {
node {
label 'Autolabel'
customWorkspace 'C:/git_repository'
}
}
steps {
script {
for (i in sanitylist2) {
echo "running : ${i}"
bat "test.bat ${i}" //takes one script from sanitylist1 and replays
sleep(10)
}
}
}
}
stage('SanityList3') {
options {
timestamps()
timeout(time: 6, unit: 'HOURS')
}
agent {
node {
label 'Autolabel'
customWorkspace 'C:/git_repository'
}
}
steps {
script {
for (i in sanitylist3) {
echo "running : ${i}"
bat "test.bat ${i}" //takes one script from sanitylist1 and replays
sleep(10)
}
}
}
}
} //parallel
} //parallel stage
要求:
由于我们连接了 4 个节点,因此我们预计所有 4 个节点都会被利用。但我们看到只有 3 个节点正在使用,而 1 个节点未使用。
我们如何将下一个脚本从任何 sanitylists 变量(即 sanitylist1 或 sanitylist2 或 sanitylist3)发送到未使用的节点以实现更快的重播?
我们不想创建一个新阶段“SanityList4”并分配给剩余节点。 要求是将所有节点动态分配到可用阶段。
我想不出一种方法以声明的方式做到这一点,但如果您愿意接受一些脚本编写,那么您可以从单个脚本名称列表动态生成并行阶段:
def scriptNames = ['script1.sh', ..., 'script9.sh'] // The list of scripts to run
def stages = scriptNames.collectEntries { scriptName -> [
scriptName,
{
node('Autolabel') {
ws('C:/git_repository') {
echo "running : ${scriptName}"
bat "test.bat ${scriptName}"
sleep(10)
}
}
}
]}
parallel (stages)
显然,您可以添加所有其他内容,例如超时、时间戳和其他所有内容,将其放入声明性管道的
script
块中。