Home > OS >  What will be the groovy syntax to return null as a null object not a string in Jenkins pipeline
What will be the groovy syntax to return null as a null object not a string in Jenkins pipeline

Time:01-15

BRANCH = "develop" 

BRANCH = "${BRANCH=="develop"?"null":"${BRANCH}"}"

print (BRANCH.getClass()) # class org.codehaus.groovy.runtime.GStringImpl

What is the correct syntax for null to be treated as a NUllObject? Expecting to return class org.codehaus.groovy.runtime.NullObject

CodePudding user response:

Your ternary currently returns a string with the characters null, so you would need to need modify to:

BRANCH = BRANCH == 'develop' ? null : BRANCH

CodePudding user response:

As a very minimal example to actually run as a pipeline, you can use this:

pipeline {
  agent any

  stages {
    stage('Hello') {
      steps {
        script {
          def BRANCH = "develop" 
          BRANCH = "${BRANCH}" == "develop" ? null : "${BRANCH}"
          print (BRANCH.getClass())
        }
      }
    }
  }
}

The output looks then like this: console output

  • Related