Combining multiple collectEntries in Groovy and Jenkins Pipeline

I am trying to use somewhat collectEntriesconsistently in my Groovy script. Its better to see the code, right now I have:

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
        }

The "UsedPorts.txt" file contains different ports, separated by a line break, for example:

4723
4733
4743

Thus, these numbers are stored in a variable ports, and this variable is then used to start a server instance for each port. Thus, in this case, after this the command starts 3 different servers:

def steps = ports.collectEntries { port ->
                ["UI Test on port $port", {
                    sh "#!/bin/bash -lx \n startServerWithDifferentPort --params=port=$port"
                }]
            }
            parallel steps

Due to parallel stepsits launch, 3 server instances simultaneously with different ports.

Works well, but I have a different file, and I need to do the same. Therefore, in my second file there are entries such as:

name1
name2
name3

I again created a variable in which I store my 3 entries:

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
+4
1

, - ports names . , ports names - :

def ports = [8080, 8081, 8082, 8083]
def names = ['Host A', 'Host B', 'Host C', 'Host D', 'Host E']

:

def pairs = [[port: 8080, name: 'Host A'], [port: 8081, name: 'Host B'], [port: 8082, name: 'Host C'], [port:8083, 'Host D']]

, , .

Groovy GroovyCollections.transpose(List lists), (, [[8080, 8081, 8082, 8083], ['Host A', 'Host B', 'Host C', 'Host D', 'Host E']]) "zips" :

[[8080, 'Host A'], [8081, 'Host B'], [8082, 'Host C'], [8083, 'Host D']]

Jenkins Pipeline - , :

org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods transpose java.util.List

, collect 0 min(ports.size(), names.size()), /. :

node {
    stage('Test') {
        def ports = [8080, 8081, 8082, 8083]
        def names = ['Host A', 'Host B', 'Host C', 'Host D', 'Host E']

        def pairs = (0..<Math.min(ports.size(), names.size())).collect { i -> [name: names[i], port: ports[i]] }

        def steps = pairs.collectEntries { pair ->
            ["UI Test on ${pair.name}", {
                sh "echo 'Running test with port ${pair.port}'"
            }]
        }

        parallel steps
    }
}

[port: ..., name: ...], collectEntries , - . script Jenkins Pipeline :

[Pipeline] {
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] parallel
[Pipeline] [UI Test on Host A] { (Branch: UI Test on Host A)
[Pipeline] [UI Test on Host B] { (Branch: UI Test on Host B)
[Pipeline] [UI Test on Host C] { (Branch: UI Test on Host C)
[Pipeline] [UI Test on Host D] { (Branch: UI Test on Host D)
[Pipeline] [UI Test on Host A] sh
[UI Test on Host A] [test-pipeline] Running shell script
[Pipeline] [UI Test on Host B] sh
[UI Test on Host B] [test-pipeline] Running shell script
[Pipeline] [UI Test on Host C] sh
[UI Test on Host C] [test-pipeline] Running shell script
[Pipeline] [UI Test on Host D] sh
[UI Test on Host A] + echo Running test with port 8080
[UI Test on Host A] Running test with port 8080
[UI Test on Host B] + echo Running test with port 8081
[UI Test on Host B] Running test with port 8081
[UI Test on Host D] [test-pipeline] Running shell script
[Pipeline] [UI Test on Host A] }
[UI Test on Host C] + echo Running test with port 8082
[UI Test on Host C] Running test with port 8082
[UI Test on Host D] + echo Running test with port 8083
[UI Test on Host D] Running test with port 8083
[Pipeline] [UI Test on Host B] }
[Pipeline] [UI Test on Host C] }
[Pipeline] [UI Test on Host D] }
[Pipeline] // parallel
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

, .

+3
source

Source: https://habr.com/ru/post/1692088/


All Articles