与字符串比较在詹金斯管道中不起作用 [英] Comparison with string doesn't work in Jenkins pipeline

查看:89
本文介绍了与字符串比较在詹金斯管道中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何下面的IF-ELSE起作用的想法

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

而以下一个则不

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数据类型进行比较?

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

推荐答案

尝试将.trim()添加到您的sh中.因为在输出中可以有换行,这会阻止正确比较.

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

这篇关于与字符串比较在詹金斯管道中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆