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

How can I trigger email notification on Pipeline build failure using the Email Notify pipe?

How do I add the logic to a pipe to check the build exit code and only send email upon failure?

3 answers

1 accepted

2 votes
Answer accepted

Hi @Doug Harriman , 

You should be able to check the exist code of the pipeline step in the after-script section of the step https://bitbucket.org/blog/after-scripts-now-available-for-bitbucket-pipelines

 

pipelines:
  default:
    - step:
        name: Build and test
        script:
          - npm install
          - npm test
        after-script:
          - if [[ BITBUCKET_EXIT_CODE -eq 0 ]]; then exit 0; else echo "Step failed"
- pipe: atlassian/email-notify:0.3.8
variables:
USERNAME: 'myemail@example.com'
PASSWORD: $PASSWORD
FROM: 'myemail@example.com'
TO: 'example@example.com'
HOST: 'smtp.gmail.com'
BODY_PLAIN: "Build failed!"
          

The BITBUCKET_EXIT_CODE variable is available in the after-script. The value will be 0 of the step is successful and 1 if the step has failed. 

Hi,

 

We tried to implement this suggestion, but get the following error : 

if [[ BITBUCKET_EXIT_CODE -eq 0 ]]; then exit 0; else echo "Step failed";bash: /opt/atlassian/pipelines/agent/tmp/bashScript1288869279188383501.sh: line 96: syntax error: unexpected end of file

 

Any input would be appreciated.

 

Thanks

Like Georgi Vladov likes this

Me too:

 

Fixes adding "fi":

 

if [[ BITBUCKET_EXIT_CODE -eq 0 ]]; then exit 0else echo "Step failed" fi
Like Radoslav Dragijev likes this

Did not work for me. Any other approach?

I thought I should be posting my question as a separate one so moving it to a separate thread here How to configure email notifications using Microso... (atlassian.com).

Thanks.

 

Hi @Alexander Zhukov ,

I am also getting same error like...

if [[ BITBUCKET_EXIT_CODE -eq 0 ]]; then exit 0; else echo "Step failed"bash: /opt/atlassian/pipelines/agent/tmp/bashScript4568911739432207606.sh: line 94: syntax error: unexpected end of file

 

Also i have tried @Goinnn 's trick, but i didn't work for me. Please help me.

Thanks.

I'm a bit late but try this:

if [[ BITBUCKET_EXIT_CODE -eq 0 ]]; then exit 0else echo "Step failed"; fi;

Also, did you get this implemented?

In the logs it says "Sending email..." and then "✖ Failed to send email to <TO-email>. Check your configuration settings."

What needs to be filled in 'username' and 'from' fields?

Dear did you able to fix this issue 
because I am getting the same error 
"failed to send email check your configuration setting "

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Bitbucket

New improvements to user management in Bitbucket Cloud 👥

Hey Community! We’re willing to wager that quite a few of you not only use Bitbucket, but administer it too. Our team is excited to share that we’ll be releasing improvements throughout this month of...

3,818 views 10 16
Read article

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