Comparison with string doesn't work in Jenkins pipeline

自闭症网瘾萝莉.ら 提交于 2019-12-11 04:06:39

问题


Any idea why the IF-ELSE below works

def checkPrValidity() {
    wordCountStr = sh returnStdout: true, script: 'git diff --ignore-space-at-eol $target_branch..PRbranch src | wc -l' 
    wordCount = wordCountStr.toInteger() //force conversion to int data type 
    if (wordCount == 0) {
        return false;
    } else {
        println("This is a valid PR, continue the job execution")
        return true;
    }
}

while the one below doesn't

def checkPrValidity() {
    wordCountStr = sh returnStdout: true, script: 'git diff --ignore-space-at-eol $target_branch..PRbranch src | wc -l'
    if (wordCountStr == '0') {
        return false;
    } else {
        println("This is a valid PR, continue the job execution")
        return true;
    }
}

Why do i need to specifically convert a string to Integer, while it fails to compare it as a string data type?


回答1:


Try to add .trim() to your sh. Because in output there can be new line, which prevents correct comparison.



来源:https://stackoverflow.com/questions/49677494/comparison-with-string-doesnt-work-in-jenkins-pipeline

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