我正在嘗試創建一個多階段的 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")
}
uj5u.com熱心網友回復:
我仍然不確定這個問題的正確原因,但我假設它來自readStdoutJenkinssh命令的標志。
這個問題的解決方案是用try catch導致unexpected EOF錯誤的命令包裝,這里是if部分,并將 else 部分放在該catch部分中。它看起來像這樣:
def isDone = false
def response = ""
def state = ""
sh "mkdir -p ${workspace}/results/${currentBuildNumber}/$testCaseName"
for (int i = 0; i < 10 && isDone == false; i ) {
sh "kubectl get pods --all-namespaces | grep -i $testCaseName | awk '{ printf (\$4 == \"Running\") ? \"yes\" : \"'wait'\" }' > ${workspace}/results/${currentBuildNumber}/$testCaseName/commandResult.txt"
sh "kubectl get pods --all-namespaces | grep -i $testCaseName | awk '{ printf \$4 }' > ${workspace}/results/${currentBuildNumber}/$testCaseName/podState.txt"
timeout(time:30,unit:"SECONDS") {
waitUntil {
response = readFile "${workspace}/results/${currentBuildNumber}/$testCaseName/commandResult.txt"
return (response != "")
}
}
timeout(time:30,unit:"SECONDS") {
waitUntil {
state = readFile "${workspace}/results/${currentBuildNumber}/$testCaseName/podState.txt"
return (state != "")
}
}
try {
if ("$response" == "yes") {
println("$testCaseName pod is running")
isDone = true
} else {
println("$testCaseName pod state is: $state")
sleep(time:1,unit:"MINUTES")
}
} catch(Exception e) {
println("$testCaseName pod state is: $state")
if ((i == 9) && (isDone == true)) {
error("Build failed because $testCaseName pod couldn't start")
} else {
sleep(time:1,unit:"MINUTES")
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/388733.html
