7

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?

user3266259
  • 369
  • 3
  • 8
  • 22
  • Yep. The `parallel()` function, which takes a `Map` of stages (not a list of stages like in your example), does this. Have a look at [this answer](https://stackoverflow.com/a/53456430/3195526). – Paul Hicks Aug 13 '19 at 23:01

2 Answers2

8

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"
                            }
                        }
                )
Unforgettable631
  • 940
  • 6
  • 10
  • This worked for me, though i had to put the `parallel` block in a stage for it to show the stage name instead of `Parallel` – P. Sithole Feb 13 '23 at 13:41
2

This worked for me

    stage('Check code quality') {
      parallel {
        stage('Run prospector') {
          when {
            expression { params.SKIP_PROSPECTOR == false }
          }

          steps {
            checkout scm
            sh 'echo "Running prospector..."'
            sh 'make dockerized-run-prospector'
          }
        }

        stage('Run Tests') {
          when {
            expression { params.SKIP_TESTS == false }
          }

          steps {
            checkout scm
            sh 'echo "Running tests..."'
            sh 'make dockerized-test'
          }
        }
      }
    }

This runs stages inder the parent stage parallely. Jenkins blue shows it like this enter image description here

Rishav
  • 3,818
  • 1
  • 31
  • 49