This project is a Webhook Listener built with Node.js (Express) that listens to GitHub webhooks and executes specific scripts when a push is made to the master branch. It supports multiple repositories (API and Frontend) and automatically updates the code on the server.
- ✅ Secure webhook verification using HMAC signatures.
- ✅ Supports multiple repositories (API and Frontend).
- ✅ Executes custom scripts based on the repository.
- ✅ Filters events to trigger actions only on the
masterbranch. - ✅ Handles
pingandpushevents. - ✅ Uses PM2 to keep the service running.
- ✅ Clear and detailed logging.
/github-webhooks
│
├── server.js # Main Express server
├── ecosystem.config.js # PM2 configuration
├── .env # Environment variables
├── package.json # Dependencies
├── logs/ # Output and error logs
│ ├── error.log
│ └── output.log
└── scripts/ # Custom scripts for repos
├── script-api.sh
└── script-front.sh
git clone https://github.com/axbegue/github-webhooks.git
cd github-webhooksnpm installCreate a .env file in the project root:
PORT=3000
GITHUB_SECRET=your_github_secret
API_REPO_NAME=api-repo-name
API_SCRIPT_PATH=/path/to/your/script-api.sh
FRONT_REPO_NAME=front-repo-name
FRONT_SCRIPT_PATH=/path/to/your/script-front.shGITHUB_SECRET: The secret set in your GitHub webhook.API_REPO_NAMEandFRONT_REPO_NAME: Exact names of the repositories.API_SCRIPT_PATHandFRONT_SCRIPT_PATH: Absolute paths to your deployment scripts.
Example for script-api.sh:
#!/bin/bash
cd /path/to/your/api-project || exit 1
git reset --hard
output=$(git pull 2>&1)
if [[ "$output" == *"Already up to date."* ]]; then
echo "✅ Repository is already up to date."
exit 0
fi
echo "$output"
npm install
echo "🚧 Building the application..."
nest build
echo "🚀 Restarting the server..."
pm2 restart proyect-name
echo "✅ Update completed successfully."Make the scripts executable:
chmod +x /path/to/your/script-api.sh
chmod +x /path/to/your/script-front.shnode server.jsAccess at http://localhost:3000/webhook.
npm install pm2 -gpm2 start ecosystem.config.js- Go to your repository on GitHub.
- Settings > Webhooks > Add webhook.
- Payload URL:
http://your-server/webhook. - Content type:
application/json. - Secret: Use the same value as
GITHUB_SECRETin.env. - Events: Select Just the push event.
- Save the webhook and push code to test it.
- HMAC signature verification to ensure webhooks come from GitHub.
- Branch filtering: only triggers scripts on
master. - Error and execution logs for debugging.
Contributions are welcome! Feel free to open an issue or submit a pull request to improve the project.
This project is licensed under the MIT License.
Developed by Antonio Begue (https://github.com/axbegue) 🚀