How to send a build status email from Bitbucket Cloud Pipelines
Platform Notice: Cloud Only - This article only applies to Atlassian apps on the cloud platform.
Summary
This article highlights the process behind sending an email to a specific user with the pipeline build status of a build executed from Bitbucket Cloud Pipelines.
Solution
Pipelines build running on Atlassian's infrastructure or with a self-hosted Linux Docker runner
The Email notify pipe can be used to send an email from Bitbucket Pipelines by configuring it within your YML file:
Example: Sending an email using Google (Gmail):
image: atlassian/default-image:latest
pipelines:
default:
- step:
name: test email pipe
script:
- exit 1
after-script:
- STATUS="success"
- if [[ $BITBUCKET_EXIT_CODE -ne 0 ]]; then STATUS="FAILED" ; fi
- pipe: atlassian/email-notify:0.13.1
variables:
USERNAME: $USERNAME
PASSWORD: $APP_PASSWORD
FROM: 'SENDER'S_EMAIL_ID'
TO: 'RECIPIENT_EMAIL_ID'
HOST: 'smtp.gmail.com'
PORT: 587
SUBJECT: 'BUILD# ${BITBUCKET_BUILD_NUMBER} ${STATUS}:Bitbucket Pipe Notification for ${BITBUCKET_BRANCH}.'
Alternatively, if you'd like to send an email notification only when the build has failed, you can use the below YAML configuration:
image: atlassian/default-image:latest
pipelines:
default:
- step:
name: test email pipe
script:
- exit 1
after-script:
- STATUS="FAILED"
- if [[ $BITBUCKET_EXIT_CODE -ne 0 ]]; then echo "Step failed" ; else exit 0; fi
- pipe: atlassian/email-notify:0.13.1
variables:
USERNAME: $USERNAME
PASSWORD: $APP_PASSWORD
FROM: 'SENDER'S_EMAIL_ID'
TO: 'RECIPIENT_EMAIL_ID'
HOST: 'smtp.gmail.com'
PORT: 587
SUBJECT: 'BUILD# ${BITBUCKET_BUILD_NUMBER} ${STATUS}:Bitbucket Pipe Notification for ${BITBUCKET_BRANCH}.'
ℹ️ $USERNAME and $APP_PASSWORD are user-defined variables with values the email ID and the app password of the Google account. Please ensure you replace SENDER'S_EMAIL_ID and RECIPIENT_EMAIL_ID with the email address to send email from and the email address to send email to, respectively.
ℹ️ Please note that due to changes in Google not supporting less secure apps (see Google changes for less secure apps), you must create a Google app password to make this pipe work with a Google account.
If you are unable to configure build status emails after following this article, please raise a support ticket or a community support ticket for further assistance.
Pipelines builds running with a self-hosted Linux Shell, MacOS, or Windows runner
Pipes are not supported in Linux Shell, MacOS, and Windows runners, so you won't be able to use the email-notify pipe with these types of builds. If you run Pipelines builds with any of these runners, you can look into the following options:
A command-line email client compatible with the machine's OS on which the runner is running. If you install such a client on the machine, you should be able to use it in the after-script of a Pipelines step to send an email notification depending on the step's exit code.
An email API from your mail provider. If your email provider has an API endpoint that you can use to send emails, you could use that, for example, with curl in the after-script of a Pipelines step, to send an email notification.
Was this helpful?