Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create parallel stages in Jenkins scripted pipeline?

I am trying to implement parallelization in my Jenkins pipeline code where I can run two stages in parallel. I know this is possible in declarative pipeline, but I am using scripted pipeline.

I've attempted to implement this by doing something like this:

parallel(
    stage('StageA') {
        echo "This is branch a"
    },
    stage('StageB') {
        echo "This is branch b"
    }
  )

When I run this and look at this in Blue ocean, the stages do not run in parallel, but instead, StageB is executed after StageA. Is it possible to have parallel stages in scripted jenkins pipeline? If so, how?

like image 406
user3266259 Avatar asked Aug 13 '19 22:08

user3266259


People also ask

What is parallel in Jenkins pipeline?

This means that one stage could run on a certain machine, and another stage of the same pipeline could run on another machine. Thus having a Jenkins distributed build and not just a Jenkins parallel build.

Can we have multiple steps in a stage in Jenkins?

Jenkins Pipeline allows you to compose multiple steps in an easy way that can help you model any sort of automation process. Think of a "step" like a single command which performs a single action. When a step succeeds it moves onto the next step. When a step fails to execute correctly the Pipeline will fail.


1 Answers

Try this syntax for scripted pipeline:

            parallel(
                    "StageA": {
                        echo "This is branch a"
                    },
                    "StageB": {
                        echo "This is branch b"
                    }
            )

It should look like this in Blue Ocean, this is what you expect right?

Parallel blue ocean

If you want to see the stages (and console output) in the classic view, you can use stage like this:

 parallel(
                        "StageA": {
                            stage("stage A") {
                                echo "This is branch a"
                            }
                        },
                        "StageB": {
                            stage("stage B") {
                                echo "This is branch b"
                            }
                        }
                )
like image 184
Unforgettable631 Avatar answered Oct 31 '22 16:10

Unforgettable631