This Python application serves as a webhook server for GitHub. It listens for incoming webhook requests from GitHub and, upon a trigger, executes a predefined shell command. This is particularly useful for automating tasks such as pulling the latest code to your deployment server whenever new changes are pushed to a repository.
- Python 3.8 or higher
- FastAPI
- Uvicorn: an ASGI server
- A GitHub repository with webhook access
git clone https://github.com/leossok/github-webhook-server.git
pip install -r requirements.txt
export WEBHOOK_SECRET='your_github_webhook_secret'
Use for verification of the webhook request. This should be set to the same value as the secret you set in your GitHub repository's webhook settings. (docs)
To start the server, run the script with the desired command to execute upon receiving a webhook:
python3 /app/webhook_server.py --host 0.0.0.0 --port 6900 'cd /home/ubuntu/cra-2 && git pull'
Replace cd /home/ubuntu/cra-2 && git pull
with the command you want to execute when the webhook is triggered.
For me, I use this command to pull the latest code from my GitHub repository to my deployment server.
--host
: The host address for the server. Defaults to0.0.0.0
.--port
: The port on which the server listens. Defaults to5900
.--name
: The name of the server, used in logging. Defaults togithub_webhook_server
.
Webhook Setup on GitHub (docs)
- Go to your repository on GitHub.
- Click on Settings > Webhooks > Add webhook.
- Set the Payload URL to your server's address (e.g.,
http://yourserver:port/webhook
). - Choose
application/json
for theContent type
. - Enter your
WEBHOOK_SECRET
. - Select the events for which you want to receive webhook notifications.
- Click
Add webhook
`.
The server verifies the GitHub signature in each request to ensure it's genuinely from GitHub. Ensure your WEBHOOK_SECRET is secure and not publicly exposed.
The application logs information about incoming requests and command execution results. Logs are stored in a file named [server-name].log.
Contributions to this project are welcome. Please follow standard GitHub contribution guidelines.
MIT License - feel free to use and modify as needed.