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

hasProperty returns null on pipeline context (e.g. for Job Parameters)

XMLWordPrintable

      hasProperty does not work as expected when used on the this-context of a pipeline script. It should return true for existing parameter and false for non-existing parameter but it returns null in both cases. getProperty in contrast works as expected.

      Steps to reproduce:

       Create parameterized Pipeline job with following parameters:

      • string parameter myProperty and value "present" and the following pipeline script
      • // Pipeline Script
        pipeline {
            agent none
            stages {
                stage('HasPropertyTest') {
                    steps {
                        script {
                            echo "Value of 'myProperty': ${getProperty("myProperty")}"
                            echo "Got: ${hasProperty("myProperty")} - expected ${true}"
                            try {
                                echo "Value of 'missingProperty' ${getProperty("missingProperty")}"
                            } catch (MissingPropertyException) {
                                echo "Parameter 'missingProperty' does not exist."
                            }
                            echo "Got: ${hasProperty("missingProperty")} - expected ${false}"
                            if(hasProperty("myProperty")) {
                                // success
                            } else {
                                error "No Property: myProperty"
                            }
                        }
                    }
                }
            }
        }
        
        
      • No Groovy Sandbox

      Got Output:

      Running in Durability level: MAX_SURVIVABILITY
      [Pipeline] stage
      [Pipeline] { (HasPropertyTest)
      [Pipeline] script
      [Pipeline] {
      [Pipeline] echo
      Value of 'myProperty': present
      [Pipeline] echo
      Got: null - expected true
      [Pipeline] echo
      Parameter 'missingProperty' does not exist.
      [Pipeline] echo
      Got: null - expected false
      [Pipeline] error
      [Pipeline] }
      [Pipeline] // script
      [Pipeline] }
      [Pipeline] // stage
      [Pipeline] End of Pipeline
      ERROR: No Property: myProperty
      Finished: FAILURE
      

      Expected Output:

      Running in Durability level: MAX_SURVIVABILITY
      [Pipeline] stage
      [Pipeline] { (HasPropertyTest)
      [Pipeline] script
      [Pipeline] {
      [Pipeline] echo
      Value of 'myProperty': present
      [Pipeline] echo
      Got: true - expected true
      [Pipeline] echo
      Parameter 'missingProperty' does not exist.
      [Pipeline] echo
      Got: false - expected false
      [Pipeline] }
      [Pipeline] // script
      [Pipeline] }
      [Pipeline] // stage
      [Pipeline] End of Pipeline
      Finished: SUCCESS
      

       

            abayer Andrew Bayer
            seflue Sebastian Flügge
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: