Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-58236

Provide mechanism to define dynamic stages in declarative pipelines

    Details

    • Similar Issues:

      Description

      Hello, it would be great if we had a mechanism to define dynamic stages within declarative pipelines. The use case is that I want to create a pipeline function for micro service repositories. Each repository contains a number of container images that needs to be built in parallel. The only way I found is using the these pseudo stages wrapped by the `parallel` step:

      pipeline {
        agent any
        stages {
          stage('build') {
            steps {
              runParallel items: ("a".."f").collect { "Stage ${it}" }
            }
          }
        }
      }
      
      def runParallel(args) {
        parallel args.items.collectEntries { name -> [ "${name}": {
          stage("${name}") {
            echo name
          }
        }]}
      }
      

      This is bad because the sub-stages can neither have an agent nor contain other sub-stages. And it requires a separate function (I couldn't get it working without).
      On the other hand, this shouldn't make the declarative pipeline too scripty. A minimal scriptiness approach would look like this:

      pipeline {
        agent none
        stages {
          stage('build') {
            stages {
              ("A".."F").collect {
                stage("Stage ${it}") {
                  agent any
                  steps {
                    echo "Hello from stage ${it}!"
                  }
                }
              }
            }
          }
        }
      }
      

        Attachments

          Activity

          hendrikhalkow Hendrik Halkow created issue -
          hendrikhalkow Hendrik Halkow made changes -
          Field Original Value New Value
          Description Hello, it would be great if we had a mechanism to define dynamic stages within declarative pipelines. The use case is that I want to create a pipeline function for micro service repositories. Each repository contains a number of container images that needs to be built in parallel. The only way I found is using the these pseudo stages wrapped by the `parallel` step:

          {code}
          pipeline {
            agent any
            stages {
              stage('build') {
                steps {
                  runParallel items: ("a".."f").collect { "Stage ${it}" }
                }
              }
            }
          }

          def runParallel(args) {
            parallel args.items.collectEntries { name -> [ "${name}": {
              stage("${name}") {
                echo name
              }
            }]}
          }
          {code}

          This is bad because the sub-stages can neither have an agent nor contain other sub-stages. And it requires a
          On the other hand, this shouldn't make the declarative pipeline too scripty. A minimal scriptiness approach would look like this:

          {code}
          pipeline {
            agent none
            stages {
              stage('build') {
                stages {
                  ("A".."F").collect {
                    stage("Stage ${it}") {
                      agent any
                      steps {
                        echo "Hello from stage ${it}!"
                      }
                    }
                  }
                }
              }
            }
          }
          {code}
          Hello, it would be great if we had a mechanism to define dynamic stages within declarative pipelines. The use case is that I want to create a pipeline function for micro service repositories. Each repository contains a number of container images that needs to be built in parallel. The only way I found is using the these pseudo stages wrapped by the `parallel` step:

          {code}
          pipeline {
            agent any
            stages {
              stage('build') {
                steps {
                  runParallel items: ("a".."f").collect { "Stage ${it}" }
                }
              }
            }
          }

          def runParallel(args) {
            parallel args.items.collectEntries { name -> [ "${name}": {
              stage("${name}") {
                echo name
              }
            }]}
          }
          {code}

          This is bad because the sub-stages can neither have an agent nor contain other sub-stages. And it requires a separate function (I couldn't get it working without).
          On the other hand, this shouldn't make the declarative pipeline too scripty. A minimal scriptiness approach would look like this:

          {code}
          pipeline {
            agent none
            stages {
              stage('build') {
                stages {
                  ("A".."F").collect {
                    stage("Stage ${it}") {
                      agent any
                      steps {
                        echo "Hello from stage ${it}!"
                      }
                    }
                  }
                }
              }
            }
          }
          {code}
          abayer Andrew Bayer made changes -
          Assignee Andrew Bayer [ abayer ]

            People

            • Assignee:
              Unassigned
              Reporter:
              hendrikhalkow Hendrik Halkow
            • Votes:
              6 Vote for this issue
              Watchers:
              7 Start watching this issue

              Dates

              • Created:
                Updated: