Reputation: 247
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://[email protected]/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://[email protected]/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.
Upvotes: 2
Views: 3311
Reputation: 1740
This issue is strange because this git push
failed so it should return an exit code different from 0
which then should fail your build. The exit code of last command of the Jenkin's Execute Shell build step is what determines the success/failure of the Build Step. 0
- success, anything else - failure. If you can provide more data maybe I could debug this. What exit code is being returned after your git push? (You can check exit code of last command by typing: echo $?
) Can you also add some example?
But don't worry I also have a simple workaround for you (Maybe not pretty but it works). You can always manually say to Jenkins to fail build by setting error code by yourself. In this case, you can grep
your git push for error. If there is, exit with code 1.
git push ... | grep -z "error: failed to push some refs to" && exit 1 || true
Or more generic:
git push ... | grep -z "error:" && exit 1 || true
Where do you exactly run this git push
command? In execute shell build step? Do you use Jenkins Source Code Management? (It will make easier to work with repositories inside Jenkins build)
Dummy job to show that exit 1
will force failure:
you can always try doing the same more bash script like:
RESPONSE=$(git push ... | grep -z "error:" || true)
if [ -n "$RESPONSE" ]; then
exit 1
fi
also, I've added || true
to workaround commands because I have forgotten that grep
will exit 1
if no line is selected:
Exit status is 0 if any line is selected, 1 otherwise
Which means that this would force fail all of your builds.
Upvotes: 3