I am trying to use multiple collectEntries
in series in my Groovy script. Its better to see the code, right now I've got:
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
}
In the file "UsedPorts.txt" are different ports seperate by line break like:
4723
4733
4743
So this numbers getting stored in the variable ports
and this variable is then used to start an instance of an server for each port. So in this case it would then start 3 different serverinstance by this command:
def steps = ports.collectEntries { port ->
["UI Test on port $port", {
sh "#!/bin/bash -lx \n startServerWithDifferentPort --params=port=$port"
}]
}
parallel steps
Because of parallel steps
its starting 3 instance of the server simultaneously with different ports.
Thats working fine, but I have another file and need to do the same again. So in my second file there are entries like:
name1
name2
name3
I created again a variable where I store my 3 entries:
def anotherFile = readFile 'Names.txt'
def names = anotherFile.split('\n')
This is what I tried:
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
}
But I can not seperate my second collectEntries
by comma, because it gives me a syntax error. And now my problem is, how I can use this variable in the same command. Is it even possible?
Thanks
Update #1
After using the answer of Szymon Stepniak my new code looks like this:
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
}
This is causing the error java.lang.ArrayIndexOutOfBoundsException
Update #2
Content of AppiumUsedPorts.txt
:
4723
4733
Content of DeviceIDs.txt
5353352c
G000KU0663550R92