create nested parallel stages in a loop Jenkins declarative pipeline

2k Views Asked by At

I have list of items, each of them need to go through two stages, when for each of the items the stages must to be one after the other, but the items can run parallelly. So, I tried to create a nested parallel stages for every item in the list, so all the items will run parallelly, and for each of them the stages will run one after the other when I tried to do it without a loop (only for 2 items from my list) it worked this way:

stage('nested parallel stage') {
    parallel {
        stage("stages for item1") {
            steps {
                script {
                    stage("stage1 for item1 ") {
                        sh "echo stage 1 for item1"
                    } //stage 1
                    stage("stage 2 for item1") {
                        sh "echo stage 2 to item1"
                    } // stage 2
                } //script
            } // steps
        } // stage
        stage("stages for item2") {
            steps {
                script {
                    stage("staqge1 for item 2") {
                        sh "echo stage 1 for item2"
                    } //stage 1
                    stage("stage 2 for item2") {
                        sh "echo stage 2 to item2"
                    } // stage2
                } //script
            } // steps
        } // stage
    } //parallel
} // nested parallel stage

It worked without any problem. But, when I tried to do it in a loop, like this:

def itemsMap = getitemsMap ()
def parallelItemsStages = itemsMap.collectEntries{
    [ "${it.value.item}": {
            stage("stages for ${it.value.item}") {
                steps{
                    script {
                        stage("stage 1 for ${it.value.item}") {
                            sh "echo stage 1 for ${it.value.item}"
                        } //stage 1
                        stage("stage 2 for ${it.value.item}") {
                            sh "echo stage 2 for ${it.value.item}"
                        } // stage 2
                    } // script
                } // steps
            } // stage
        } // nested stages
    }] // collectEntries
} // collectEntries
parallel parallelItemsStages

I got this error:

ava.lang.NoSuchMethodError: No such DSL method 'steps' found among steps
2

There are 2 best solutions below

3
On

Trying to understand getitemsMap (); May I ask what is the role of def itemsMap = getitemsMap ()

0
On

try this:

def itemsMap = getitemsMap ()
def parallelItemsStages = itemsMap.collectEntries{
    [ "${it.value.item}": {
            script {
                stage("stage 1 for ${it.value.item}") {
                    sh "echo stage 1 for ${it.value.item}"
                } //stage 1
                stage("stage 2 for ${it.value.item}") {
                    sh "echo stage 2 for ${it.value.item}"
                } // stage 2
            } // script
        } // nested stages
    }] // collectEntries
} // collectEntries
parallel parallelItemsStages

that worked for me just now :)