'Why getting "Build step 'Execute shell' marked build as failure" error while creating a backup tar for files

We are trying to run a job in jenkins server. Before deploying I am trying to create a tar file of all files and folders. Whenever I try to build job tar file is created successfully but I get "Build step 'Execute shell' marked build as failure" error and build fails.

below are the commands I have added in "Execute shell" section of job

echo "START" 
echo "-----GIT LOG-------" 
git log --pretty=format:"%H - %an, %ar, %ad : %s " -5  

git log --stat -1 
echo "------ARCHIVE------" 
tar -zcvf bblc.tar.gz *

below is the result I am getting after building job

**Build step 'Execute shell' marked build as failure
Archiving artifacts
[BFA] Scanning build for known causes...
[BFA] No failure causes found
[BFA] Done. 0s
Started calculate disk usage of build
Finished Calculation of disk usage of build in 0 seconds
Started calculate disk usage of workspace
Finished Calculation of disk usage of workspace in 0 seconds
Finished: FAILURE**

I am unable to find where I am going wrong?



Solution 1:[1]

Somehow your script is returning the non-zero value due to which Jenkins is making the build as failure.

If you are sure your script is working fine you can add exit 0 at the end of your script which will execute all steps and at the end return 0 which means success.

See below example:

echo "START" 
echo "-----GIT LOG-------" 
git log --pretty=format:"%H - %an, %ar, %ad : %s " -5  

git log --stat -1 
echo "------ARCHIVE------" 
tar -zcvf bblc.tar.gz *
exit 0

Otherwise you have check document of tar command(or whatever last command in your script) to understand why it returns non-zero value

Solution 2:[2]

From the log shown we cannot see is it one of the git commands or the tar command that is a problem.

Login to jenkins server, become jenkins user, cd into jobs workspace. Then run the commands in script and echo $? after each command.

If you see a command that does not return "0" then jenkins will exit at that point with "Build step 'Execute shell' marked build as failure" error. Good detail explaining jenkins execute shell on the answer here to this question: How/When does Execute Shell mark a build as failure in Jenkins? https://stackoverflow.com/a/22816074/623159

The tar command shown there looks suspect to me.

It is fine the first time it is run but it catches itself the next time it is run and you can see echo $? shows "1" - and any non-zero return code is an error and would cause jenkins to exit the script at that command and mark script as failed.

-bash-4.2$ mkdir tmp
$ cd tmp
$ touch foo
$ tar -zcvf bblc.tar.gz *
foo
$ echo $?
0
$ tar -zcvf bblc.tar.gz *
bblc.tar.gz
tar: bblc.tar.gz: File shrank by 115 bytes; padding with zeros
foo
$ echo $?
1

PROPER SOLUTION: be more specific with the tar command. Better to run it and place tarfile not in the directory being tarred e.g. put tar file into /tmp and move it to workspace afterwards. Don't just specify "*" give it explicit file and dir names.

tar -zcvf /tmp/bblc.tar.gz * && mv /tmp/bblc.tar.gz ./

or better

tar -zcvf /tmp/bblc.tar.gz dir1 dir2 dir3 file1 file2

There are some cases where you legitimately have a grep or a find that finds nothing and returns non-zero but it is correct.

OTHER SOLUTION: To continue execution when command fails:

tar -zcvf bblc.tar.gz * || true

The following question has this answer: don't fail jenkins build if execute shell fails

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Amit Nanaware
Solution 2 gaoithe