Search code examples
jenkinsjenkins-pipelinejenkins-groovyjenkins-job-dsl

Jenkins Workspaces are not created for Parallel Stages created inside the Pipeline Script


Jenkins Custom Workspaces are not created for Parallel Stages created inside the Pipeline Script

Only the main Custom Workspace "main_ws" is created.

Why are Custom Workspaces for parallel script-generated stages not created and how can I fix this?

def getTests(Name, Versions) {
    return Versions.collectEntries { version ->
        [
                (version): {
                    stage("${Name}:${version} Tests") {
                        agent {
                            node {
                                label "docker"
                                customWorkspace "${Name}${version}"
                            }
                        }
                        script {
                            bat "echo Testing the ${Name}:${version}"
                        }
                    }
                }
        ]
    }
}


pipeline {
  agent {
    label "docker"
  }

  tools {
    gradle "gradle"
  }

  parameters {
    text(name: 'VERSIONS', defaultValue: '1.0\n2.0\n3.0\n', description: '')
    booleanParam(name: 'NAME', defaultValue: false, description: '')
  }
  stages {
    stage('Tests') {
      parallel {
        stage('Tests') {
          when {
            expression {
              return params.NAME
            }
          }
          agent {
            node {
              label "docker"
              customWorkspace "main_ws"
            }
          }
          steps {
            script {
              def versions = params.VERSIONS.split("\n")
              parallel getTests("NAME", versions)
            }
          }
        }
      }
    }
  }
}

Solution

  • Declarative syntax inside scripted syntax doesn't work the same way as outside. Outside the script step pipeline and stage are keywords and accept agent keyword. Inside the script step pipeline and stage are steps that know nothing agent keyword anymore. The corresponding scripted syntax is node and ws, see here.