How do I add the logic to a pipe to check the build exit code and only send email upon failure?
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 [[ <span>BITBUCKET_EXIT_CODE -eq 0 </span>]]; then exit 0; else echo "Step failed"<br> - <span>pipe</span><span>:</span> <span>atlassian/email-notify:0.3.8</span> <br><span> variables</span><span>:</span> <br><span> USERNAME</span><span>:</span> <span>'myemail@example.com'</span> <br><span> PASSWORD</span><span>:</span> <span>$PASSWORD</span> <br><span> FROM</span><span>:</span> <span>'myemail@example.com'</span> <br><span> TO</span><span>:</span> <span>'example@example.com'</span> <br><span> HOST</span><span>:</span> <span>'smtp.gmail.com'<br></span> BODY_PLAIN: "Build failed!"<br>
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 :
<span>if [[ BITBUCKET_EXIT_CODE -eq 0 ]]; then exit 0; else echo "Step failed";</span><span>bash: /opt/atlassian/pipelines/agent/tmp/bashScript1288869279188383501.sh: line 96: syntax error: unexpected end of file</span>
<span>if [[ BITBUCKET_EXIT_CODE -eq 0 ]]; then exit 0; else echo "Step failed";</span>
<span>bash: /opt/atlassian/pipelines/agent/tmp/bashScript1288869279188383501.sh: line 96: syntax error: unexpected end of file</span>
Any input would be appreciated.
Thanks
Me too:
Fixes adding "fi":
<span>if</span> [[ <span>BITBUCKET_EXIT_CODE -eq 0 </span>]]; then exit <span>0</span>; <span>else</span> echo <span>"Step failed" <u><strong>fi</strong></u></span>
Did not work for me. Any other approach?
Hi @Alexander Zhukov ,
I am also getting same error like...
<span>if [[ BITBUCKET_EXIT_CODE -eq 0 ]]; then exit 0; else echo "Step failed"</span><span>bash: /opt/atlassian/pipelines/agent/tmp/bashScript4568911739432207606.sh: line 94: syntax error: unexpected end of file</span>
<span>if [[ BITBUCKET_EXIT_CODE -eq 0 ]]; then exit 0; else echo "Step failed"</span>
<span>bash: /opt/atlassian/pipelines/agent/tmp/bashScript4568911739432207606.sh: line 94: syntax error: unexpected end of file</span>
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:
<span>if</span> [[ <span>BITBUCKET_EXIT_CODE -eq 0 </span>]]; then exit <span>0</span>; <span>else</span> echo <span>"Step failed"<strong>;</strong> <u><strong>fi;</strong></u></span>
Also, did you get this implemented?
In the logs it says "Sending email..." and then "<span>✖ Failed to send email to <TO-email>. Check your configuration settings."</span>
<span>✖ Failed to send email to <TO-email>. Check your configuration settings."</span>
What needs to be filled in 'username' and 'from' fields?
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.
Dear did you able to fix this issue because I am getting the same error "failed to send email check your configuration setting "
getting same can some help if its working for them.
If anyone is still struggling to get the above snippet to work.
You can turn on notifications from the repo itself in Bitbucket.
1. Go the repository you want to monitor
2. On the Source tab, click the ellipsis and select Manage Notifications
3. Click Watch this repository
Now when a pipeline fails you should get an email alert
Thank you so much! That is such a weird place to put that setting, I never would have found it.
It looks like you're new here. Sign in or register to get started.