Integration between GitHub and Discord for Pull Request Notifications
Table of contents
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:
- Access to a repository on GitHub.
- A Discord server with permissions to create webhooks.
- 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.
- In Discord, go to the server where you want notifications to be sent.
- Right-click on the desired channel and select Channel Settings.
- In the side menu, select Integrations and then click Webhooks.
- 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.
- In the GitHub repository, go to Settings.
- In the side menu, click on Secrets and variables > Actions.
- Click New Secret and add two secrets:
- Name:
DISCORD_WEBHOOK_ID
- Name:
- Value: Extract the numeric part of the webhook URL, for example,
123456789.- Name:
DISCORD_WEBHOOK_TOKEN
- Name:
- 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?
-
Event Definition (
on): The action is triggered when a Pull Request is opened or closed, and also when there is a push to themasterbranch. -
Notification Job:
- If a PR is opened, the action sends a message to Discord using
discord-actionwith details about the PR. - If a PR is closed and merged, another message is sent indicating that the PR has been merged into the
masterbranch.
- Message Parameters:
webhook_idandwebhook_tokenuse the secrets you configured on GitHub.usernamedefines the name of the bot that will appear on Discord.- The
messagecontains 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!