1

I'm trying to write a declarative pipeline code that accepts a map and create a pipeline. I can able to achieve sequential stages or parallel stages but facing problems while making a pipeline that contains sequential stages inside parallel stages.

The input data would be Map. Each list in the map should run parallel and the items inside the list corresponding to each key should run in sequentially.

example data : [1:[11,12], 2:[21,22], 3:[31,32]]

Am using Jenkins 2.164.3

The output should be

enter image description here

1 Answer 1

2

This was covered in an official blog post when the feature to nest stages inside a parallel block was added.

Here is the example copy-pasted from the blog post. In this example, the Windows and Linux blocks are run in parallel, but the stages within each block are run sequentially:

pipeline {
    agent none

    stages {
        stage("build and deploy on Windows and Linux") {
            parallel {
                stage("windows") {
                    agent {
                        label "windows"
                    }
                    stages {
                        stage("build") {
                            steps {
                                bat "run-build.bat"
                            }
                        }
                        stage("deploy") {
                            when {
                                branch "master"
                            }
                            steps {
                                bat "run-deploy.bat"
                            }
                        }
                    }
                }

                stage("linux") {
                    agent {
                        label "linux"
                    }
                    stages {
                        stage("build") {
                            steps {
                                sh "./run-build.sh"
                            }
                        }
                        stage("deploy") {
                             when {
                                 branch "master"
                             }
                             steps {
                                sh "./run-deploy.sh"
                            }
                        }
                    }
                }
            }
        }
    }
}

EDIT: I missed that you're trying to do this based on arbitrary input. If you want to do that, you must use scripted Pipelines (or a script nested in a Declarative Pipeline). Plain Declarative Pipelines don't support that kind of runtime-defined Pipelines.

3
  • Even in the case of scripted pipeline, is it possible to have nested inside parallel. The parallel accepts Map<String, closure> . I need like Map<String, closure<inner stages>>
    – june
    May 21, 2019 at 10:34
  • Yes, it is possible. I've done it several times and there are a few questions on this site about that topic. A closure is an arbitrary block of code so you can put anything you want in it, including stage definitions.
    – jayhendren
    May 21, 2019 at 16:14
  • 1
    Can you share a piece of code?
    – june
    May 24, 2019 at 12:29

Your Answer

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

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