Jenkins Pipeline Groovy 字符串比较不起作用

问题描述 投票:0回答:1

我已经为此苦苦挣扎了几个小时,尝试了几种不同的事情。我开始使用 switch/case,因为 txt.txt 文件仅包含数字状态 [0 .. 4]。 服务器的输出清楚地表明它是一个字符串,那么为什么它在两个条件语句(if..else、switch/case)中总是无法通过 strcmp 测试。这没有任何意义。

def output = readFile "$WORKSPACE/txt.txt"
println output.getClass()
println output
if (output == "1") { // also tried converting toString() both before and during the if stmt
  println "Jenkins Node diskspace starting to get a bit slim."
}
else {
  println "Plenty of disk space"
}

产生这个结果:

10:41:14  [Pipeline] stage
10:41:14  [Pipeline] { (Execute script to get disk usage status)
10:41:14  [Pipeline] script
10:41:14  [Pipeline] {
10:41:15  [Pipeline] readFile
10:41:15  [Pipeline] echo
10:41:15  class java.lang.String
10:41:15  [Pipeline] echo
10:41:15  1
10:41:15  
10:41:15  [Pipeline] echo
10:41:15  Plenty of disk space
10:41:15  [Pipeline] }
10:41:15  [Pipeline] // script
10:41:15  [Pipeline] }
10:41:15  [Pipeline] // stage
10:41:15  [Pipeline] }
10:41:15  
10:41:15  [Pipeline] // ansiColor
10:41:15  [Pipeline] }
10:41:15  [Pipeline] // node
10:41:15  [Pipeline] End of Pipeline
10:41:15  Finished: SUCCESS
jenkins groovy pipeline
1个回答
0
投票

文件中有新行符号。

10:41:15  [Pipeline] echo
10:41:15  1
10:41:15                        <<< an empty line after 1

你可以尝试做

if (output.trim() == "1") ...
© www.soinside.com 2019 - 2024. All rights reserved.