Home > Net >  Jenkins pipeline with kubectl get pods return "unexpected EOF"
Jenkins pipeline with kubectl get pods return "unexpected EOF"

Time:12-17

I'm trying to create a Jenkins pipeline with multi-stages, the first stage creates the image, the second stage create new pods from the newly created image and the third stage checks if each pods is alive.

In the third stage one of the commands is:

result = sh(returnStdout: true, script: "kubectl get pods --all-namespaces | grep -i <specific pod name> | wc -l").trim()

and the value I expect to receive is 1 or 0 but when the command return 0 there is an unexpected EOF error:

/var/jenkins_home/workspace/git-test@tmp/durable-c72ab15c/script.sh: line 1: unexpected EOF while looking for matching `''

after the run failed the location from the error doesn't exist.

How can I get the value I need?

The Stage looks like this:

  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")
    }

CodePudding user response:

I still don't know for sure the right cause for this problem but I'm assuming that it came from the readStdout flag from the Jenkins sh command.

The solution for this problem is to wrap with try catch the command that causes the unexpected EOF error which here is the if section and put the else section in the catch section. it looks like this:

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")
    }
  }
}
  • Related