HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Jenkins pipeline nested parallel

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
jenkinsnestedparallelpipeline

Problem

Jenkins pipeline, declarative.

Simple question, how do I create a parallel stages inside a stage which is parallel by itself?

```
pipeline {
agent { label 'master' }
options {
timestamps()
timeout(time: 15, unit: 'MINUTES')
ansiColor('xterm')
}

stages {
stage('Main parallel') {
parallel {
stage('Start containers') {
agent { label 'my-agent' }
stages {
stage('Start Linux containers') {
steps {
script {
dir (""){
println "running"
}
}
}
}

}
}
stage('Build UI') {
stages {
stage('Cleanup main workspace'){
steps {
dir("") {
cleanWs()
}
}
}
stage('Git checkout') {
steps {
dir("") {
checkout()
}
}
}

stages {
stage('Gradle build') {
parallel{
stage('gradle1') {
steps {
dir("") {
script {
buildInfo = rtGradle.run switches: gradleParams , tasks: 'clean deploy'

Solution

Sure, you can do this with Scripted Pipelines. I've never tried to do it with Declarative so I'm not sure whether it's possible to do with Declarative or, if it is, if it's easy or straightforward.

Here's an example of what this might look like with a Scripted Pipeline. I haven't tested it so there may be syntax errors or other problems.

def parallelTopLevelSteps = [:]

parallelTopLevelSteps['A and B'] = {
  def parallelNestedSteps = [:]

  parallelNestedSteps['step A'] = { echo('A') }
  parallelNestedSteps['step B'] = { echo('B') }

  parallel(parallelNestedSteps)
}

parallelTopLevelSteps['C and D'] = {
  def parallelNestedSteps = [:]

  parallelNestedSteps['step C'] = { echo('C') }
  parallelNestedSteps['step D'] = { echo('D') }

  parallel(parallelNestedSteps)
}

parallel(parallelTopLevelSteps)

Code Snippets

def parallelTopLevelSteps = [:]

parallelTopLevelSteps['A and B'] = {
  def parallelNestedSteps = [:]

  parallelNestedSteps['step A'] = { echo('A') }
  parallelNestedSteps['step B'] = { echo('B') }

  parallel(parallelNestedSteps)
}

parallelTopLevelSteps['C and D'] = {
  def parallelNestedSteps = [:]

  parallelNestedSteps['step C'] = { echo('C') }
  parallelNestedSteps['step D'] = { echo('D') }

  parallel(parallelNestedSteps)
}

parallel(parallelTopLevelSteps)

Context

StackExchange DevOps Q#5240, answer score: 5

Revisions (0)

No revisions yet.