0
votes

I want to execute multiple parallel steps in my Jenkins pipeline. All variables I need are given through ArrayLists. Now I want to build the Code via string builder and a for loop. After that I want to execute the String as Code

Pseudocode:

sb << try{ \n

for(i=0; i<TMP; i++) {
    sb <<
    parallel( 
    build VARIABLE{
        def BUILDJOBNAME = build job: BUILDJOBVARIABLE, parameters:
                [
                      string(name: 'parametername', value: PARAMETER)
                ]
    }
}

sb << catch(e){
 (...)
}

But how am I able to execute this? I tried GroovyShell.evaluate(sb.toString()) But this results in the following error:

hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: java.lang.Class.evaluate() is applicable for argument types: (java.lang.String)

1
GroovyShell.evaluate is an instance method rather than class method. - aristotll

1 Answers

1
votes

We use a Groovy hash to define the parallel steps and execute them in one parallel statement:

def tests = ["test1", "test2", "test3", "test4"];

try {
    def branches = [:];
    for (int i = 0; i < tests.size(); i++) {
        def test = tests[i];
        branches["$test"] = {
            try
            {
                env.test = test;
                build job: 'BUILD_JOB', parameters: [string(name: 'NAME', value: test)]
            }
            catch (err)
            {
                currentBuild.result = 'FAILURE';
            }
        }
    }

    parallel branches
}

Each element in the branches hash is one parallel execution path.