Jenkins groovy如果步骤中的条件不起作用

tvz2xvvm  于 2023-08-02  发布在  Jenkins
关注(0)|答案(1)|浏览(116)

我有以下阶段在groovy脚本的jenkins作业”:

stage('Remove servers') {
            when {
                expression { params.DO_REMOVE == true }
            }
            steps {
                script {
                    parallel RemoveSource: {
                        sh """set -x
                            export KUBECONFIG=${source_config}                        
                            kubectl get ns ${source_namespace} || exists="False"                                                    
                        """
                        echo "${exists}"
                        if ("${exists}" != "False") {
                            build job: 'RemoveFCC',
                                parameters: [string(name: 'Branch', value: Branch),                                                                                          
                                             booleanParam(name: 'build_ansible', value: false),
                                             string(name: 'pipeline', value: 'yes')]
                        } else {
                            echo "Server does not exist. skipped fcc run"
                        }
                },
                RemoveTarget: {
                    sh """set -x
                        export KUBECONFIG=${target_config}         
                        kubectl get ns ${target_namespace} || exists="False"                                               
                    """
                    echo "${exists}"
                    if ("${exists}" != "False") {
                        build job: 'RemoveFCC',
                            parameters: [string(name: 'Branch', value: Branch),                                             
                                         booleanParam(name: 'build_ansible', value: false),
                                         string(name: 'pipeline', value: 'yes')]
                        } else {
                               echo "Server does not exist. skipped fcc run"
                        }                     
                    }
                }
            }
        }

字符串
即使echo "${exists}"打印False,if条件仍在执行。我不知道我错过了什么。尝试添加when而不是if

svdrlsy4

svdrlsy41#

你可以从sh块中修改这个变量,这样它就改变了“bash变量”,并且在if语句中检查groovy变量
当你执行sh块jenkins将从它创建bash脚本并在代理上执行,下一个sh块将不会从上一个块中获取任何变量,因为不同的shell
你不能从sh块中设置groovy var,你只能使用sh块的输出并解析它

stage('Remove servers') {
  when {
      expression { params.DO_REMOVE == true }
  }
  steps {
    script {
      parallel RemoveSource: {
        def sourceExists = sh (
          script: """
              set -x
              export KUBECONFIG=${source_config}
              kubectl get ns ${source_namespace}
          """,
          returnStdout: true
        ).trim() == ""

        echo "${sourceExists}"

        if (sourceExists) {
          build job: 'RemoveFCC',
            parameters: [
              string(name: 'Branch', value: Branch),
              booleanParam(name: 'build_ansible', value: false),
              string(name: 'pipeline', value: 'yes')]
        } else {
          echo "Server does not exist. skipped fcc run"
        }
      },
      RemoveTarget: {
        def targetExists = sh (
          script: """
            set -x
            export KUBECONFIG=${target_config}
            kubectl get ns ${target_namespace}
          """,
          returnStdout: true
        ).trim() == ""

      echo "${targetExists}"
      if (targetExists) {
        build job: 'RemoveFCC',
          parameters: [
            string(name: 'Branch', value: Branch),
            booleanParam(name: 'build_ansible', value: false),
            string(name: 'pipeline', value: 'yes')]
        } else {
          echo "Server does not exist. skipped fcc run"
        }
      }
    }
  }
}

字符串

相关问题