Git If-else语句在Jenkins管道脚本(groovy)中工作不正常

Git If-else语句在Jenkins管道脚本(groovy)中工作不正常,git,jenkins,groovy,continuous-integration,jenkins-pipeline,Git,Jenkins,Groovy,Continuous Integration,Jenkins Pipeline,我有一个管道脚本(groovy),我试图使用if和else条件,但它的行为并不正常。似乎它总是返回false 以下是我的示例(剪贴)管道脚本: def branch_exists_in_node = 1 def branch_exists_in_common = 0 def branch_exists_in_extra = 0 if(branch_exists_in_node == 1) { NODE_BRANCH = en

我有一个管道脚本(groovy),我试图使用if和else条件,但它的行为并不正常。似乎它总是返回false

以下是我的示例(剪贴)管道脚本:

def branch_exists_in_node = 1
def branch_exists_in_common = 0
def branch_exists_in_extra = 0
                if(branch_exists_in_node == 1) {
                    NODE_BRANCH = env.BRANCH_NAME
                }else {
                    NODE_BRANCH = "master";
                }
                if(branch_exists_in_common == 1) {
                    COMMON_BRANCH = env.BRANCH_NAME
                }else {
                    COMMON_BRANCH = "master"
                }
但在这里,即使值为1,它也总是求值为false。语法有问题吗

当我对上述变量进行回显时,它打印得很好

                    echo "${branch_exists_in_angular}" //0
                    echo "${branch_exists_in_node}" //1
                    echo "${branch_exists_in_common}" //0
更新: 这是我的最小jenkins管道脚本,请帮助

def EXTRA_BRANCH
def ANGULAR_BRANCH
def NODE_BRANCH
def COMMON_BRANCH
def branch_exists_in_angular
def branch_exists_in_node
def branch_exists_in_common
def branch_exists_in_extra

pipeline {
    agent {
        label 'nimbus-cloud'
    }
    options {
        gitLabConnection('gitlab')
        timeout(time:1, unit: 'HOURS')
    }
    environment {
        WORK_DIR = "${WORKSPACE}/${BUILD_NUMBER}"
        EXTRA_REPO = "git@gitlab.example.com:tools/extra.git"
        COMMON_REPO = "git@gitlab.example.com:tools/common.git"
        ANGULAR_REPO = "git@gitlab.example.com:tools/angular.git"
        NODE_REPO = "git@gitlab.example.com:tools/node.git"
        EXTRA_BRANCH = "${env.BRANCH_NAME}"
    }
    stages {
        stage('PREDEPLOYMENT: Cleanup and Setting up the VM. '){
            steps {
                running("${JOB_NAME}")
                echo "Deleting previous images. "
                sh 'docker rmi -f $(docker images -a -q) | echo "Not able to delete some images"'
                dir("$WORKSPACE"){
                    sh 'rm -rf *'
                }
                // setting up
                echo "BRANCH NAME IS ${env.BRANCH_NAME}"
                script {
                    EXTRA_BRANCH = env.BRANCH_NAME // this will be different across all the repos
                // Check if above branch is already there on every repo -- for angular
                    try {
                        sshagent(['my-git-ssh']){
                            branch_exists_in_angular = sh(script: 'git ls-remote --heads $ANGULAR_REPO $EXTRA_BRANCH | wc -l', returnStdout: true)
                            echo "${branch_exists_in_angular}"
                            branch_exists_in_node = sh(script: 'git ls-remote --heads $NODE_REPO $EXTRA_BRANCH | wc -l', returnStdout: true)
                            echo "${branch_exists_in_node}"
                            branch_exists_in_common = sh(script: 'git ls-remote --heads $COMMON_REPO $EXTRA_BRANCH  | wc -l', returnStdout: true)
                            echo "${branch_exists_in_common}"
                        }
                    } catch(Exception e){
                        echo "WARN: something unexpected occured. "
                        echo "${e}"
                    }
                    // below lines prints as expected
                    echo "${branch_exists_in_angular}" // 0
                    echo "${branch_exists_in_node}" // 1
                    echo "${branch_exists_in_common}" //0
                    if(branch_exists_in_angular) {
                        ANGULAR_BRANCH = env.BRANCH_NAME
                    }else {
                        ANGULAR_BRANCH = "master";
                    }
                    if(branch_exists_in_node) {
                        NODE_BRANCH = env.BRANCH_NAME
                    }else {
                        NODE_BRANCH = "master";
                    }
                    if(branch_exists_in_common) {
                        COMMON_BRANCH = env.BRANCH_NAME
                    }else {
                        COMMON_BRANCH = "master"
                    }
                }
                echo ANGULAR_BRANCH // prints master = expected
                echo NODE_BRANCH // prints master but expected is checkout branch name feature-test
                echo COMMON_BRANCH // prints master expected
            }
            post {
                success {
                    echo "Success: VM Cleaned up for testing. "
                }
                failure {
                    echo "Error: Some error  occured while cleaning up the system. "
                    failure("${JOB_NAME}")
                }
            }
        }
    }
}
请记住
sh(returnStdout:true,script:…)
返回
String
因此
branch\u等变量存在于\u angular
中是字符串而不是数字。在Groovy(包括Jenkins Groovy CPS环境)中,以下表达式的计算结果总是
true

if ('0') {
    echo "0 is 0"
}

if ('1') {
    echo "1 is 1"
}
使用
(expr)作为整数将
sh
步骤的结果强制转换为整数

branch_exists_in_angular = sh(script: 'git ls-remote --heads $ANGULAR_REPO $EXTRA_BRANCH | wc -l', returnStdout: true) as Integer

它将使您的变量成为
整数类型
,然后
if(0)
将计算为
false
if(1)
将计算为
true

请显示完整值。您的变量可能在当前范围之外或类似的范围之外定义。确定。给我一个minute@SzymonStepniak我已经添加了代码…请检查哇,你救了我一天。。。从早上开始就在挣扎。多谢各位