Home > front end >  Get list of child-stage names in parallel stage of Jenkinsfile at runtime
Get list of child-stage names in parallel stage of Jenkinsfile at runtime

Time:11-23

I have a Jenkinsfile running integration tests which works well, apart from the fact that my testdata is hardcoded and may change.

I have created a step to fetch testdata for all steps at once, to avoid using the, with the intention of running integration tests quicker, in parallel.

If I want to fetch all the testdata in a pre-step, and fetch data for each stage under stage('Integration Tests') I need to figure out how many child-stages there are when running the jenkins pipeline. Is this possible?

        stage('Integration Tests'){           
            parallel {
                stage('TestGroup 1'){
                    steps {
                        script {
                            sh  script: 'npm run some-init-func'
                            sh  script: 'npm run newman-run-collection --collection_file="100 tests.postman_collection.json"'
                            sh  script: 'npm run newman-run-collection --collection_file="110 more tests.postman_collection.json"'
                        }
                    }
                    post {
                        always {
                            junit 'newman/*.xml'
                            archiveArtifacts artifacts: 'newman/*.html'
                        }
                    }
                }
                stage('TestGroup 2'){
                    steps {
                        script {
                            sh  script: 'npm run some-init-func'
                            sh  script: 'npm run newman-run-collection --collection_file="200 tests.postman_collection.json"'
                            sh  script: 'npm run newman-run-collection --collection_file="210 even more tests.postman_collection.json"'
                        }
                    }
                    post {
                        always {
                            junit 'newman/*.xml'
                            archiveArtifacts artifacts: 'newman/*.html'
                        }
                    }
                }
            }

CodePudding user response:

I see two ways to do this. Do some Groovy code or refactor your Pipeline.

Option 01

You can probably refactor your Pipeline to something like the one below.

pipeline {
    agent any
    stages {
        stage('Tests') {
            steps {
                script {
                    println getParallelStages().size()
                    parallel getParallelStages()
                }
            }
        }
    }
}


def getParallelStages() {
    return ["Group1": {stage('TestGroup 1'){
                        script {
                            sh  script: 'npm run some-init-func'
                            sh  script: 'npm run newman-run-collection --collection_file="100 tests.postman_collection.json"'
                            sh  script: 'npm run newman-run-collection --collection_file="110 more tests.postman_collection.json"'
                        }
                    post {
                        always {
                            junit 'newman/*.xml'
                            archiveArtifacts artifacts: 'newman/*.html'
                        }
                    }
                }}, 

                "Group2": {stage('TestGroup 2'){
                        script {
                            sh  script: 'npm run some-init-func'
                            sh  script: 'npm run newman-run-collection --collection_file="200 tests.postman_collection.json"'
                            sh  script: 'npm run newman-run-collection --collection_file="210 even more tests.postman_collection.json"'
                        }
                    post {
                        always {
                            junit 'newman/*.xml'
                            archiveArtifacts artifacts: 'newman/*.html'
                        }
                    }
                }
              }
        ]
}

Option 02

Here is one way to do this with Groovy. Let's assume you have a unique name with a common prefix for your child stages. In that case, you can read the Pipeline definition and count the unique strings present. Something like the one below.

// Get the script
def jobInfo = Jenkins.instance.getItemByFullName("$JOB_NAME").getDefinition().getScript()
// Count the stages
println jobInfo.count("TestGroup ")
  • Related