Send an Email notification when Github Actions fails

Ravgeet Dhillon
Ravgeet Dhillon on Dec 03, 20205 min read
Written in
Blog banner for Send an Email notification when Github Actions 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 open-source community. Some of the community members asked us about how they can send an email notification when a Github Action fails. So to take in the request, today we will see how we can build a workflow that allows us to achieve this purpose.

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, we often want to notify more than one team member about the possible failure of the workflow.

Contents

1. Create a sample workflow

Let us write a simple workflow that prints the infamous Hello World. Create a new file build.yml in .github/workflows directory and add:

name: Build

on:
  push:
    branches: main

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - name: Hello World
        run: echo Hello, world!
Github Action executed successfully
Github Action executed successfully

2. Add Send Email Action

Dawid Dziurla has published a Github action that allows us to configure a lot of aspects related to sending the emails. We just need to add the below step to our 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

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. We use the workflow’s context variables to build our email subject and body. Don’t forget to add your username and password as Action secrets.

Make sure to use App-Specific password for the above action. Learn how to create an app-specific password for GMail.

Results

Before testing the action in use, let us deliberately fail the action. All we need to do is update the Hello World step’s run command to echo Hello, world! && exit 1. exit 1 sets an exit status of 1 which tells the Github Actions that some kind of error has occurred. Let push our code and see what happens.

Github Actions workflow failed deliberately
Github Actions workflow failed deliberately

From the above screenshot, we can see that the Send mail step was executed even though the previous step failed. Let us check our inbox for the email about the failure.

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

Sweet! We can see that email notification was sent to our recipients. The subject and body were populated with the appropriate repository and workflow.

Github Actions is a great CI/CD tool. By using the right actions, we can build workflows that help in boosting team productivity at any workspace. If you any doubts or appreciation for our team, let us know in the comments below.

Subscribe to RavSam Web Solutions newletter for information about Website development, mobile apps development and software development

Loved this post? Join our Newsletter 🚀

We write about Nuxt, 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, Freelancer and Developer at RavSam. He helps startups, businesses, open-source organizations with Digital Product Development. He is a fan of Jamstack and likes to work with Flutter, Strapi, Nuxt/Vue and Python. Connect with Ravgeet on Twitter and LinkedIn.

KEEP READING

Blog banner for Script as a Task using VS Code IDE

Script as a Task using VS Code IDE

Written in Automation
Blog banner for Deploy a Serverless Probot/Github App on Netlify Functions

Deploy a Serverless Probot/Github App on Netlify Functions

Written in Automation

Got a project or partnership in mind?

Let's Talk

Contact Us ->