Issue
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
Solution
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
Answered By - Matt Answer Checked By - Willingham (WPSolving Volunteer)