0

I'm looking for an elegant or inelegant solution for failing a step in ADO pipelines when git commands return unexpected and unwanted results. In general, I call git with a bash task, like so:

steps:
- bash: |    
    git merge ${{ parameters.sourceBranch }}

If the merge fails, I would like this step to fail in my ADO pipeline. How would I go about doing that?

3
  • git merge exits nonzero on conflicts or other failures; how you get azure to respect the exit status from commands, as reported by bash, I don't know. Note that bash has its own exit status and the default is to exit with the status from the last-executed command, so given what you've shown here, if git merge exits with status 1, bash would also exit with status 1. In general 0 = success and any nonzero value = failure, but we're back to "what the heck does Azure do" (which I have no idea). Commented May 17, 2022 at 17:43
  • @Zeek Aran can I check - have you tried running the task as-is, and finding out what the pipeline does when the merge fails? Commented May 19, 2022 at 10:59
  • 1
    @VinceBowdren Yes, I'm here because some of my git commands have been silently failing for the last couple months. Oops! The setup is exactly as I have it above, and ADO doesn't care at all. Commented May 19, 2022 at 17:54

3 Answers 3

1

Given that the result of the git merge command should be not successful on bash I created the below bash condition. If result is correct nothing will be done, else exit for the bash will be returned

if git merge ${{ parameters.sourceBranch }} > /dev/null 2>&1; then
        echo success
else
        exit 1
fi
Sign up to request clarification or add additional context in comments.

2 Comments

This fails the step! Thank you. Can you explain this part? /dev/null 2>&1 It seems to eat the failure message. If possible, I'd like to have it return exit 1 on failure and also include the failure message from git.
@ZeekAran correct, you could do msg=$(git merge ${{ parameters.sourceBranch }} 2>&1) and then print the $msg
0

Found a somewhat elegant solution just by setting a variable to the output:

git merge ${{ parameters.sourceBranch }}
status=$?
[ $status -eq 0 ] || exit $status

$? is the last status code. The next line says "if the last status was 0, continue on, else/or: exit with that status code".

Comments

0

Would failOnStderr work? It looks like the bash step you're using is similar to script here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.