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

While running in parallel for a loop, the index is wrong for each iteration

XMLWordPrintable

      When using a parallel step for an indexed loop, using the loop-iterator-index for internal usage results with wrong value (showing the max-index-value for all iterations).
      A workaround for it is to set a local variable for saving the current iteration-index and use the local variable.
      The following pipeline demonstrates the problem (the issue is demonstrated with the internal 'echo' steps):

      pipeline
       {
           agent any
           stages
           {
               stage('Explore bug')
               {
                    steps
                   {
                        script
                        {
                            int maxIndex1=3
                            def loopers1 = [:]
                            for (int loopIndex1 = 0; loopIndex1 < maxIndex1; loopIndex1++)
                           {
                               echo "loopIndex1=${loopIndex1}"
                               loopers1["loop-$\{loopIndex1}"] =
                               {
                                   echo "loopIndex1=${loopIndex1}"
                               }
                          }
                          parallel loopers1
                     }
                 }
           }
           stage('Bug workaround')
           {
               steps
               {
                   script
                   {
                       int maxIndex2=3
                       def loopers2 = [:]
                       for (int loopIndex2 = 0; loopIndex2 < maxIndex2; loopIndex2++)
                       {
                           int index2=loopIndex2
                           echo "index2=${index2}"
                           loopers2["loop-$\{index2}"] =
                           {
                               echo "index2=${index2}"
                           }
                      }
                     parallel loopers2
                 } 
               }
             }
           }
       }

       

            Unassigned Unassigned
            yorammi Yoram Michaeli
            Votes:
            7 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: