Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-55600

Parallel execution of same job N times with different parameters leads to only 1 execution of the job

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: Minor Minor
    • groovy-plugin
    •  Jenkins 2.150.1, Pipeline-Groovy Plugin 2.57

      1) I created sample job (see "sample_job_config.xml" attachment) which has "PROJECT_NAME" parameter
      2) I want to run it in parallel 5 times with different "PROJECT_NAME" values. I created following script for this:

      pipeline {
          agent none
          stages {
              stage('Processing projects') {
                  steps {
                      script {
                          def projects = ['project0', 'project1', 'project2', 'project3', 'project4']
                          def projectsBuilds = [:]
      
                          def labelParameters
                          for (project in projects) {
                              //JENKINS-55426 need to save iterated variable value into intermediate variable, otherwise there will be last value from collection for all jobs
                              def currentProject = project
                              labelParameters = []
                              labelParameters.add([$class: 'LabelParameterValue', name: 'node', label: 'linux'])
                              labelParameters.add([$class: "StringParameterValue", name: "PROJECT_NAME", value: currentProject])
      
                              projectsBuilds[currentProject] = {
                                  stage(String.format('%s execution', currentProject)) {
                                      build job: 'Sample-Job', parameters: labelParameters
                                  }
                              }
                          }
      
                          parallel projectsBuilds;
                      }
                  }
              }
          }
      }
      

      3) However, when I run this script it only invokes job ONCE, not FIVE TIMES (see screen)

          [JENKINS-55600] Parallel execution of same job N times with different parameters leads to only 1 execution of the job

          Alexandr Panshin added a comment - - edited

          Though I still guess it is a defect (I suppose "labelParameters = []" instruction in Groovy should rewrite pointer value?), this problem seems to appear because of using same "labelParameters" variable for all of jobs.
          Instantiating container for parameters for each job seems to solve the problem:

          pipeline {
              agent none
              stages {
                  stage('Processing projects') {
                      steps {
                          script {
                              def projects = ['project0', 'project1', 'project2', 'project3', 'project4']
                              def projectsBuilds = [:] // essential change here
          
                              for (project in projects) {
                                  //JENKINS-55426 need to save iterated variable value into intermediate variable, otherwise there will be last value from collection for all jobs
                                  def currentProject = project
                                  def labelParameters = []
                                  labelParameters.add([$class: 'LabelParameterValue', name: 'node', label: 'linux'])
                                  labelParameters.add([$class: "StringParameterValue", name: "PROJECT_NAME", value: currentProject])
          
                                  projectsBuilds[currentProject] = {
                                      stage(String.format('%s execution', currentProject)) {
                                          build job: 'Sample-Job', parameters: labelParameters
                                      }
                                  }
                              }
          
                              print(projectsBuilds)
                              parallel projectsBuilds;
                          }
                      }
                  }
              }
          }
          

          Alexandr Panshin added a comment - - edited Though I still guess it is a defect (I suppose "labelParameters = []" instruction in Groovy should rewrite pointer value?), this problem seems to appear because of using same "labelParameters" variable for all of jobs. Instantiating container for parameters for each job seems to solve the problem: pipeline { agent none stages { stage( 'Processing projects' ) { steps { script { def projects = [ 'project0' , 'project1' , 'project2' , 'project3' , 'project4' ] def projectsBuilds = [:] // essential change here for (project in projects) { //JENKINS-55426 need to save iterated variable value into intermediate variable, otherwise there will be last value from collection for all jobs def currentProject = project def labelParameters = [] labelParameters.add([$class: 'LabelParameterValue' , name: 'node' , label: 'linux' ]) labelParameters.add([$class: "StringParameterValue" , name: "PROJECT_NAME" , value: currentProject]) projectsBuilds[currentProject] = { stage( String .format( '%s execution' , currentProject)) { build job: 'Sample-Job' , parameters: labelParameters } } } print(projectsBuilds) parallel projectsBuilds; } } } } }

          Found the source of the problem, created new defect, which better describes problem

          Alexandr Panshin added a comment - Found the source of the problem, created new defect, which better describes problem

            vjuranek vjuranek
            alpanshin Alexandr Panshin
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: