Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Trying to fail in a bash if statement

indietravel July 19, 2018

I'd love to fail! Unfortunately, if there are exceptions in my deploy environment, I still pass!

My pipeline logs into a server, then runs the deploy script:

deploy.sh reads:

#!/bin/bash
#
# Deploy 2.0
#
# Runs from within Pipelines. Do not run manually.

# Set high expectations so that Pipelines fails on an error or exit 1
set -o pipefail

# Deploy code
echo "cd to /path/to/repo"
cd /path/to/repo
if [ -z "$(git status --porcelain)" ]; then
# Working directory clean
git checkout master
git pull origin master
git log -1
exit
else
# Uncommitted changes
echo "Uncommited changes. Failing..."
exit 1
fi

With `exit 1`, I'd expect my pipeline to fail, but it doesn't. I'm guessing that because the script accurately runs `exit 1` it doesn't fail? 

Any ideas on how to intentionally fail a pipeline from within a bash script? 

2 answers

1 accepted

0 votes
Answer accepted
Philip Hodder
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 23, 2018

Can you try adding

set -e

to the start of your bash script?

indietravel July 24, 2018

Thanks Philip. Still no dice. Here's the updated script where I try to brute-force that:

# Deploy 2.0
#
# Runs from within Pipelines. Do not run manually.

# Set high expectations so that Pipelines fails on an error or exit 1
set -e
set -o pipefail

# Deploy code
echo "cd to /data/*/scripts/toolkit"
cd /data/*/scripts/toolkit
if [ -z "$(git status --porcelain)" ]; then
# Working directory clean
git checkout master
git pull origin master
git log -1
exit
else
# Uncommitted changes
echo "Uncommited changes. Failing..."
set -e; exit 1;
fi

I tried setting it at the start, and then again right within the command block.

 

I have a dirty repo, so I get the correct echo response (Uncommited changes. Failing...) and then a Pipeline success message:

Screenshot 2018-07-24 11.43.25.png

Philip Hodder
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 25, 2018

I'm struggling to reproduce this. Can you add your bitbucket-pipelines.yml here?

indietravel July 25, 2018

Thanks for your help Philip. Here's the pipelines config:

pipelines:
branches:
master:
- step:
name: Deploy
deployment: production
script:
- echo "Deploying to prod";
- ssh user@ip "cd /path/to/repo && sh utils/deploy.sh"

 It's super simple! 

Philip Hodder
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 26, 2018

Hmmmm... Lets try some dumb things to investigate...

Change

ssh user@ip "cd /path/to/repo && sh utils/deploy.sh"

 to

ssh user@ip "cd /path/to/repo && sh utils/deploy.sh && echo $?" && echo $?

Lets see if the exit code is correct on your server, and what SSH returns. SSH is suppose to propagate the exit code. But will be good to be certain the issue is localised to your SSH session.

---

Also, can you verify to me that the script on your server has also been updated? Maybe through in (the cat command):

ssh user@ip "cd /path/to/repo && sh utils/deploy.sh && echo $? && cat utils/deploy.sh" && echo $?

just to be sure. :)

indietravel July 27, 2018

@Philip Hodder I have no idea what has changed, but this started working after sleeping on it for night. Looks like the code above is fine, so accepting your answer. Thanks for the hand-holding... Not sure what the issue is/was.

Philip Hodder
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 27, 2018

No problem. :)

0 votes
indietravel July 24, 2018

I just added two pieces of really bad grammar into the if statement that causes a normal bash script to error out and fail.

The build still passes.

#!/bin/bash
#
failme=((0/1))
echo $z
Thomas Kekeisen May 12, 2020

This works:

 

#!/bin/bash
#
set -e
set -o pipefail

failme=((0/1))
echo $z

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events