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

Pipeline build parameters are null in first build

      Parameters which are set in properties in a pipeline build are null the first time the build is run, even if they are given default values. On subsequent builds, the parameters have values as expected.

      Example: create a new Pipeline job and define the pipeline script like this in the build config:

      properties([
        [$class: 'ParametersDefinitionProperty',
          parameterDefinitions: [
            [$class: 'StringParameterDefinition',
              name: 'SOME_PARAMETER',
              defaultValue: 'some value',
              description: 'A test parameter']]
        ]
      ])
      
      echo "Value of parameter:"
      echo env.SOME_PARAMETER
      

      Run the build by clicking "Build" (note that this says "Build" rather than "Build with Parameters" unless the build has been run at least once). The console output is:

      [Pipeline] echo
      Value of parameter:
      [Pipeline] echo
      null
      

      Run the build a second time by clicking "Build with Parameters" and then confirming the default value. The console output is now:

      [Pipeline] echo
      Value of parameter:
      [Pipeline] echo
      some value
      

      If you refer to the parameter directly using SOME_PARAMETER rather than env.SOME_PARAMETER, the first build fails with a groovy.lang.MissingPropertyException instead.

      This isn't so bad when you're configuring a single new job, but it becomes a real problem for multibranch pipeline jobs, because the first build of every branch always fails unless the pipeline script does extra work to handle the missing parameter.

          [JENKINS-40574] Pipeline build parameters are null in first build

          Suzanne Hamilton created issue -
          Suzanne Hamilton made changes -
          Description Original: Parameters which are set in {{properties}} in a pipeline build are null the first time the build is run, even if they are given default values. On subsequent builds, the parameters have values as expected.

          Example: create a new Pipeline job and define the pipeline script like this in the build config:

          {code}
          properties([
            [$class: 'ParametersDefinitionProperty',
              parameterDefinitions: [
                [$class: 'StringParameterDefinition',
                  name: 'SOME_PARAMETER',
                  defaultValue: 'some value',
                  description: 'A test parameter']]
            ]
          ])

          echo "Value of parameter:"
          echo env.SOME_PARAMETER
          {code}

          Run the build by clicking "Build" (note that this says "Build" rather than "Build with Parameters"). The console output is:

          {code}
          [Pipeline] echo
          Value of parameter:
          [Pipeline] echo
          null
          {code}

          Run the build a second time by clicking "Build with Parameters" and then confirming the default value. The console output is now:

          {code}
          [Pipeline] echo
          Value of parameter:
          [Pipeline] echo
          some value
          {code}

          If you refer to the parameter directly using {{SOME_PARAMETER}} rather than {{env.SOME_PARAMETER}}, the first build fails with a {{groovy.lang.MissingPropertyException}} instead.

          This isn't so bad when you're configuring a single new job, but it becomes a real problem for multibranch pipeline jobs, because the first build of every branch always fails unless the pipeline script does extra work to handle the missing parameter.
          New: Parameters which are set in {{properties}} in a pipeline build are null the first time the build is run, even if they are given default values. On subsequent builds, the parameters have values as expected.

          Example: create a new Pipeline job and define the pipeline script like this in the build config:

          {code}
          properties([
            [$class: 'ParametersDefinitionProperty',
              parameterDefinitions: [
                [$class: 'StringParameterDefinition',
                  name: 'SOME_PARAMETER',
                  defaultValue: 'some value',
                  description: 'A test parameter']]
            ]
          ])

          echo "Value of parameter:"
          echo env.SOME_PARAMETER
          {code}

          Run the build by clicking "Build" (note that this says "Build" rather than "Build with Parameters" unless the build has been run at least once). The console output is:

          {code}
          [Pipeline] echo
          Value of parameter:
          [Pipeline] echo
          null
          {code}

          Run the build a second time by clicking "Build with Parameters" and then confirming the default value. The console output is now:

          {code}
          [Pipeline] echo
          Value of parameter:
          [Pipeline] echo
          some value
          {code}

          If you refer to the parameter directly using {{SOME_PARAMETER}} rather than {{env.SOME_PARAMETER}}, the first build fails with a {{groovy.lang.MissingPropertyException}} instead.

          This isn't so bad when you're configuring a single new job, but it becomes a real problem for multibranch pipeline jobs, because the first build of every branch always fails unless the pipeline script does extra work to handle the missing parameter.

          Suzanne Hamilton added a comment - This StackOverflow question seems to be referring to the same issue: http://stackoverflow.com/questions/40782302/jenkinsfile-parameter-properties-not-configured-in-jenkins-server-at-initial-bra

          Paul LeTang added a comment -

          You can get around this by using params.SOME_PARAMETER instead of env.SOME_PARAMETER.

          Paul LeTang added a comment - You can get around this by using params.SOME_PARAMETER instead of env.SOME_PARAMETER.

          Glib Briia added a comment -

          Tried using params.SOME_PARAMETER, no exception is thrown but parameter value is passed as 'null'. 

          As mentioned above this causing all initial builds to fail in multi branch pipelines configuration with automatic pipelines creation for new branches or pull requests. Bumping priority.

           

          Glib Briia added a comment - Tried using params.SOME_PARAMETER, no exception is thrown but parameter value is passed as 'null'.  As mentioned above this causing all initial builds to fail in multi branch pipelines configuration with automatic pipelines creation for new branches or pull requests. Bumping priority.  
          Glib Briia made changes -
          Priority Original: Minor [ 4 ] New: Major [ 3 ]

          anderson oliveria added a comment - - edited

          Hello, from Rio BRZ.

          The jenkins pipeline parameters are post-processed. This is a issue.

          Maybe the assignee can implement some sort of "constructor node" and use a two step process pipeline:
          filtering the first of "constructors node" and the pipeline parameters. Next, show the user interface, with the parameters set. At last, execute the pipeline.

          One sample of the "constructor example model", with preprocessing pipeline:
          --------------------------------------------------------------------
          import groovy.json.JsonSlurperClassic
          import java.text.SimpleDateFormat

          @NonCPS
          def jsonParse(def json) {
          new groovy.json.JsonSlurperClassic().parseText(json)
          }

          // Some static parameter ...
          def stringS="Start new VM"

          __node { //The Constructor Node idea...
          sh 'cat /var/lib/libvirt/dnsmasq/virbr0.status'

          def props = readFile file: '/var/lib/libvirt/dnsmasq/virbr0.status'
          def jsonProps = jsonParse(props)

          def params = jsonProps

          params.each

          { println it."ip-address" stringS = stringS+"\n"+it."ip-address" }

          println stringS
          }

          pipeline {

          agent any

          parameters

          { choice(choices: stringS, description: 'Opcoes de Comando para Ligar Maquina Virtual ou Desligar Maquina Virtual', name: 'VMCOMMAND') choice(choices: 'Do nothing\nSet Database\nStart interc2\nStop interc2', description: 'Opcoes de Comando para Ligar Maquina Virtual ou Desligar Maquina Virtual', name: 'VMARGS1') }

          anderson oliveria added a comment - - edited Hello, from Rio BRZ. The jenkins pipeline parameters are post-processed. This is a issue. Maybe the assignee can implement some sort of "constructor node" and use a two step process pipeline: filtering the first of "constructors node" and the pipeline parameters. Next, show the user interface, with the parameters set. At last, execute the pipeline. One sample of the "constructor example model", with preprocessing pipeline: -------------------------------------------------------------------- import groovy.json.JsonSlurperClassic import java.text.SimpleDateFormat @NonCPS def jsonParse(def json) { new groovy.json.JsonSlurperClassic().parseText(json) } // Some static parameter ... def stringS="Start new VM" __node { //The Constructor Node idea... sh 'cat /var/lib/libvirt/dnsmasq/virbr0.status' def props = readFile file: '/var/lib/libvirt/dnsmasq/virbr0.status' def jsonProps = jsonParse(props) def params = jsonProps params.each { println it."ip-address" stringS = stringS+"\n"+it."ip-address" } println stringS } pipeline { agent any parameters { choice(choices: stringS, description: 'Opcoes de Comando para Ligar Maquina Virtual ou Desligar Maquina Virtual', name: 'VMCOMMAND') choice(choices: 'Do nothing\nSet Database\nStart interc2\nStop interc2', description: 'Opcoes de Comando para Ligar Maquina Virtual ou Desligar Maquina Virtual', name: 'VMARGS1') }
          anderson oliveria made changes -
          Priority Original: Major [ 3 ] New: Critical [ 2 ]
          anderson oliveria made changes -
          Labels New: Parameters Pipeline
          anderson oliveria made changes -
          Labels Original: Parameters Pipeline New: Build Jenkinsfile Parameters Pipeline

            Unassigned Unassigned
            suzannehamilton Suzanne Hamilton
            Votes:
            9 Vote for this issue
            Watchers:
            17 Start watching this issue

              Created:
              Updated:
              Resolved: