【发布时间】:2022-01-16 07:19:07
【问题描述】:
我正在尝试创建一个多阶段的 Jenkins 管道,第一阶段创建映像,第二阶段从新创建的映像创建新的 pod,第三阶段检查每个 pod 是否处于活动状态。
在第三阶段,其中一个命令是:
result = sh(returnStdout: true, script: "kubectl get pods --all-namespaces | grep -i <specific pod name> | wc -l").trim()
我希望收到的值是 1 或 0,但是当命令返回 0 时出现意外的 EOF 错误:
/var/jenkins_home/workspace/git-test@tmp/durable-c72ab15c/script.sh:第 1 行:在寻找匹配的 `'' 时出现意外 EOF
运行失败后,错误中的位置不存在。
我怎样才能得到我需要的价值?
舞台是这样的:
def isDone = false
def response = ""
for (int i = 0; i < 10 && isDone == false; i++) {
timeout(time: 60, unit: 'SECONDS') {
waitUntil {
response = sh(returnStdout: true, script: "kubectl get pods -o=name --all-namespaces --field-selector status.phase=Running | grep -i $testCaseName | grep . | awk '{ print (\$1 == \"\") ? \"0\" : \"1\" }'").trim()
if (response != "") {
return true
}
}
}
if (response == '1') {
sh "echo '$testCaseName pod is running'"
isDone = true
} else {
sh "echo '$testCaseName pod isn't running yet'"
sleep(time:1,unit:"MINUTES")
}
【问题讨论】:
-
我怀疑您的问题出在您的
awk命令中,因为您需要对引号进行双重转义。您可以尝试将此处的命令移到 shell 脚本文件中并将其保存在您的存储库中。或者只是将全部内容移到一个 shell 脚本中,然后你就可以运行它而无需通过 Jenkins 进行迭代 -
我怀疑意外的EOF来自
wc所以我将命令更改为:kubectl get pods --all-namespaces | grep -i $testCaseName | awk '{ print (\$4 == \"Running\") ? \"yes\" : \"no\" }'但现在我开始认为问题可能来自returnStdout -
@RandomGuy17,嘿,你成功了吗?
-
是的,解决方案是用
try catch包装 if 语句,并在 catch 部分中函数需要执行 else 情况。
标签: jenkins kubernetes kubernetes-pod