sh command giving "[[: not found " error when I am running through Jenkins stage

4/14/2020

In this Jenkin stage, I am trying to get the pod status and if the status is "TRUE" it will move to the next stage else wait for it.

      node('master'){
           withEnv(["KUBECONFIG=${JENKINS_HOME}/.kube/config"]){
                POSTGRES_CMD= """while [[ \$(kubectl get  pods -l app=postgres-${NAME_SPACE} -o 'jsonpath={..status.conditions[?(@.type=="Ready")].status}' -n ${NAME_SPACE}) != "True" ]]; do echo "waiting for pod" && sleep 1;done"""
                echo "${POSTGRES_CMD}"
                READY_POD = sh (
                        script: "${POSTGRES_CMD}",
                        returnStdout: true
                  ).trim()
                 echo "${READY_POD}"
Error:- /var/lib/jenkins/workspace/k8s@2@tmp/durable-95fdd/script.sh: [[: not found
-- me25
jenkins
kubernetes
linux

2 Answers

4/14/2020

That’s a bash feature. Most small container images use busybox or similar which is sh but not bash.

-- coderanger
Source: StackOverflow

4/14/2020

POSIX sh supports single bracket conditionals: []. You should be fine using $() for command substitution, in some environment you might need to use back ticks `

while [ "`echo \"true\"`" == "true" ]; do
  sleep 1
done

In any case, kubectl supports running that wait loop for you:

kubectl wait pods --for=condition=Ready -l app=postgres-${NAME_SPACE} -n ${NAMESPACE} --timeout=60s
-- Matt
Source: StackOverflow