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 Answers1

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.

jayhendren
  • 3,022
  • 8
  • 16