Boost Your GitHub Actions Workflow with Discord Webhook Notifications

Learn how to send Discord notifications using webhooks in GitHub Actions. This tutorial covers setting up a GitHub Actions workflow and sending a message to a Discord channel when a build fails, without mentioning roles.

· 3 min read
Github & Discord logos
Boost Your GitHub Actions Workflow with Discord 

In this tutorial, we will demonstrate how to integrate GitHub Actions build to send a notification to a Discord bot and post an alert when the build fails. We will be using the Discord Message Notify action to send notifications to Discord.


Prerequisites

  • A GitHub repository with a GitHub Actions workflow
  • A Discord server with a channel for notifications

Step 1: Create a Discord Webhook

  1. Go to the Discord channel where you want to receive notifications.
  2. Click on the settings gear icon next to the channel name.
  3. In the settings, find the Webhooks option and create a new webhook.
Discord channel Webhook Page

Step 2 (Optional): Create a Simple Python Project

  1. Clone the repository to your local machine.
  2. Create a new Python file called main.py with the following content:
def add_numbers(a, b):
    return a + b

def test_add_numbers():
    assert add_numbers(2, 3) == 5
    assert add_numbers(1, -1) == 0
    # This assertion will purposely fail
    assert add_numbers(10, 20) == 50

This script contains a simple function add_numbers that adds two numbers and a test_add_numbers function with assertions to test the add_numbers function. One of the assertions is incorrect and will cause the test to fail.

Step 3: Add Secrets to GitHub Repository

  1. Go to your GitHub repository and click on the "Settings" tab.
  2. In the left sidebar, click on "Secrets".
  3. Click on "New repository secret" and add two secrets:
  • WEBHOOK_ID: The webhook ID from the Discord webhook URL (the numbers before the first slash)
  • WEBHOOK_TOKEN: The webhook token from the Discord webhook URL (the alphanumeric string after the first slash)
Github's Settings page
Webhook link example


Step 4: Modify GitHub Actions Workflow

  1. In your repository, create a new directory called .github and a subdirectory called workflows.
  2. Create a new YAML file called main.yml inside the workflows directory with the following content:
name: Python Tests

on: [push, pull_request]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout code
      uses: actions/checkout@v2

    - name: Set up Python
      uses: actions/setup-python@v2
      with:
        python-version: 3.8

    - name: Install dependencies
      run: pip install pytest

    - name: Run tests
      run: python -m pytest main.py

    - name: Send Discord notification on failure
      if: failure()
      uses: appleboy/discord-action@master
      with:
        webhook_id: ${{ secrets.WEBHOOK_ID }}
        webhook_token: ${{ secrets.WEBHOOK_TOKEN }}
        color: "#eb4034"
        username: "GitHub Build Bot"
        message: "Build failed for ${{ github.repository }} on commit ${{ github.sha }}. Check the GitHub Actions log for more details."

This workflow will run on every push and pull request, set up Python 3.8, install the pytest package, and run the tests in the main.py file. If the tests fail, it will send a Discord notification as explained in the previous tutorial.

Step 4: Push Changes to GitHub

Commit the changes to your local repository and push them to GitHub.

Now, when you push the changes to GitHub, the GitHub Actions workflow will run and detect the failing test. This will trigger the Discord notification, and you should receive an alert in the specified Discord channel.

Failed action message

Conclusion

You have now successfully integrated GitHub Actions build with Discord notifications. This tutorial showed how to create a Discord webhook, add secrets to your GitHub repository, modify your GitHub Actions workflow to send a notification on build failure and test the integration.