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

Pipeline 'load' step return instance shares variables between calls of it

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not A Defect
    • Icon: Minor Minor
    • workflow-cps-plugin
    • None
    • Jenkins 2.332.3

      I have a scripted pipeline job with this script:

      node(null) {
          def c = { id ->
              ws('some_ws') {
                  for (int a = 0; a < 10; ++a) {
                      //id += (int)(Math.random() * 100000)
                      def loadStepValidationId = "${id} ${a}"
                      writeFile file: 'script.groovy', text: 'this.loadStepValidationId = \"' + loadStepValidationId + '\"\nreturn this'
                      def sc = load 'script.groovy'
                      sleep time: 100, unit: 'MILLISECONDS'
                      if (loadStepValidationId != sc?.loadStepValidationId) {
                          error "Attempted to load script ${loadStepValidationId}, but got ${sc?.loadStepValidationId}."
                      }
                      if (isUnix()) {
                          sh('rm script.groovy')
                      } else {
                          bat('del /q script.groovy')
                      }           
                  }
                  
              }
          }
          def pstages = [:]
          for (int i = 0; i < 20; ++i) {
              pstages[i.toString()] = { c(i) }
          }
          parallel pstages
      } 

      It fails because it turns out that referring 'this' in the script returns who knows what instance.

      To fix it, I need to turn the variable 'loadStepValidationId' into a function that returns a constant value.

      Can someone shed some light on this? This smells like a bug or at least undefined, undocumented behavior.

      I ran variations of this script and it seems that all variables that are assigned to 'this' basically belong to a singleton, is this by design? Is this documented somewhere?

            Unassigned Unassigned
            gl1koz3 Edgars Batna
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: