Search code examples
jenkinsgroovyjenkins-pipelinejenkins-groovy

Jenkins Parallel Build reads in an empty map, but it held data in a previous stage


Total noobie trying to make a parallel build more dynamic.

Using this declarative script https://stackoverflow.com/a/48421660/14335065 instead of reading in a prepopulated map def jobs = ["JobA", "JobB", "JobC"], which works perfectly.

I am trying to read in from a global map variable JOBS = [] which I populate in a stage using JOBS.add("JobAAA") syntax.

Printing out JOBS in a pipeline stage shows there are contents within,

JOBS map is [JobAAA, JobBBB, JobCCC]

but when I use it to generate a parallel build it seems to become empty and I am getting error message

No branches to run

I know I must be mixing my understands up somewhere, but can anyone please point me in the right direction.

Here is the code I am fighting with

def jobs = ["JobA", "JobB", "JobC"]
JOBS_MAP = []

def parallelStagesMap = jobs.collectEntries() {
   ["${it}" : generateStage(it)]
}

def parallelStagesMapJOBS = JOBS_MAP.collectEntries(){
   ["${it}" : generateStage(it)]
}

def generateStage(job) {
   return {
       stage("Build: ${job}") {
           echo "This is ${job}."
       }
   }
}

pipeline {
   agent any

   stages {
       stage('populate JOBS map') {
           steps {
               script {
                   JOBS_MAP.add("JobAAA")
                   JOBS_MAP.add("JobBBB")
                   JOBS_MAP.add("JobCCC")
               }
           }
       }
       stage('print out JOBS map'){
           steps {
               echo "JOBS_MAP map is ${JOBS_MAP}"
           }
       }
       stage('parallel job stage') {
           steps {
               script {
                   parallel parallelStagesMap
               }
           }
       }
       stage('parallel JOBS stage') {
           steps {
               script {
                   parallel parallelStagesMapJOBS
               }
           }
       }
   }
}

Solution

  • Try this:

    def jobs = ["JobA", "JobB", "JobC"]
    JOBS_MAP = []
    
    def generateStage(job) {
       return {
           stage("Build: ${job}") {
               echo "This is ${job}."
           }
       }
    }
    
    pipeline {
       agent any
    
       stages {
           stage('populate JOBS map') {
               steps {
                   script {
                       JOBS_MAP.add("JobAAA")
                       JOBS_MAP.add("JobBBB")
                       JOBS_MAP.add("JobCCC")
                   }
               }
           }
           stage('print out JOBS map'){
               steps {
                   echo "JOBS_MAP map is ${JOBS_MAP}"
               }
           }
           stage('parallel job stage') {
               steps {
                   script {
                       def parallelStagesMap = jobs.collectEntries() {
                           ["${it}" : generateStage(it)]
                       }
                       parallel parallelStagesMap
                   }
               }
           }
           stage('parallel JOBS stage') {
               steps {
                   script {
                       def parallelStagesMapJOBS = JOBS_MAP.collectEntries(){
                          ["${it}" : generateStage(it)]
                       }
    
                       parallel parallelStagesMapJOBS
                   }
               }
           }
       }
    }