<p>I'm not really sure I understand your question, but you can execute arbitrary Groovy inside of a <code>script</code> block, so you can do just about anything within <code>script</code>.</p>
<p>For example, here is my interpretation of what you are asking for:</p>
<pre><code>stage ('Execute jobs') {
    steps {
        node('node1') {
            script {
                parallel_jobs = [:]
                if (params.includeJob1 == &quot;true&quot;) {
                    parallel_jobs['ci'] = {
                        build job: job1, parameters: [], propagate: true
                    }
                }
                parallel_jobs['second'] = {
                    build job: job2, parameters: [], propagate: true
                }

                // ...
                // and more jobs dinamically added to the parallel_jobs list
                // ... parallel_jobs['etc'] = ...

                if (params.parallel) {
                    // execute jobs in parallel
                    parallel(parallel_jobs)
                } else {
                    // execute jobs in sequence
                    parallel_jobs.each { name, closure-&gt;
                        closure.call()
                    }
                }
            }
        }
    }
}
</code></pre>
<p>In Groovy, Maps (i.e. associative arrays) are ordered.  So if you use this to execute the builds serially rather than in parallel, the builds will be executed in the order they are defined.</p>

CC BY-SA 4.0
2020-07-17T21:21:29.200Z
That's exactly what I was looking for, it works perfect, and in the expected order (FIFO).
Alejandro Décimo
CC BY-SA 4.0
2020-07-24T15:25:59.023Z
@AlejandroDécimo as per your prior comment: please mark this answer as accepted ...
Pierre.Vriens
CC BY-SA 4.0
2020-07-05T23:32:54.530Z
2020-07-05T23:32:54.530Z
jayhendren
<p>Context: I have a long dynamic list of builds calls that I am currently executing in parallel successfully. Example below.</p>
<p>Problem: In some specific moments it is required to run that list of builds but in a sequential fashion.</p>
<p>Question: Is there any way to reuse the list of jobs that I already have generated for the parallel execution, but to execute them in a sequential way?</p>
<pre><code>stage ('Execute jobs') {
    steps {
        node('node1') {
            script {
                parallel_jobs = [:]
                if (params.includeJob1 == &quot;true&quot;) {
                    parallel_jobs['ci'] = {
                        build job: job1, parameters: [], propagate: true
                    }
                }
                parallel_jobs['second'] = {
                    build job: job2, parameters: [], propagate: true
                }

                // ...
                // and more jobs dinamically added to the parallel_jobs list
                // ... parallel_jobs['etc'] = ...

                parallel parallel_jobs
            }
        }
    }
}
</code></pre>
<p>I am thinking to replace the 'parallel' call for any other Jenkins command, but cannot find the correct one at docs (<a href="https://www.jenkins.io/doc/book/pipeline/syntax/#parallel" rel="nofollow noreferrer">https://www.jenkins.io/doc/book/pipeline/syntax/#parallel</a>).</p>
<p>Thank you.</p>

<jenkins-pipeline><automation><job-dsl>
Jenkins Declarative Pipeline: Can Jenkins execute a dynamic list of jobs?
Alejandro Décimo
# Jenkins Declarative Pipeline: Can Jenkins execute a dynamic list of jobs?

(Asked by: username_0 on 2020-07-03)

Context: I have a long dynamic list of builds calls that I am currently executing in parallel successfully. Example below.
Problem: In some specific moments it is required to run that list of builds but in a sequential fashion.
Question: Is there any way to reuse the list of jobs that I already have generated for the parallel execution, but to execute them in a sequential way?
stage ('Execute jobs') {
    steps {
        node('node1') {
            script {
                parallel_jobs = [:]
                if (params.includeJob1 == "true") {
                    parallel_jobs['ci'] = {
                        build job: job1, parameters: [], propagate: true
                    }
                }
                parallel_jobs['second'] = {
                    build job: job2, parameters: [], propagate: true
                }

                // ...
                // and more jobs dinamically added to the parallel_jobs list
                // ... parallel_jobs['etc'] = ...

                parallel parallel_jobs
            }
        }
    }
}

I am thinking to replace the 'parallel' call for any other Jenkins command, but cannot find the correct one at docs (https://www.jenkins.io/doc/book/pipeline/syntax/#parallel).
Thank you.

**Tags:** jenkins-pipeline, automation, job-dsl


---

## 1 answers

### Answer by username_1 on 2020-07-05. Score: 2 | Accepted answer

I'm not really sure I understand your question, but you can execute arbitrary Groovy inside of a script block, so you can do just about anything within script.
For example, here is my interpretation of what you are asking for:
stage ('Execute jobs') {
    steps {
        node('node1') {
            script {
                parallel_jobs = [:]
                if (params.includeJob1 == "true") {
                    parallel_jobs['ci'] = {
                        build job: job1, parameters: [], propagate: true
                    }
                }
                parallel_jobs['second'] = {
                    build job: job2, parameters: [], propagate: true
                }

                // ...
                // and more jobs dinamically added to the parallel_jobs list
                // ... parallel_jobs['etc'] = ...

                if (params.parallel) {
                    // execute jobs in parallel
                    parallel(parallel_jobs)
                } else {
                    // execute jobs in sequence
                    parallel_jobs.each { name, closure->
                        closure.call()
                    }
                }
            }
        }
    }
}

In Groovy, Maps (i.e. associative arrays) are ordered.  So if you use this to execute the builds serially rather than in parallel, the builds will be executed in the order they are defined.

> **Comments:**
> **username_0**: That's exactly what I was looking for, it works perfect, and in the expected order (FIFO).  
> **username_2**: @AlejandroDécimo as per your prior comment: please mark this answer as accepted ...  

---

