Send an Email notification when Github Actions fails
Tags: email, automation, githubactions
Published: 2020-12-05 07:37:47

---
canonical_url: https://www.ravsam.in/blog/send-an-email-notification-when-github-actions-fails/
date: 2020-12-04 06:36:36 UTC
published: true
tags: email,automation,githubactions
title: 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.

- 1. Create a sample workflow

- 2. Add Send Email Action

- Results

#### 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:

```yml
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](https://cdn-images-1.medium.com/max/1024/0*3s8OnPfyqAo6fSPE.png)<figcaption>Github Action executed successfully</figcaption>

#### 2. Add Send Email Action

[Dawid Dziurla](https://github.com/dawidd6/action-send-mail) 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:

```yml
- 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](https://support.google.com/mail/answer/185833?hl=en-GB).

#### 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](https://cdn-images-1.medium.com/max/1024/0*diu0XokYoCw6eutf.png)<figcaption>Github Actions workflow failed deliberately</figcaption>

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](https://cdn-images-1.medium.com/max/1024/0*3gnbohaBi4Gi2TwC.png)<figcaption>Email Notification sent about failed Github Action</figcaption>

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.

#### About Us

We are helping businesses around the world setup automation techniques to boost their productivity and eliminate human errors. Get in touch with us to know more about our automation software development services.

We provide Web Design, Web Development, Mobile App Development, Software Development, and Automation Services. We have been rated as one of the fastest-growing companies in India. We have been able to generate seven-figure revenue due to our customer-centric services powered by passion and skillset. We are always looking forward to work on great ideas. If you are looking for [an application development company](https://wwww.ravsam.in/services/), you are most welcome to get in touch with our team.