1

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.

1 Answer 1

2

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.

2
  • That's exactly what I was looking for, it works perfect, and in the expected order (FIFO). Jul 17, 2020 at 21:21
  • @AlejandroDécimo as per your prior comment: please mark this answer as accepted ...
    – Pierre.Vriens
    Jul 24, 2020 at 15:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.