I would like the ability to have stage names be dynamic strings. This is useful, for instance, when defining stages within a matrix, so that the stage names can contain the values of the axes. (Part of the problem here is that Blue Ocean doesn't render matrixes well at all.) Right now attempting to do this yields an error because stage names can only be string literals, not Groovy strings (interpolation).

      Note that I am not looking for the steps within the stage to be dynamic.

      Example:

      matrix {
          axes {
              axis {
                  name 'PLATFORM'
                  values 'linux', 'mac', 'windows'
              }
          }
          stages {
              stage("Build ${PLATFORM}") {
                  // ...
              }
              stage("Test ${PLATFORM}") {
                  // ...
              }
              stage("Deploy ${PLATFORM}") {
                  // ...
              }
          }
      }
      

          [JENKINS-61280] Allow dynamic stage names

          This issue making some problems in many pipelines I manage ... and till it's fixed in the upstream I've implemented a custom step dynamicMatrix.

          That custom step mimics Jenkins declarative matrix but with better visualization and extra customization options. It makes it easier to run a parallel matrix with different combinations and groups.

          Here is an example:

          stage('single_matrix') {
              steps {
                  dynamicMatrix([
                      failFast: false,
                      axes: [
                          PLATFORM: ['linux', 'mac', 'windows'],
                          BROWSER: ['chrome', 'firefox', 'safari']
                      ],
                      actions: {
                          stage("${PLATFORM}_${BROWSER}") {
                              sh 'echo ${PLATFORM} - ${BROWSER}'
                              sh 'echo ${MATRIX_STAGE_NAME}'
                          }
                      }
                  ])
              }
          }
          

          It also has 2 other varieties to work with different combinations and groups.

          If you have different axes combinations but the same stages then dynamicMatrixMultiCombinations could be used for that case.

          stage('multi_combinations_matrix') {
              steps {
                  dynamicMatrixMultiCombinations([
                      failFast: false,
                      axes: [
                          [
                              PLATFORM: ['linux', 'mac', 'windows'],
                              BROWSER: ['chrome', 'firefox']
                          ],
                          [
                              PLATFORM: ['mac', 'windows']
                              BROWSER: ['safari'],
                          ],
                      ],
                      actions: {
                          stage("${PLATFORM}_${BROWSER}") {
                              sh 'echo ${PLATFORM}, ${BROWSER}'
                              sh 'echo ${MATRIX_STAGE_NAME}'
                          }
                      }
                  ])
              }
          }
          

          If you have different groups or different stages that should run in parallel at the same time, then dynamicMatrixMultiGroups could be used for that case.

          stage('multi_groups_matrix') {
              steps {
                  dynamicMatrixMultiGroups([
                      [
                          failFast: false,
                          axes: [
                              PLATFORM: ['windows']
                              BROWSER: ['edge', 'safari'],
                          ],
                          actions: {
                              stage("${PLATFORM}_${BROWSER}") {
                                  bat 'echo this is a Windows command'
                              }
                          }
                      ],
                      [
                          failFast: false,
                          axes: [
                              PLATFORM: ['linux', 'mac'],
                              BROWSER: ['chrome', 'firefox']
                          ],
                          actions: {
                              stage("${PLATFORM}_${BROWSER}") {
                                  sh 'echo this is a Unix-like command'
                              }
                          }
                      ]
                  ])
              }
          }
          

          Enjoy

          Ahmed AbouZaid added a comment - This issue making some problems in many pipelines I manage ... and till it's fixed in the upstream I've implemented a custom step dynamicMatrix . That custom step mimics Jenkins declarative matrix but with better visualization and extra customization options. It makes it easier to run a parallel matrix with different combinations and groups. Here is an example: stage( 'single_matrix' ) { steps { dynamicMatrix([ failFast: false , axes: [ PLATFORM: [ 'linux' , 'mac' , 'windows' ], BROWSER: [ 'chrome' , 'firefox' , 'safari' ] ], actions: { stage( "${PLATFORM}_${BROWSER}" ) { sh 'echo ${PLATFORM} - ${BROWSER}' sh 'echo ${MATRIX_STAGE_NAME}' } } ]) } } It also has 2 other varieties to work with different combinations and groups. If you have different axes combinations but the same stages then dynamicMatrixMultiCombinations could be used for that case. stage( 'multi_combinations_matrix' ) { steps { dynamicMatrixMultiCombinations([ failFast: false , axes: [ [ PLATFORM: [ 'linux' , 'mac' , 'windows' ], BROWSER: [ 'chrome' , 'firefox' ] ], [ PLATFORM: [ 'mac' , 'windows' ] BROWSER: [ 'safari' ], ], ], actions: { stage( "${PLATFORM}_${BROWSER}" ) { sh 'echo ${PLATFORM}, ${BROWSER}' sh 'echo ${MATRIX_STAGE_NAME}' } } ]) } } If you have different groups or different stages that should run in parallel at the same time, then dynamicMatrixMultiGroups could be used for that case. stage( 'multi_groups_matrix' ) { steps { dynamicMatrixMultiGroups([ [ failFast: false , axes: [ PLATFORM: [ 'windows' ] BROWSER: [ 'edge' , 'safari' ], ], actions: { stage( "${PLATFORM}_${BROWSER}" ) { bat 'echo this is a Windows command' } } ], [ failFast: false , axes: [ PLATFORM: [ 'linux' , 'mac' ], BROWSER: [ 'chrome' , 'firefox' ] ], actions: { stage( "${PLATFORM}_${BROWSER}" ) { sh 'echo this is a Unix-like command' } } ] ]) } } Enjoy

          aabouzaid looks the library still has issues 

           
          08:14:57 org.jenkinsci.plugins.workflow.cps.CpsCompilationErrorsException: startup failed:08:14:57 /jenkins/rocmhead-jenkins/jobs/maintenance/jobs/pipeline-syntax/builds/32/libs/c33ce574fce29a3d6c07d8cfea025972d7308787b91e72d78da8f772c7b63863/vars/dynamicMatrix.groovy: 109: expecting '}', found '' @ line 109, column 10.08:14:57 echo "${stepName} Invalid parameter '${parameter}'. Available parameter are: ${availableParameters}"

          omkar kakarparthi added a comment - aabouzaid  looks the library still has issues    08:14:57 org.jenkinsci.plugins.workflow.cps.CpsCompilationErrorsException: startup failed: 08:14:57 /jenkins/rocmhead-jenkins/jobs/maintenance/jobs/pipeline-syntax/builds/32/libs/c33ce574fce29a3d6c07d8cfea025972d7308787b91e72d78da8f772c7b63863/vars/dynamicMatrix.groovy: 109: expecting '}', found '' @ line 109, column 10. 08:14:57 echo "${stepName} Invalid parameter '${parameter}'. Available parameter are: ${availableParameters}"

          omkar143 I've fixed it
          Please give it another try now.

          Ahmed AbouZaid added a comment - omkar143 I've fixed it Please give it another try now.

          Kohen Chia added a comment -

          Upvoted. Please add this feature. I'm begging you.

          Kohen Chia added a comment - Upvoted. Please add this feature. I'm begging you.

          Neha Mittal added a comment -

          Hi bitwiseman ,

          Are we getting  fix with this issue as we are blocked and cannot use this. Please let us know when is ETA planned.

          Regards,

          Neha. 

          Neha Mittal added a comment - Hi bitwiseman , Are we getting  fix with this issue as we are blocked and cannot use this. Please let us know when is ETA planned. Regards, Neha. 

          John Pfuntner added a comment - - edited

          I'm dealing with a project with several Jenkins scripts - some of them appear to accept a variable in the stage.  I don't understand why but it seems that if the stage statement is inside a script block, you can specify a variable in the stage name:

          stages {
            stage('static name') {
              steps {
                script {
                  stage("dynamic name with ${ENV.name}") {
                    .
                    .
                    .
                  }
                }
              }
            }
          }
          

          John Pfuntner added a comment - - edited I'm dealing with a project with several Jenkins scripts - some of them appear to accept a variable in the stage.  I don't understand why but it seems that if the stage statement is inside a script block, you can specify a variable in the stage name: stages { stage('static name') { steps { script { stage("dynamic name with ${ENV.name}") { . . . } } } } }

          Jesse Rittner added a comment -

          pfuntner What you are seeing is declarative vs. scripted pipelines. Generally declarative pipelines have more constraints, but you can use a script block as an escape hatch to go from declarative to scripted. The stage block in a declarative pipeline and the stage function in a scripted pipeline are analogous to each other but are not entirely equivalent.

          Jesse Rittner added a comment - pfuntner What you are seeing is declarative vs. scripted pipelines. Generally declarative pipelines have more constraints, but you can use a script block as an escape hatch to go from declarative to scripted. The stage block in a declarative pipeline and the stage function in a scripted pipeline are analogous to each other but are not entirely equivalent.

          john added a comment -

          Hi
          I just hit this issue while converting some jobs to declarative pipeline / matrix. Makes the matrix visualization really confusing for users.
          Is someone actually looking at this issue, or should I assume it's never going to be fixed, since it has been reported 2 years ago ?
          Thanks

          john added a comment - Hi I just hit this issue while converting some jobs to declarative pipeline / matrix. Makes the matrix visualization really confusing for users. Is someone actually looking at this issue, or should I assume it's never going to be fixed, since it has been reported 2 years ago ? Thanks

          sascha inu added a comment -

          Actual "Matrix..." will displayed at the beginning of every matrix line.
          I think is would by enough, when there a properly name will displayed.


          "Matrix - app = 'app1', stg = 'dev'" is display as popup, actual. so all the data is there, only hidden.

          To set new name, something like "display_name" can be used:

          matrix {
               axes {
                  axis {
                       name 'app'
                       values 'app1', 'app2'
                   }
                   axis {
                       name 'stg'
                       values 'tst', 'dev'
                    }
                }
               display_name "${app} - ${stg}"
               ...
          

          sascha inu added a comment - Actual "Matrix..." will displayed at the beginning of every matrix line. I think is would by enough, when there a properly name will displayed. "Matrix - app = 'app1', stg = 'dev'" is display as popup, actual. so all the data is there, only hidden. To set new name, something like "display_name" can be used: matrix {      axes {         axis {              name 'app'              values 'app1' , 'app2'          }          axis {              name 'stg'              values 'tst' , 'dev'           }       }      display_name "${app} - ${stg}"      ...

          christian added a comment -

          I would also be for the last idea, maybe even simpler. instead of displaying Matrix... you could just make the display field so big that all strings are shown Matrix = 'XXX'.

          christian added a comment - I would also be for the last idea, maybe even simpler. instead of displaying Matrix... you could just make the display field so big that all strings are shown Matrix = 'XXX'.

            bitwiseman Liam Newman
            rittneje Jesse Rittner
            Votes:
            58 Vote for this issue
            Watchers:
            58 Start watching this issue

              Created:
              Updated: