【问题标题】:Jenkins pipeline with kubectl get pods return "unexpected EOF"带有 kubectl get pods 的 Jenkins 管道返回“意外的 EOF”
【发布时间】: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


【解决方案1】:

我仍然不确定导致此问题的正确原因,但我假设它来自 Jenkins sh 命令的 readStdout 标志。

此问题的解决方案是使用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")
    }
  }
}

【讨论】:

    猜你喜欢
    • 2017-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-21
    • 2021-11-05
    • 2021-10-06
    相关资源
    最近更新 更多