Send a custom email notification to multiple recipients when a Github Actions workflow fails or succeeds.

Send an Email notification when Github Action fails

Ravgeet Dhillon

Ravgeet Dhillon

Updated on Nov 29, 2021 in Automation

⏱ 5 min read

Blog banner for Send an Email notification when Github Action fails

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.

Github Action executed successfully
Github Action completed with success

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.

Github Actions workflow failed deliberately
Github Actions workflow failed deliberately

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.

Email Notification sent about failed Github Action
Email Notification about the failed Github Action

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.

📫

Loved this post? Join our Newsletter.

We write about React, Vue, Flutter, Strapi, Python and Automation. We don't spam.

Please add a valid email.
By clicking submit button, you agree to our privacy policy.
Thanks for subscribing to our newsletter.
There was some problem while registering your newsletter subscription. Please try again after some time or notify the owners at info@ravsam.in

ABOUT AUTHOR

Ravgeet Dhillon

Ravgeet is a Co-Founder and Developer at RavSam. He helps startups, businesses, open-source organizations with Digital Product Development and Technical Content Writing. He is a fan of Jamstack and likes to work with React, Vue, Flutter, Strapi, Node, Laravel and Python. He loves to play outdoor sports and cycles every day.

TAGGED WITH

Got a project or partnership in mind?

Let's Talk

Contact Us ->