# Dynamic number of stage

**URL:** <https://community.jenkins.io/t/dynamic-number-of-stage/5956>\
**Category:** Using Jenkins\
**Created:** [February 24, 2023, 3:47pm UTC](https://community.jenkins.io/t/dynamic-number-of-stage/5956 "2023-02-24T15:47:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Toug](https://avatars.discourse-cdn.com/v4/letter/t/4da419/32.png) [@Toug](https://community.jenkins.io/u/Toug)\
**Post date:** [February 24, 2023, 3:47pm UTC](https://community.jenkins.io/t/dynamic-number-of-stage/5956/1 "2023-02-24T15:47:09Z")

</div>

Hi,  
I would like to change the number of stage and that some of them would be parallel.  
Currently I have this script, the important is a the end in the “stage(‘Tests’)” with the test category 1 and category 2.

```auto
pipeline {
    agent { label 'my_agent'}
    stages {
        stage('stage_1') {
            steps {
                script {
                    sh '''
                    echo "stage_1"
                    '''
                }
            }
        }
        stage('stage_2') {
            agent { node { label 'agent_stage2' } }
                when {
                    expression {
                        return (params.PREBUILT_BIN_PATH != '');
                    }
                }
                steps {
                    dir ('client_binaries') {
                        script {
                            if (!clientBinariesExist(VERSION)) {
                                logger.info()
                                sh '''
                                echo 
                                '''
                            }
                            logger.info("stage_2")
                        }
                    }
                }
        }
        stage('Tests') {
            parallel {
                stage('tests category 1') {
                    steps {
                        dir('build_ct_m') {
                            script {
                                println("Running test cat 1")
                            }
                        }
                    }
                }
                stage('tests category 2') {
                    steps {
                        dir('build_ct_m') {
                            script {
                                println("Running test cat 2")
                            }
                        }
                    }
                }
            }
        }
    }
}

```

Currently test category 1 and 2 are well run in parallel. But now I would like to have a dynamic number of test (ie dynamic number of stage ‘test category X’) that will depend on a given parameter given to the job.  
I tried for for loop after the parallel keyword, but groovy was complaining that he wants a stage declaration at this point.

Thank.

---

<div class="post-metadata">

**Author:** ![slide\_o\_mix](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/slide_o_mix/32/20977_2.png) [@slide\_o\_mix](https://community.jenkins.io/u/slide_o_mix)\
**Post date:** [February 24, 2023, 4:53pm UTC](https://community.jenkins.io/t/dynamic-number-of-stage/5956/2 "2023-02-24T16:53:49Z")

</div>

I don’t think you can do dynamic stages in declarative, only scripted. I think I am understanding what you want, but maybe you can describe it a bit more?

---

<div class="post-metadata">

**Author:** ![Toug](https://avatars.discourse-cdn.com/v4/letter/t/4da419/32.png) [@Toug](https://community.jenkins.io/u/Toug)\
**Post date:** [February 27, 2023, 7:33am UTC](https://community.jenkins.io/t/dynamic-number-of-stage/5956/3 "2023-02-27T07:33:49Z")

</div>

I would like to run some identical stages in parallel mode and the number of them depends on a variable (parameter given to the pipeline).  
I managed to get the variable number of tests, but these ones are not started in parallel, there are lunch in sequential mode.

Here is my last code, here the stages “tests category 1” and “tests category 2” are well run in parallel. But the different iterations of each test category (ie '“Test CAT 1 iter ’ + i”) are run in sequential mode. I would like them to run in parallel mode also.

Thanks.

```auto
pipeline {
    agent { label 'my_agent'}
    stages {
        stage('stage_1') {
            steps {
                script {
                    sh '''
                    echo "stage_1"
                    '''
                }
            }
        }
        stage('stage_2') {
            agent { node { label 'agent_stage2' } }
                when {
                    expression {
                        return (params.PREBUILT_BIN_PATH != '');
                    }
                }
                steps {
                    dir ('client_binaries') {
                        script {
                            if (!clientBinariesExist(VERSION)) {
                                logger.info()
                                sh '''
                                echo 
                                '''
                            }
                            logger.info("stage_2")
                        }
                    }
                }
        }
        stage('Tests') {
            parallel {
                stage('tests category 1') {
                    steps {
                        dir('build_ct_CAT1') {
                            script {
								for(int i=1; i < params.CAT1_TESTRUN_NUMBER.toInteger() + 1; i++) {
									stage('Test CAT 1 iter ' + i) {
										println("Running test cat 1")
									}
								}
                            }
                        }
                    }
                }
                stage('tests category 2') {
                    steps {
                        dir('build_ct_CAT2') {
                            script {
								for(int j=1; j < params.CAT2_TESTRUN_NUMBER.toInteger() + 1; j++) {
									stage(''Test CAT 2 iter " + j) {
										println("Running test cat 1")
									}
								}
                            }
                        }
                    }
                }
            }
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![halkeye](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/halkeye/32/8_2.png) [@halkeye](https://community.jenkins.io/u/halkeye)\
**Post date:** [February 27, 2023, 7:58am UTC](https://community.jenkins.io/t/dynamic-number-of-stage/5956/4 "2023-02-27T07:58:10Z")

</div>

Declarative pipeline (ones that start with pipeline) are intended to be fully parable before the job starts.

What you are describing is not only doable but by design and documented in scripted pipelines

---

<div class="post-metadata">

**Author:** ![Toug](https://avatars.discourse-cdn.com/v4/letter/t/4da419/32.png) [@Toug](https://community.jenkins.io/u/Toug)\
**Post date:** [February 27, 2023, 12:39pm UTC](https://community.jenkins.io/t/dynamic-number-of-stage/5956/5 "2023-02-27T12:39:42Z")

</div>

Sorry, I’m not getting it.

---

<div class="post-metadata">

**Author:** ![PierreBtz](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/pierrebtz/32/55_2.png) [@PierreBtz](https://community.jenkins.io/u/PierreBtz)\
**Post date:** [March 3, 2023, 2:56pm UTC](https://community.jenkins.io/t/dynamic-number-of-stage/5956/6 "2023-03-03T14:56:33Z")

</div>

What Gavin explained is that you cannot use a Declarative Pipeline since, by design, it cannot be dynamic (everything needs to be known at the start).  
But you can use a Scripted Pipeline instead. The `parallel` step takes a map ([Pipeline: Groovy](https://www.jenkins.io/doc/pipeline/steps/workflow-cps/#parallel-execute-in-parallel)) so you can create the map dynamically and pass it to the `parallel` step. A quick example:

```auto
def branches = [
    'a branch': aBranch(),
    'another branch': anotherBranch()
]

parallel branches

def aBranch() {
    return {
        node {
            sh 'echo "hello from branch 1"'
        }
    }
}

def anotherBranch() {
    return {
        node {
            sh 'echo "hello from branch 2"'
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![Toug](https://avatars.discourse-cdn.com/v4/letter/t/4da419/32.png) [@Toug](https://community.jenkins.io/u/Toug)\
**Post date:** [March 9, 2023, 2:26pm UTC](https://community.jenkins.io/t/dynamic-number-of-stage/5956/7 "2023-03-09T14:26:15Z")

</div>

Thanks all for you help.
