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

Jenkins cannot execute one job with the same (by value, not adress) parameters, more than once

XMLWordPrintable

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

      I want to execute same job with same parameters five times
      But the following code leads to only one actual execution:

      pipeline {
          agent none
          stages {
              stage('Processing same project 5 times') {
                  steps {
                      script {
                          def projectsBuilds = [:]
      
                          for (int i = 0; i < 5; i++) {
                              int currentIteration = i
                              String uniqueRunName = String.format('Run #%d', currentIteration);
      
                              def labelParameters = []
                              labelParameters.add([$class: 'LabelParameterValue', name: 'node', label: 'linux'])
                              labelParameters.add([$class: "StringParameterValue", name: "PROJECT_NAME", value: 'Sample-Job'])
      
                              print(String.format("This parameters set hash is \"%s\"", labelParameters.hashCode()))
      
                              projectsBuilds[uniqueRunName] = {
                                  stage(String.format('Sample-Job execution #%d', currentIteration)) {
                                      build job: 'Sample-Job', parameters: labelParameters
                                  }
                              }
                          }
      
                          parallel projectsBuilds;
                      }
                  }
              }
          }
      }
      

      As you see from execution log screen (screen0), though I explicitly instantiate "labelParameters" on each iteration, it has the same hash code in every iteration. Seems, Jenkins groovy plugin consider this situation as "one parameters container - one run"

      Meanwhile if I just add one more parameter to container - current iteration index, groovy stops to "optimize" my code and now "labelParameters" has different hash code on every iteration. This leads to execution of "Sample-Job" 5 times, as intended (screen1)

      pipeline {
          agent none
          stages {
              stage('Processing same project 5 times') {
                  steps {
                      script {
                          def projectsBuilds = [:]
      
                          for (int i = 0; i < 5; i++) {
                              int currentIteration = i
                              String uniqueRunName = String.format('Run #%d', currentIteration);
      
                              def labelParameters = []
                              labelParameters.add([$class: 'LabelParameterValue', name: 'node', label: 'linux'])
                              labelParameters.add([$class: "StringParameterValue", name: "PROJECT_NAME", value: 'Sample-Job'])
                              labelParameters.add([$class: "StringParameterValue", name: "ITERATION_INDEX", value: currentIteration.toString()])
      
                              print(String.format("This parameters set hash is \"%s\"", labelParameters.hashCode()))
      
                              projectsBuilds[uniqueRunName] = {
                                  stage(String.format('Sample-Job execution #%d', currentIteration)) {
                                      build job: 'Sample-Job', parameters: labelParameters
                                  }
                              }
                          }
      
                          parallel projectsBuilds;
                      }
                  }
              }
          }
      }
      

        1. screen0.jpg
          screen0.jpg
          156 kB
        2. screen1.jpg
          screen1.jpg
          171 kB

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

              Created:
              Updated: