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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Trying to fail in a bash if statement

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

Can you try adding

set -e

to the start of your bash script?

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

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

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! 

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. :)

@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.

No problem. :)

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

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
Community showcase
Posted in Bitbucket

How do you use the Bitbucket-Jira integration? How does it help your team?

Hey everyone, over the past year, we've added several new features on how you can use Bitbucket and Jira together. We'd love to know how you'll are using the integration and how it's helping your t...

120 views 0 2
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you