I am running a bunch of git operations in execute shell Jenkins configurations. I do see the error being generated, but Jenkins job still shows up as successful. How do I trap the error below so it fails the status of Jenkins job:
error: failed to push some refs to 'ssh://git@git
After editing jenkins shell git push to look like this:
git push "$target" --all | grep -z "error:" && exit 1
Jenkins job is still marked successful even though the error is occuring
15:24:06 ! [remote rejected] test/test/testBranch ->test/test/testBranch (pre-receive hook declined)
15:24:06 error: failed to push some refs to 'ssh://git@git.testing-repo/test/test-test.git'
15:24:06 hint: Updates were rejected because the remote contains work that you do
15:24:06 hint: not have locally. This is usually caused by another repository pushing
15:24:06 hint: to the same ref. You may want to first integrate the remote changes
15:24:06 hint: (e.g., 'git pull ...') before pushing again.
15:24:06 hint: See the 'Note about fast-forwards' in 'git push --help' for details.
15:24:06 Everything up-to-date
15:24:07 Finished: SUCCESS
EDIT #2 All the changes exist in Execute shell (#!/bin/bash) configuration part of jenkins.
Script:
RESPONSE=$(git push "$target" --all | grep "error:" || true)
if [ -n "$RESPONSE" ]; then
exit 1
fi
Output:
14:42:30 Cloning into bare repository 'testing-repo.git'...
14:44:25 From bitbucket.org:TEST/testing-repo
14:44:25 * branch HEAD -> FETCH_HEAD
14:44:29 remote:
14:44:29 ! [remote rejected] test/test/testBranch ->test/test/testBranch (pre-receive hook declined)
14:44:29 error: failed to push some refs to 'ssh://git@git.testing-repo/test/test-test.git'
14:44:29 hint: Updates were rejected because the remote contains work that you do
14:44:29 hint: not have locally. This is usually caused by another repository pushing
14:44:29 hint: to the same ref. You may want to first integrate the remote changes
14:44:29 hint: (e.g., 'git pull ...') before pushing again.
14:44:29 hint: See the 'Note about fast-forwards' in 'git push --help' for details.
14:44:29 Everything up-to-date
14:44:29 Finished: SUCCESS
EDIT #3: Actually when I am debugging in the shell $RESPONSE doesn't contain any data, so it would explain why it doesn't change the status of the jenkins job. So it seems even if git command actually did what it is supposed to, it did not feed output of the command into $RESPONSE
EDIT #4 RESPONSE=$(git push "$target" --all 2>&1 | grep "error:" || true) did the trick.

