Capture exit code from bash scripts in jenkins groovy scripts

Foobar-naut picture Foobar-naut · Jan 9, 2018 · Viewed 14.2k times · Source

Executing a bash script copy_file.sh from Jenkins Groovy script and trying to shoot mail depending upon the exit code generated form the bash script.

copy_file.sh:

#!/bin/bash

$dir_1=/some/path
$dir_2=/some/other/path

if [ ! -d $dir ]; then
  echo "Directory $dir does not exist"
  exit 1
else
  cp $dir_2/file.txt $dir_1
  if [ $? -eq 0 ]; then
      echo "File copied successfully"
  else
      echo "File copy failed"
      exit 1
  fi
fi

Portion of the groovy script:

stage("Copy file")  {
    def rc = sh(script: "copy_file.sh", returnStatus: true)
    echo "Return value of copy_file.sh: ${rc}"
    if (rc != 0) 
    { 
        mail body: 'Failed!',       
        subject: 'File copy failed',        
        to: "[email protected]"       
        System.exit(0)
    } 
    else 
    {
        mail body: 'Passed!',   
        subject: 'File copy successful',
        to: "[email protected]"
    }
}

Now, irrespective of the exit 1s in bash script, groovy script is always getting return code 0 in rc and shooting Passed! mails!

Any suggestions why I can not receive the exit code from bash script in this Groovy script?

DO I NEED TO USE RETURN CODE INSTEAD OF EXIT CODE?

Answer

natansun picture natansun · Aug 30, 2018

Your groovy code is OK.

I created a new pipeline job to check your problem, but altered it a bit.

Instead of running your shell script copy_file.sh I created ~/exit_with_1.sh script, that only exits with an exit code of 1.

The job has 2 steps:

  1. Creation of ~/exit_with_1.sh script

  2. Run the script and check the exit code that stored in rc.

I got 1 as an exit code in this example. If you think something wrong with your groovy <-> bash configuration, consider replace your copy_file.sh content with only exit 1 then try to print the result (before posting the emails).

The jenkins job I created:

node('master') {
    stage("Create script with exit code 1"){
            // script path
            SCRIPT_PATH = "~/exit_with_1.sh"

            // create the script
            sh "echo '# This script exits with 1' > ${SCRIPT_PATH}"
            sh "echo 'exit 1'                    >> ${SCRIPT_PATH}"

            // print it, just in case
            sh "cat ${SCRIPT_PATH}"

            // grant run permissions
            sh "chmod +x ${SCRIPT_PATH}"
    }
    stage("Copy file")  {
        // script path
        SCRIPT_PATH = "~/exit_with_1.sh"

       // invoke script, and save exit code in "rc"
        echo 'Running the exit script...'
        rc = sh(script: "${SCRIPT_PATH}", returnStatus: true)

        // check exit code
        sh "echo \"exit code is : ${rc}\""

        if (rc != 0) 
        { 
            sh "echo 'exit code is NOT zero'"
        } 
        else 
        {
            sh "echo 'exit code is zero'"
        }
    }
    post {
        always {
            // remove script
            sh "rm ${SCRIPT_PATH}"
        }
    }
}