I have a Jenkins pipeline that uses an if statement to check if a docker container is running. I run the following command to get the running state:
def containerStatus = sh(script: "ssh -o StrictHostKeyChecking=no -l <user> <server> 'docker container inspect -f '{{.State.Status}}' ${tagName}'", returnStdout: true)
I have added
echo containerStatus
and in the Jenkins console the output for this is "running"
However, when I have the following in the pipeline:
if(containerStatus.toString() == 'running'){
echo 'Initial status: Container running'
...some code...
}
this condition is not executed (I hit my defined error condition). I have also tried removing the .toString(), but no luck.
The complete stage in the pipeline is:
stage("Container") {
steps {
script{
def containerStatus = sh(script: "ssh -o StrictHostKeyChecking=no -l <user> <server> 'docker container inspect -f '{{.State.Status}}' ${tagName}'", returnStdout: true)
echo containerStatus
if(containerStatus.toString() == 'running'){
echo 'Initial status: Container running'
...some code...
}
else {
error "Container not running"
}
}
}
}
CodePudding user response:
You need to trim the resulted output:
def containerStatus = sh(script: "ssh -o StrictHostKeyChecking=no -l <user> <server> 'docker container inspect -f '{{.State.Status}}' ${tagName}'", returnStdout: true).trim()