why my bash function does not work in Jenkinsfil

12/11/2019

I write this function

def docker_images(image_name,image_tag){

def curl_image_exists_status_code =

    curl_image_exists_status_code=sh (
    script:""" docker login -u XXXXX -p $$$  2> /dev/null 1>/dev/null
    docker pull XXXXX/'${image_name}':'${image_tag}' > /dev/null && echo "success" || echo 'failed' 
    """,  returnStdout: true)
    echo "${curl_image_exists_status_code}"
    return (curl_image_exists_status_code == 'success')?true: false
    }

but the output is

success

null

and the last line returns false the output of curl_image_exists_status_code should be success and the last line should return true but why it is not working?

-- lovecode
continuous-integration
docker
jenkins
jenkins-pipeline
kubernetes

2 Answers

12/11/2019

try

def docker_images(image_name,image_tag){

def curl_image_exists_status_code=sh (
    script:""" docker login -u XXXXX -p $$$  2> /dev/null 1>/dev/null
    docker pull XXXXX/'${image_name}':'${image_tag}' > /dev/null && echo "success" || echo 'failed' 
    """,  returnStdout: true)
    echo "${curl_image_exists_status_code}"
    return (curl_image_exists_status_code == 'success')?true: false
    }
-- yasin lachini
Source: StackOverflow

12/11/2019

You need to add .trim() to the output of your sh to be as follow:

def docker_images(image_name,image_tag){

   def curl_image_exists_status_code =

      curl_image_exists_status_code=sh (
      script:""" docker login -u XXXXX -p $$$  2> /dev/null 1>/dev/null
      docker pull XXXXX/'${image_name}':'${image_tag}' > /dev/null && echo "success" || echo 'failed' 
    """,  returnStdout: true).trim() //this will make your output trimmed
      echo "${curl_image_exists_status_code}"
      return (curl_image_exists_status_code == 'success')?true: false
    }
-- Marwan Hefny
Source: StackOverflow