If else statement not working properly in Jenkins pipeline script (groovy)

家住魔仙堡 提交于 2019-12-14 03:00:52

问题


I have the pipeline script (groovy) where i m trying to use if and else condition but its not behaving normally. Seems it always returns false.

Here is my sample (snip of) pipeline script:

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"
                }

But here it always evaluating to false even if the value is 1. Is there any issue with the syntax?

when i do echo of above variables, it prints well..

                    echo "${branch_exists_in_angular}" //0
                    echo "${branch_exists_in_node}" //1
                    echo "${branch_exists_in_common}" //0

UPDATE: Here is my minimal jenkins pipeline script, please help

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}")
                }
            }
        }
    }
}

回答1:


Keep in mind that sh(returnStdout: true, script: ...) returns String so variables like branch_exists_in_angular are strings and not numbers. In Groovy (including Jenkins Groovy CPS environment) following expressions always evaluate to true:

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

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

Cast the result of sh step to an integer using (expr) as Integer:

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

It will make your variables to be a type of Integer and then if (0) will evaluate to false and if (1) will evaluate to true.



来源:https://stackoverflow.com/questions/51379224/if-else-statement-not-working-properly-in-jenkins-pipeline-script-groovy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!