问题
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