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?
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:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I'm struggling to reproduce this. Can you add your bitbucket-pipelines.yml here?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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. :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
No problem. :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
This works:
#!/bin/bash
#
set -e
set -o pipefail
failme=((0/1))
echo $z
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.