我有一个流水线阶段,我等待从sh
脚本中取回某个字符串,并且只有当字符串匹配时,才继续进行下一个阶段,但是,它无法按预期进行:
node('master') {
stage("wait for bash completion") {
waitUntil {
def output = sh returnStdout: true, script: 'cat /tmp/test.txt'
output == "hello"
}
}
stage("execute after bash completed") {
echo "the file says hello!!!"
}
}
执行过程是这样的:
+ cat /tmp/test.txt
[Pipeline] }
Will try again after 0.25 sec
[Pipeline] {
[Pipeline] sh
[workspace] Running shell script
+ cat /tmp/test.txt
[Pipeline] }
Will try again after 0.3 sec
[Pipeline] {
[Pipeline] sh
[workspace] Running shell script
+ cat /tmp/test.txt
[Pipeline] }
Will try again after 0.36 sec
...
(so on and so forth)
我想念什么?
答案 0 :(得分:1)
在waitUntil
的帮助下:
反复运行其主体,直到返回true。如果返回false,请稍等片刻,然后重试。 -
您的执行输出看起来就像正在等待output == "hello"
匹配。也许文件/tmp/test.txt
的内容不完全是hello
。您可能在其中包含一些空格,例如换行作为最后一个字符。
答案 1 :(得分:1)
您可能需要添加.trim()
shell stdout才能工作,即
def output = sh(returnStdout: true, script: 'cat /tmp/test.txt').trim()
否则,您最终将在输出末尾显示换行符。
但可能更好的解决方案是使用groovy脚本:
steps {
sh "echo something > statusfile" // do something in the shell
waitUntil(initialRecurrencePeriod: 15000) {
script {
def status = readFile(file: "statusfile")
if ( status =~ "hello") {
return true
}else {
println("no hello yet!")
return false
}
}
}
}