Gabriel Caiana

Integration between GitHub and Discord for Pull Request Notifications


Table of contents
  1. Prerequisites
  2. Step 1: Create a Webhook on Discord
  3. Step 2: Add the Webhook as a Secret on GitHub
  4. Step 3: Create the GitHub Action
  5. How does it work?
  6. Step 4: Test the Integration
  7. Conclusion

Integrating GitHub and Discord can be an effective way to keep your team informed about important events, such as opening or closing Pull Requests (PRs). In this article, you’ll learn how to set up an integration that sends notifications to a specific Discord channel whenever a PR is opened or merged into the master branch.

Prerequisites

Before you start, you will need:

  1. Access to a repository on GitHub.
  2. A Discord server with permissions to create webhooks.
  3. Access to GitHub Actions in the repository.

Step 1: Create a Webhook on Discord

First, we need to create a webhook in Discord, which will be used to send automatic messages to a specific channel.

  1. In Discord, go to the server where you want notifications to be sent.
  2. Right-click on the desired channel and select Channel Settings.
  3. In the side menu, select Integrations and then click Webhooks.
  4. Click New Webhook, give the webhook a name (for example, “GitHub Bot”), choose the channel, and click Copy Webhook URL. This URL will be used later.

Step 2: Add the Webhook as a Secret on GitHub

To keep your credentials secure, we will store the webhook ID and Token as secrets on GitHub.

  1. In the GitHub repository, go to Settings.
  2. In the side menu, click on Secrets and variables > Actions.
  3. Click New Secret and add two secrets:
    • Name: DISCORD_WEBHOOK_ID
  • Value: Extract the numeric part of the webhook URL, for example, 123456789.
    • Name: DISCORD_WEBHOOK_TOKEN
  • Value: Extract the alphanumeric part after the ID in the URL, for example, P85V9JQxwH1qpoxxDj4G8lvG3OI34p0SIiCnwKXTsGj.

Step 3: Create the GitHub Action

Now that the webhook is configured, we can create a GitHub Action that will send notifications to Discord when certain events occur. The following is an example YAML file for the action:

name: notify

on:
  pull_request:
    types: [opened, closed]
    branches:
      - master
  push:
    branches:
      - master

jobs:
  notify:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2

      - name: Send message for pull request opened
        if: github.event_name == 'pull_request' && github.event.action == 'opened'
        uses: appleboy/discord-action@master
        with:
          webhook_id: ${{ secrets.DISCORD_WEBHOOK_ID }}
          webhook_token: ${{ secrets.DISCORD_WEBHOOK_TOKEN }}
          color: "#48f442"
          username: "The Bot"
          message: |
            🎉 New pull request opened!

            **${{ github.event.pull_request.title }}**

            ${{ github.event.pull_request.body }}

            [Repository](${{
              github.event.repository.html_url
            }})
              
            [Pull Request](${{
              github.event.pull_request.html_url
            }})

      - name: Send message for merge to master
        if: github.event_name == 'pull_request' && github.event.action == 'closed' && github.event.pull_request.merged == true
        uses: appleboy/discord-action@master
        with:
          webhook_id: ${{ secrets.DISCORD_WEBHOOK_ID }}
          webhook_token: ${{ secrets.DISCORD_WEBHOOK_TOKEN }}
          color: "#48f442"
          username: "The Bot"
          message: |
            🚀 Pull request merged into master!

            **${{ github.event.pull_request.title }}**

            Author: ${{ github.event.pull_request.user.login }}

            [Repository](${{
              github.event.repository.html_url
            }})

            [Pull Request](${{
              github.event.pull_request.html_url
            }})

How does it work?

  1. Event Definition (on): The action is triggered when a Pull Request is opened or closed, and also when there is a push to the master branch.

  2. Notification Job:

  • If a PR is opened, the action sends a message to Discord using discord-action with details about the PR.
  • If a PR is closed and merged, another message is sent indicating that the PR has been merged into the master branch.
  1. Message Parameters:
  • webhook_id and webhook_token use the secrets you configured on GitHub. username defines the name of the bot that will appear on Discord.
  • The message contains the notification content, formatted with PR details.

Step 4: Test the Integration

After configuring the action and pushing the repository, you will see notifications in the specified Discord channel. Test by opening a PR and merging it to verify that messages are being sent correctly.

Conclusion

This integration between GitHub and Discord allows your team to receive real-time updates directly in Discord, keeping everyone informed about the progress of Pull Requests. This workflow can be adjusted as needed, enabling notifications for other GitHub events or even sending custom messages based on data in the repository.


If you have questions or want to exchange development ideas, feel free to contact me on LinkedIn or GitHub. It will be a pleasure to continue the conversation!