Jenkins 管道 waitUntil bash 命令 returns 某个字符串
Jenkins Pipeline waitUntil bash command returns certain string
我有一个管道阶段,我等待从 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)
我错过了什么?
来自waitUntil
的帮助:
Runs its body repeatedly until it returns true. If it returns false, waits a while and tries again. --
您的执行输出看起来与它正在等待 output == "hello"
匹配完全一样。也许文件 /tmp/test.txt
的内容不完全是 hello
。你可能有一些空白,例如换行作为最后一个字符。
您可能需要添加 .trim()
shell 标准输出才能工作,即
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
}
}
}
}
我有一个管道阶段,我等待从 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)
我错过了什么?
来自waitUntil
的帮助:
Runs its body repeatedly until it returns true. If it returns false, waits a while and tries again. --
您的执行输出看起来与它正在等待 output == "hello"
匹配完全一样。也许文件 /tmp/test.txt
的内容不完全是 hello
。你可能有一些空白,例如换行作为最后一个字符。
您可能需要添加 .trim()
shell 标准输出才能工作,即
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
}
}
}
}