Georgi Georgiev
Georgi Georgiev

Reputation: 1623

Is it possible to run multiple stages with their respective steps in parallel in a Jenkins Pipeline?

I searched and experimented a lot, but couldn't find any working example of what I need.

I want to run 2 stages in parallel, each stage has several steps. And by parallel I mean the stages themselves and their respective steps. All existing examples and topics I found relate to executing steps in parallel inside a stage.

I don't need that, I need to run 2 stages alongside their steps in parallel. Please look at this screenshot:

enter image description here

Current behavior is that Parent Stage 1 is executed and then Parent Stage 2 starts to execute.

Is it possible to run "Parent Stage 1" and "Parent Stage 2" in parallel?

Upvotes: 0

Views: 827

Answers (2)

yehudahs
yehudahs

Reputation: 2668

The answer of @stefano is correct - use stages inside parallel although he didn't use it in his example. here is what I did:

pipeline {
  agent { node {
    label 'you node'
  }}
  stages {
    stage ("Run Tests"){
      parallel {
        stage ("Run Parent 1 stages") {
          stages {
            stage("Parent 1 Stage 1") {
              steps {
                echo "Parent 1 stage 1"
              }
            }
            stage("Parent 1 stage 2") {
              steps {
                  echo  "Parent 1 stage 2"
              }
            }
            stage("Parent 1 stage 3") {
              steps {
                echo "Parent 1 stage 3"
              }
            }  
          }
        }
        stage ("Run Parent 2 stages") {
          stages {
            stage("Parent 2 stage 1") {
              steps {
                echo "Parent 2 stage 1"
              }
            }
          }
        }
      }
    }
  }
}

and you will get this pipeline: enter image description here

Upvotes: 0

Stefano
Stefano

Reputation: 5076

You can use stages inside the parallel block. e.g.

pipeline {
    agent any
    stages {
        stage('Non Parallel Execution') {
            steps {
                 sh 'echo step 1a'
                 sh 'echo step 2a'
                 sh 'echo step 3a'
            }
        }
        stage('Parallel Execution') {
            parallel {
                 stage('Parent A') {
                    steps {
                        sh 'echo step 1'
                        sh 'echo step 2'
                    }
                }
                stage('Parent B') {
                    steps {
                        sh 'echo step 3'
                        sh 'echo step 4'
                    }
                }
            }
        }
    }
}

src: https://jenkins.io/doc/book/pipeline/syntax/#parallel

If you're trying to have nested parallel execution instead, I'd suggest to read the answers to this question: Error: The parallel step can only be used as the only top-level step

Upvotes: 1

Related Questions