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: "xyz@abc.com"
System.exit(0)
}
else
{
mail body: 'Passed!',
subject: 'File copy successful',
to: "xyz@abc.com"
}
}
Now, irrespective of the exit 1
s 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?