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

NodeLabel parameters are null objects in pipeline script params

      Accessing the parameter with param will give you a null object, but accessing it through env and global will give you the string of one node.

      Example:

      properties ([
          parameters([
              [
                   $class: 'BooleanParameterDefinition',
                   defaultValue: false,
                   description: 'This will do a clean and build of the project.',
                   name: 'Rebuild'
              ],
              [
                   $class: 'NodeParameterDefinition',
                   allowedSlaves: ['ALL (no restriction)'],
                   defaultSlaves: ['master'],
                   description: 'What nodes to run the build on.',
                   name: 'NODE_PARAM',
                   nodeEligibility: [$class: 'AllNodeEligibility'],
                   triggerIfResult: 'allowMultiSelectionForConcurrentBuilds'
              ]
          ])
      ])
      
      echo "NODE_PARAM: " + NODE_PARAM.getClass().toString()
      echo "env.NODE_PARAM: " + env.NODE_PARAM.getClass().toString()
      echo "params.NODE_PARAM: " + params.NODE_PARAM.getClass().toString()
      
      

      Will give you this output:

      [Pipeline] echo
      NODE_PARAM: class java.lang.String
      [Pipeline] echo
      env.NODE_PARAM: class java.lang.String
      [Pipeline] echo
      params.NODE_PARAM: class org.codehaus.groovy.runtime.NullObject
      

      As you can see, only params.NODE_PARAM is null. 

      The problem is that I can not use env.NODE_PARAM or NODE_PARAM to get multiple selection of nodes, as those are only a string representation of a single node.

       

          [JENKINS-43720] NodeLabel parameters are null objects in pipeline script params

          Joerg Schwaerzler added a comment - - edited

          Can confirm. Another issue would be that only the params. version would return the default value for the label or node paramter when called for the first time.

          Joerg Schwaerzler added a comment - - edited Can confirm. Another issue would be that only the params. version would return the default value for the label or node paramter when called for the first time.

          Maciej added a comment -

          I can see the issue on Jenkins 2.321     org.jenkins-ci.plugins:nodelabelparameter:1.9.2

          Also when running from script console the builds node parameter value is always null.

           

          Running in script console

          def buildingJobs = Jenkins.instance.getAllItems(Job.class).findAll { it.isBuilding() }
          buildingJobs.each { job->
              allRuns = job._getRuns()
              allRuns.each { item ->
                
                  def runningLabel
                  if (item.isBuilding()) {
                      runningLabel = item.getExecutor().getOwner().getNode().getNodeName()
                  }
          
                  def scheduledLabel = 'zzz';
                  // get parameters
                  def parameters = item?.actions.find{ it instanceof ParametersAction }?.parameters
                  parameters.each {
                      if( it.name == "PARAM_WITH_LABEL") {
                          scheduledLabel = it.value
                      }
                      //println "        ${it.name}: ${it.value}"
                  }
                println "'${item.getUrl()}' ${item.isBuilding()} '${scheduledLabel}' '${runningLabel}'"
              }
          }
          

           

          I get something alike

          'job/dibench/22740/' true 'null' 'nodename1'
          'job/dibench/22739/' true 'null' 'nodename2'
          'job/dibench/22738/' false 'null' 'null'
          'job/dibench/22737/' false 'null' 'null'

          Maciej added a comment - I can see the issue on Jenkins 2.321     org.jenkins-ci.plugins:nodelabelparameter:1.9.2 Also when running from script console the builds node parameter value is always null.   Running in script console def buildingJobs = Jenkins.instance.getAllItems(Job.class).findAll { it.isBuilding() } buildingJobs.each { job->     allRuns = job._getRuns()     allRuns.each { item ->                def runningLabel         if (item.isBuilding()) {             runningLabel = item.getExecutor().getOwner().getNode().getNodeName()         }         def scheduledLabel = 'zzz' ;         // get parameters         def parameters = item?.actions.find{ it instanceof ParametersAction }?.parameters         parameters.each {           if ( it.name == "PARAM_WITH_LABEL" ) {                 scheduledLabel = it.value             }             //println "        ${it.name}: ${it.value}"         }     println " '${item.getUrl()}' ${item.isBuilding()} '${scheduledLabel}' '${runningLabel}' "     } }   I get something alike 'job/dibench/22740/' true ' null ' 'nodename1' 'job/dibench/22739/' true ' null ' 'nodename2' 'job/dibench/22738/' false ' null ' ' null ' 'job/dibench/22737/' false ' null ' ' null '

          Robin Fauvel added a comment -

          I can also reproduce the problem described here.
          The parameter value should be accessible in the params object rather than (or in addition to) in the env object.
          This convention is described here and should be followed: https://www.jenkins.io/doc/book/pipeline/syntax/#parameters

          The values for these user-specified parameters are made available to Pipeline steps via the params object

          Note: ticket JENKINS-64841 refers to the same problem but was wrongly closed.

          Robin Fauvel added a comment - I can also reproduce the problem described here. The parameter value should be accessible in the params object rather than (or in addition to) in the env object. This convention is described here and should be followed: https://www.jenkins.io/doc/book/pipeline/syntax/#parameters The values for these user-specified parameters are made available to Pipeline steps via the params object Note: ticket JENKINS-64841 refers to the same problem but was wrongly closed.

            Unassigned Unassigned
            snellingen Matias Snellingen
            Votes:
            13 Vote for this issue
            Watchers:
            15 Start watching this issue

              Created:
              Updated: