We recently published a blog on how to send a slack notification when a github action fails. We got a great response from the community and some of the community members asked about how they can send an email notification when a Github Action fails.
Github has this feature natively that sends an email when a Github Action fails. It works efficiently when you are working on an individual project. However, when working in a team, you often want to notify more than one team member about the possible failure of the workflow.
In this blog, you’ll see how you can build a workflow that allows you to achieve this purpose.
Contents
Creating a Sample Workflow
Let’s write a simple Github Actions workflow that prints the infamous Hello World. Create a new file build.yml in .github/workflows directory and add the following code to it:
name: Build
on:
push:
branches: main
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Hello World
run: echo Hello, world!
Commit and push your code to Github and run the above Github Action.
Adding Send Email Action
Dawid Dziurla has published a Github action that allows you to configure a lot of aspects related to sending the emails. In build.yml, add the following step to your workflow:
- name: Send mail
if: always()
uses: dawidd6/action-send-mail@v2
with:
# mail server settings
server_address: smtp.gmail.com
server_port: 465
# user credentials
username: ${{ secrets.EMAIL_USERNAME }}
password: ${{ secrets.EMAIL_PASSWORD }}
# email subject
subject: ${{ github.job }} job of ${{ github.repository }} has ${{ job.status }}
# email body as text
body: ${{ github.job }} job in worflow ${{ github.workflow }} of ${{ github.repository }} has ${{ job.status }}
# comma-separated string, send email to
to: johndoe@gmail.com,doejohn@gmail.com
# from email name
from: John Doe
You can use
echo "${{ toJson(github) }}"
to get more workflow context variables.
The if: always()
directive tells the Github Actions to always run this step regardless of whether the preceding steps have been executed successfully or not. You use the workflow’s context variables to build your email subject and body. Don’t forget to add your username and password as Github Action secrets.
Make sure to use the App-Specific password for the above action. Learn how to create an app-specific password for GMail.
Results
Before testing the action in use, deliberately fail the action. All you need to do is update the Hello World step’s run command to echo Hello, world! && exit 1
. This sets an exit status of 1 which tells the Github Actions that some error has occurred.
Commit and push your code to Github and see what happens. You can see that the Send mail step was executed even though the previous step failed.
Check your inbox for the email about the failure.
Sweet! You can see that the email notification was sent to the recipients specified in the Send mail step. The subject and body were also populated with the appropriate repository and workflow.
Github Actions is a great CI/CD tool. By using the right actions, you can build workflows that can help in boosting team productivity at any workspace.