DEV Community

loading...
Cover image for Send an Email notification when Github Actions fails
RavSam Web Solutions

Send an Email notification when Github Actions fails

ravgeetdhillon profile image Ravgeet Dhillon Originally published at ravsam.in on ・3 min read

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
    1. 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:

name: Build

on:
  push:
    branches: main

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Hello World
        run: echo Hello, world!
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

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, you are most welcome to get in touch with our team.

Discussion (0)

pic
Editor guide