我正在尝试collectEntries
在我的 Groovy 脚本中使用多个系列。最好看一下代码,现在我得到了:
stage('Test') {
// Reading content of the file
def portsFileContent = readFile 'UsedPorts.txt'
// Split the file by next line
def ports = portsFileContent.split('\n')
def steps = ports.collectEntries { port ->
["UI Test on port $port", {
sh "#!/bin/bash -lx \n startServerWithDifferentPort --params=port=$port"
}]
}
parallel steps
}
在“UsedPorts.txt”文件中,不同的端口由换行符分隔,例如:
4723
4733
4743
所以这个数字被存储在变量ports
中,然后这个变量被用来为每个端口启动一个服务器实例。因此,在这种情况下,它将通过以下命令启动 3 个不同的服务器实例:
def steps = ports.collectEntries { port ->
["UI Test on port $port", {
sh "#!/bin/bash -lx \n startServerWithDifferentPort --params=port=$port"
}]
}
parallel steps
因为parallel steps
它启动了3个服务器实例同时使用不同的端口。
那工作正常,但我有另一个文件,需要再次做同样的事情。所以在我的第二个文件中有如下条目:
name1
name2
name3
我再次创建了一个变量来存储我的 3 个条目:
def anotherFile = readFile 'Names.txt'
def names = anotherFile.split('\n')
这是我尝试过的:
stage('Test') {
// Reading content of the file
def portsFileContent = readFile 'UsedPorts.txt'
// Split the file by next line
def ports = portsFileContent.split('\n')
// Do the same again
def anotherFile = readFile 'Names.txt'
def names = anotherFile.split('\n')
def steps = ports.collectEntries, names.collectEntries { port, name ->
["UI Test on $name", {
sh "#!/bin/bash -lx \n someMoreShellStuff --params=port=$port"
}]
}
parallel steps
}
但我不能collectEntries
用逗号分隔我的第二个,因为它给了我一个语法错误。现在我的问题是,如何在同一个命令中使用这个变量。甚至可能吗?
谢谢
更新#1
使用 Szymon Stepniak 的答案后,我的新代码如下所示:
stage('Test') {
// Reading content of the file
def portsFileContent = readFile 'AppiumUsedPorts.txt'
// Split the file by next line
def ports = portsFileContent.split('\n')
// Getting device IDs to get properties of device
def deviceIDFileContent = readFile 'DeviceIDs.txt'
def deviceIDs = deviceIDFileContent.split('\n')
// Define port and id as an pair
def pairs = (0..Math.min(ports.size(), deviceIDs.size())).collect { i -> [id: deviceIDs[i], port: ports[i]] }
def steps = pairs.collectEntries { pair ->
["UI Test on ${pair.id}", {
sh "echo 'Running test with port ${pair.port}'"
}]
}
parallel steps
}
这导致了错误java.lang.ArrayIndexOutOfBoundsException
更新#2
内容AppiumUsedPorts.txt
:
4723
4733
内容DeviceIDs.txt
5353352c
G000KU0663550R92