当我通过Docker使用Jenkins管道时,我想比较字符串值和Groovy脚本,但比较结果是错误的,以下脚本是我的groovy脚本和输出结果:
有没有人可以告诉我如何解决这个问题?
String runningContainerId = sh(script: "docker ps --filter=\"name=${appEnv}-${env.JOB_NAME}-${env.BUILD_NUMBER}\" -q | xargs", returnStdout: true)
//def runningImageId = sh(script: "docker images | grep ${appEnv}-${env.JOB_NAME}:${env.BUILD_NUMBER} | tr -s ' ' | cut -d ' ' -f 3 ", returnStdout: true)
println 'current container:' + runningContainerId
//println runningImageId
try{
def ret = sh(script: "docker ps --filter=\"name=${appEnv}-${env.JOB_NAME}\" -q | xargs", returnStdout: true)
def splitStr = ret.tokenize(" ")
println 'splitStr=>' + splitStr
String containerId = runningContainerId
println 'containerId=>' + containerId
for( String str in splitStr) {
print str == containerId
print 'str=>' + str
if ( str == containerId) {
print 'same string:' + str
} else {
print 'different string:' + str
}
}
Jenkins输出结果是:
splitStr=>[8abe81071b7f, 40e2292d5099, 4136bbdf9cc5]
[Pipeline] echo
containerId=>8abe81071b7f
[Pipeline] echo
false
[Pipeline] echo
str=>8abe81071b7f
[Pipeline] echo
different string:8abe81071b7f
[Pipeline] echo
false
[Pipeline] echo
str=>40e2292d5099
[Pipeline] echo
different string:40e2292d5099
[Pipeline] echo
false
[Pipeline] echo
str=>4136bbdf9cc5
[Pipeline] echo
different string:4136bbdf9cc5