Custom email notifications for step failures on windows runners
Platform Notice: Cloud Only - This article only applies to Atlassian products on the cloud platform.
Summary
As one may be aware, there is an email notify pipe available that allows notifying custom emails for each build step in a pipeline. However, pipes are not supported in the case of Windows runners due to the limitations of windows runners. So, one needs to explore workarounds. In the solution section, we'll cover one of them: using PowerShell scripts to send emails.
Solution
To use a PowerShell script to send emails with attachments in Bitbucket Pipelines, you can follow these steps:
Create a PowerShell script: Write a PowerShell script that handles sending emails. Here's a sample script that demonstrates sending an email with attachments using the
Send-MailMessage
cmdlet:sample email powershell script "your_script.ps1"
1 2 3 4 5 6 7 8 9 10 11 12
$smtpServer = "your_smtp_server" $smtpPort = "your_smtp_port" $smtpUsername = "your_smtp_username" $smtpPassword = "your_smtp_password" $fromAddress = "sender@example.com" $toAddress = "recipient@example.com" $subject = "Email Subject" $body = "Email Body" $attachmentPath = "path/to/attachment/file.txt" $smtpCreds = New-Object System.Management.Automation.PSCredential ($smtpUsername, (ConvertTo-SecureString -String $smtpPassword -AsPlainText -Force)) Send-MailMessage -From $fromAddress -To $toAddress -Subject $subject -Body $body -SmtpServer $smtpServer -Port $smtpPort -Credential $smtpCreds -Attachments $attachmentPath
Make sure to replace the placeholder values (
your_smtp_server
,your_smtp_port
, etc.) with your actual SMTP server details.Add PowerShell step in Bitbucket pipeline YAML: Open your Bitbucket pipeline YAML file and add a
after-script
step that executes the PowerShell script during a step failure. Here's an example of how the YAML configuration may look:bitbucket pipelines YAML file
1 2 3 4 5 6 7
pipelines: default: - step: name: Send Email image: mcr.microsoft.com/powershell after-script: - if [ $BITBUCKET_EXIT_CODE -eq 1 ]; then powershell -File path/to/your_script.ps1; fi
ℹ️ Make sure to replace
path/to/your_script.ps1
it with the actual path to your PowerShell script.
Customize the YAML as needed: Modify the pipeline YAML file according to your specific requirements, such as adding other steps, configuring environment variables, or defining triggers.
Commit and push changes: Save the changes to your pipeline YAML file and commit them to your repository. Push the changes to trigger the pipeline and execute the PowerShell script.
With the above-mentioned steps, the PowerShell script will be executed as an after-script
step in your Bitbucket pipeline, sending an email with the specified attachments upon completion.
Was this helpful?