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

`when` only stores TagsAction for first occurrence of stage name (matrix limitation)

      see https://github.com/jenkinsci/pipeline-graph-view-plugin/issues/220

      specifically https://github.com/jenkinsci/pipeline-model-definition-plugin/blob/76afff0a26188ad6ced215e4663cd2115c003a74/pipeline-model-definition/src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/Utils.groovy#L240

      Stage name and ForkScanner is used rather than passing flownodes around.
      It just picks the first stage rather than the correct one.

      This can only happen in matrix pipelines.
      Regular declarative pipelines have this behaviour blocked:

      20:04:25  org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
      20:04:25  WorkflowScript: 4: Duplicate stage name: "Skipped" @ line 4, column 5.
      20:04:25         stages {
      

      but matrix can have duplicates:

      pipeline {
          agent none
          stages {
              stage('BuildAndTest') {
                  matrix {
                      agent any
                      axes {
                          axis {
                              name 'PLATFORM'
                              values 'linux', 'windows'
                          }
                          axis {
                              name 'BROWSER'
                              values 'firefox'
                          }
                      }
                      stages {
                          stage('Build') {
                              when {
                                  branch 'testing'
                              }
                              steps {
                                  echo "Do Build for ${PLATFORM} - ${BROWSER}"
                              }
                              
                          }
                      }
                  }
              }
          }
      }
      

      Looks like BlueOcean didn't fully switch to using TagsAction and falls back to not built if nothing is set.

          [JENKINS-72841] `when` only stores TagsAction for first occurrence of stage name (matrix limitation)

          Tim Jacomb added a comment -

          I'm a bit stumped on a fix here, jglick or dnusbaum any idea?

          Best I can come up with is maybe some sort of ID can be added here:
          https://github.com/jenkinsci/pipeline-model-definition-plugin/blob/c9d1c49f4e9db93535cab33ac0d2f45821b45eaa/pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy#L302

          If this was a running pipeline with regular Jenkins code it would be straightforward, but declarative makes this a lot harder =/

          Tim Jacomb added a comment - I'm a bit stumped on a fix here, jglick or dnusbaum any idea? Best I can come up with is maybe some sort of ID can be added here: https://github.com/jenkinsci/pipeline-model-definition-plugin/blob/c9d1c49f4e9db93535cab33ac0d2f45821b45eaa/pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy#L302 If this was a running pipeline with regular Jenkins code it would be straightforward, but declarative makes this a lot harder =/

          Tim Jacomb added a comment -

          I experimented with https://github.com/jenkinsci/pipeline-model-definition-plugin/compare/master...timja:pipeline-model-definition-plugin:JENKINS-72841-pass-parent?expand=1

          Which passes the parent stage down and correctly finds its flownode, but when using ForkScanner it doesn't find the actual stage.

          any idea canuck1987 if you have a chance since you understand the flowgraph better than me

          Tim Jacomb added a comment - I experimented with https://github.com/jenkinsci/pipeline-model-definition-plugin/compare/master...timja:pipeline-model-definition-plugin:JENKINS-72841-pass-parent?expand=1 Which passes the parent stage down and correctly finds its flownode, but when using ForkScanner it doesn't find the actual stage. any idea canuck1987 if you have a chance since you understand the flowgraph better than me

          Tim Jacomb added a comment -

          Tim Jacomb added a comment - This explains the walking in that it does it in reverse order from the head passed to it: https://github.com/jenkinsci/workflow-api-plugin/blob/51fd2a625da7648f65f02e4e440a12a46bae0724/src/main/java/org/jenkinsci/plugins/workflow/graphanalysis/AbstractFlowScanner.java#L59

          Tim Jacomb added a comment -

          I figured out an approach.

          First find all potential matches.
          Then walk the DAG back using forkscanner which will walk back using parentage.
          Stop searching if the parent is found, otherwise keep iterating over potential matches.

          Tim Jacomb added a comment - I figured out an approach. First find all potential matches. Then walk the DAG back using forkscanner which will walk back using parentage. Stop searching if the parent is found, otherwise keep iterating over potential matches.

          Devin Nusbaum added a comment - - edited

          Yeah, I also noticed this when trying to fix problems with ForkScanner. See https://github.com/jenkinsci/workflow-api-plugin/pull/305#issuecomment-1696291234. Perhaps ModelInterpreter.groovy is one situation where the getContext step could actually be useful and has no real alternative given the way the DSL and when conditions were implemented. The idea would be to make a @NonCPS method that returns getContext(FlowNode.class).getId(), insert a call to that method in cases like between these two lines, and then use that ID as a starting point for scans to avoid some of the extra work and because the code clearly wants to be using FlowNode IDs and not stage names.

          Devin Nusbaum added a comment - - edited Yeah, I also noticed this when trying to fix problems with ForkScanner . See https://github.com/jenkinsci/workflow-api-plugin/pull/305#issuecomment-1696291234 . Perhaps ModelInterpreter.groovy is one situation where the getContext step could actually be useful and has no real alternative given the way the DSL and when conditions were implemented. The idea would be to make a @NonCPS method that returns getContext(FlowNode.class).getId() , insert a call to that method in cases like between these two lines , and then use that ID as a starting point for scans to avoid some of the extra work and because the code clearly wants to be using FlowNode IDs and not stage names.

          Tim Jacomb added a comment -

          Simpler alternative created using Devin's suggestion: https://github.com/jenkinsci/pipeline-model-definition-plugin/pull/700

          Tim Jacomb added a comment - Simpler alternative created using Devin's suggestion: https://github.com/jenkinsci/pipeline-model-definition-plugin/pull/700

            timja Tim Jacomb
            timja Tim Jacomb
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: