How to parallelize stages in Jenkins pipeline?

151 Views Asked by At

I have the following pipeline and I need to parallelize the javadoc generation and publishment and the execution and publish of integration tests report. enter image description here

I'm trrying the following way but it doesn't seems like the pipeline is beeing parallelize.

    stage('Generate and Publish Javadoc, Run and publish Integration Tests, Run and publish Mutation Tests') {
        parallel {
            stage('javadoc'){
                stages{
                    stage('Generate and publish Javadoc on Linux') {
                        when { expression { env.OS == 'UNIX' }}
                        steps {
                            dir('') {
                                sh './gradlew javadoc'
                                javadoc javadocDir: 'build/docs/javadoc', keepAll: true
                            }
                        }
                    }
            
                    stage('Generate and publish Javadoc on Windows') {
                        when { expression { env.OS == 'BAT' }}
                        steps {
                            dir('') {
                                bat 'gradlew.bat javadoc'
                                javadoc javadocDir: 'build/docs/javadoc', keepAll: true
                            }
                        }
                    }
                }
            }

            stage('integration test'){
                stages{
                    stage('Integration Tests on Linux') {
                        when { expression { env.OS == 'UNIX' }}
                        steps {
                            dir('') {
                                sh './gradlew integrationTest'
                                junit '**/build/test-results/integrationTest/*.xml'
                            }
                        }
                    }

                    stage('Integration Tests on Windows') {
                        when { expression { env.OS == 'BAT' }}
                            steps {
                                dir('') {
                                    bat 'gradlew.bat integrationTest'
                                    junit '**/build/test-results/integrationTest/*.xml'
                                }
                            }
                    }
                    
                    stage('Publish Integration Tests Report') {
                        steps {
                            dir('') {
                                publishHTML(target: [allowMissing         : false,
                                                    alwaysLinkToLastBuild: false,
                                                    keepAll              : true,
                                                    reportDir            : 'build/reports/tests/integrationTest',
                                                    reportFiles          : 'index.html',
                                                    reportName           : 'Integration Tests Report'])
                                }
                        }
                    }
                }
            }
        }
    }

The following code uses the tag parallel and inside it has a stage with stages inside it. With this I expect that the tag parallel only recognizes the most exterior stages.

1

There are 1 best solutions below

2
M B On

With this I expect that the tag parallel only recognizes the most exterior stages.

No, Jenkins should be able to recognize the nested stages as well. I have used a similar pipeline structure to what you have and the pipeline branches off into three parts, with each part containing multiple stages.

As far as I can see, there is no problem with your pipeline syntax and it's pretty much the same set of stages and steps that I use in my pipeline. So the question here is: are you sure that the pipeline isn't running in parallel? Using something like the Blue Ocean view for Jenkins should help you visualize the steps properly. If the steps aren't running in parallel, are they running sequentially?