与字符串的比较在Jenkins管道中不起作用

时间:2018-04-05 16:43:13

标签: groovy jenkins-pipeline

知道下面IF-ELSE的工作原理

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

而下面的那个没有

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

为什么我需要专门将string转换为Integer,而无法将其作为string数据类型进行比较?

1 个答案:

答案 0 :(得分:5)

尝试将.trim()添加到sh。因为在输出中可能有new line,这会妨碍正确比较。