How can I make Jenkins recognize a 'stage' node within a groovy method called from 'steps'?

160 Views Asked by At

I have a Jenkins pipeline supported by Groovy in the git SCM which has stage inside steps. Why I did it- To reuse the code. Actually our reusable code lies in a groovy method that has got stage. And we are calling this groovy method from steps. And it is causing the stage node to be hidden on blueocean. It is not considering the stage as a stage just because the stage has been called from steps. Below is how blueocean view looks like. The Code Push and Test Execution stage is where the problem lies. The code looks like below-

tests = [:]
//code for other stages like Prepare, Initialize, Prepare App
stage('Test') {
            stages{
                stage('Cycle and Test Definition Prep') {
                    steps {
                        script {
                                //groovy code to populate tests variable
                                for (int i = 1; i <= NUMBER_OF_ORGS.toInteger(); ++i) {
                                def orgNumber = i;
                                tests['Org ' + orgNumber] = {
                                    processOrg(orgNumber, cycleKey, definitionKey, newCycleName, folderName)
                                }
                            }
                         }
                    }
                }
                stage('Code Push and Test Execution') {
                    steps {
                        script {
                            parallel tests
                        }
                    }
                }
            }
        }
    }

void processOrg(int orgNumber, String testCycleID, String testCycleDefinition, String newCycleName, String testResultFolder) {
    def orgUsername
    def directory

    stage ('Deployment' + orgNumber) {
        //some code
    }
}



enter image description here

I was expecting the stage Deployment 1 etc to show as a stage that does not happen.

PS:- I see https://stackoverflow.com/a/57507753/6532664 & What is the difference between a node, stage, and step in Jenkins pipelines? but still want to see if there is a way out. This is working perfectly but just not showing in blueocean and I have downside code to check failure stagees. Now because it is not recognized as a stage so my downside code is not working.

2

There are 2 best solutions below

0
Ruslan On

visual problem occurs because of different logic to render stages in blueocean and classic UI

all code inside of script {} will treat as "scripted" pipeline

jenkins classic UI shows "stage name" while blueocean shows branch name

tests['Org ' + orgNumber] - its your branch name for parallel code

in perfect case need to use the same name of stage and name of branch for parallel

def tests = [:]

void processOrg(int orgNumber, String testCycleID, String testCycleDefinition, String newCycleName, String testResultFolder) {

    stage ('Deployment' + orgNumber) {
      //some code
    }
}

pipeline {
  agent any

  stages {
    stage('Test') {
      stages {
        stage('Cycle and Test Definition Prep') {
          steps {
            script {
                //groovy code to populate tests variable
                for (int i = 1; i <= NUMBER_OF_ORGS.toInteger(); ++i) {
                  def orgNumber = i;
                  tests['Deployment' + orgNumber] = {
                    processOrg(orgNumber, cycleKey, definitionKey, newCycleName, folderName)
                  }
              }
            }
          }
        }
        stage('Code Push and Test Execution') {
          steps {
            script {
              parallel tests
            }
          }
        }
      }
    }
  }
}

also would be better to use Map as arg

void processOrg(Map args) {
  print("${args.orgNumber} ${args.testCycleID}")
}

and use like

def args = [orgNumber: 1, testCycleID: "111"]
processOrg(args)

or

processOrg(orgNumber: 1, testCycleID: "111")
1
KrisOch On

Use Pipeline Graph View plugin instead of Blue Ocean.

Unfortunately Blue Ocean has problems to render programatically generated nested parallel stages. Blue Ocean is also no more developed.