Jenkins 脚本管道:Groovy if 语句

时间:2021-06-25 09:22:36

标签: jenkins groovy

我似乎无法实际打印变量 glooNamespaceExists。我在控制台中看到来自 kubectl 的响应在那里,但变量似乎是 null - I'如果命名空间已经存在,我希望跳过整个构建阶段..

    stage('Setup Gloo Ingress Controller') {
      def glooNamespaceExists = sh(script: "kubectl get ns gloo-system -o jsonpath='{.status.phase}'")

      if (glooNamespaceExists != 'Active') {
        sh 'helm repo add gloo https://storage.googleapis.com/solo-public-helm'
        sh 'helm repo update'
        sh 'kubectl create namespace gloo-system'
        sh 'helm install gloo gloo/gloo --namespace gloo-system'
      }
    }

编辑:运行的控制台输出:

[Pipeline] { (Setup Gloo Ingress Controller)
[Pipeline] sh
+ kubectl get ns gloo-system -o jsonpath={.status.phase}
Active
[Pipeline] sh
+ helm repo add gloo https://storage.googleapis.com/solo-public-helm

1 个答案:

答案 0 :(得分:2)

请将 returnStdout: true 添加到您的脚本命令中,这将返回准确的输出。否则它将返回状态代码 which 0。请像这样添加returnStdout: true,以便您的命令应该是这样的,

def glooNamespaceExists = sh(script: "kubectl get ns gloo-system -o jsonpath='{.status.phase}'", returnStdout: true)