在Jenkins管道中,将null作为空对象而不是字符串返回的groovy语法是什么

htrmnn0y  于 2023-01-16  发布在  Jenkins
关注(0)|答案(2)|浏览(164)
BRANCH = "develop" 

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

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

将null视为NUllObject的正确语法是什么?期望返回类org.codehaus.groovy.runtime.NullObject

ryhaxcpt

ryhaxcpt1#

您的三进制当前返回字符串null,因此您需要修改为:

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

mo49yndu2#

作为实际作为管道运行的最小示例,您可以使用以下代码:

pipeline {
  agent any

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

输出如下所示:

相关问题