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

Job-DSL configure section does not evaluate linearly with the rest

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Major Major
    • job-dsl-plugin
    • None
    • Tested with the latest LTS Jenkins ver. 2.150.1

      The following job-dsl code showcases the problem. The code after the configure block is apparently always executed before the configure block. Which seriously broke my trust in programming languages   ...

      The code is needed in a method to add a playbook to a job. It needs to add a environment variable but shouldn't overwrite existing ones. Therefore i need to check for existing one then either add a section or add the variables.

      There is possibly a easier solution. Thats what i came up with

      import javaposse.jobdsl.dsl.DslFactory
      import javaposse.jobdsl.dsl.Job
      def setEnvironmentVars(Map<String, String> envVars, Job job) {
        
        job.with {
          
          // exists2 to rule out there is some other variable/method whatever with that name
          boolean exists2 = false
          println "1 -> ${exists2} ${new Date().format("yyyyMMdd-HH:mm:ss.SSS")}"
          
          // This configure block is evaluated AFTER the if statement below. The outputs 2/3 are always
          // after 4/5
          configure {
            if ( (it / 'properties').get('EnvInjectJobProperty') ) {
              println "2 -> exists2 = ${exists2} ${new Date().format("yyyyMMdd-HH:mm:ss.SSS")}"
              exists2 = true
              println "switched exists to true"
              println "3 -> exists2 = ${exists2} ${new Date().format("yyyyMMdd-HH:mm:ss.SSS")}"
            }
          }
          
          // exists2 is false here ALWAYS ... even if the evironmentVariables section below is active
          // and the check in the configure block above leads to exists2 beeing changed to true
          println "4 -> exists2 = ${exists2} ${new Date().format("yyyyMMdd-HH:mm:ss.SSS")}"
          
          // This sleep even delays the execution of the configure block above !?!?!?
          sleep 2000
          
          println "5 -> exists2 = ${exists2} ${new Date().format("yyyyMMdd-HH:mm:ss.SSS")}"
        
          // Because i can't get the check in the configure block above to evaluate before getting here
          // its impossible for me to correctly append or set the environment variables here. They are
          // always set therefore overwriting any previous values. 
          if (exists2) {
      	    configure {
      	      newprops = envVars.collect {
          	        key, value -> "${key}=${value}" }.
              	  join("\n") 
                props = it / 'properties' / 'EnvInjectJobProperty' / 'info' / 'propertiesContent'
                props.setValue( (props.value() ?: "") + "\n" + newprops)       
              }
          } else {    
            environmentVariables {
              envVars.each { key, value ->
                env(key, value)
              }
            }
          }
        }
      }
      freeStyleJob("HalloWelt-TEST") { job ->  /** The idea is to have this working with or without this environmentVariables */
        environmentVariables {
          env("HALLO", "WORLD")
          env("A", "B")
        }
        
        setEnvironmentVars(["A": "B", "C": "D"], job)
      } 

       

      Result

      1 -> false 20190110-11:06:56.824
      4 -> false 20190110-11:06:56.828
      5 -> false 20190110-11:06:58.829
      2 -> false 20190110-11:06:58.834
      3 -> true 20190110-11:06:58.834 

      Expected Result

      1 -> false 20190110-11:06:56.824
      2 -> false 20190110-11:06:58.834
      3 -> true 20190110-11:06:58.834 
      4 -> true 20190110-11:06:56.855
      5 -> true 20190110-11:06:58.855
      

       

            jamietanna Jamie Tanna
            jansenm Michael jansen
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: