Giter Club home page Giter Club logo

github-webhook-server's Introduction

GitHub Webhook Server

Overview

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.

Prerequisites

  • Python 3.8 or higher
  • FastAPI
  • Uvicorn: an ASGI server
  • A GitHub repository with webhook access

Installation

Clone this repository:

git clone https://github.com/leossok/github-webhook-server.git

Install the required packages:

pip install -r requirements.txt

Set up the environment variable for the webhook secret:

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)

Usage

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.

Configuration

The server can be configured using command-line options:

  • --host : The host address for the server. Defaults to 0.0.0.0.
  • --port : The port on which the server listens. Defaults to 5900.
  • --name : The name of the server, used in logging. Defaults to github_webhook_server.

Webhook Setup on GitHub (docs)

  1. Go to your repository on GitHub.
  2. Click on Settings > Webhooks > Add webhook.
  3. Set the Payload URL to your server's address (e.g., http://yourserver:port/webhook).
  4. Choose application/json for the Content type.
  5. Enter your WEBHOOK_SECRET.
  6. Select the events for which you want to receive webhook notifications.
  7. Click Add webhook `.

Security

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.

Logs

The application logs information about incoming requests and command execution results. Logs are stored in a file named [server-name].log.

Contributing

Contributions to this project are welcome. Please follow standard GitHub contribution guidelines.

License

MIT License - feel free to use and modify as needed.

github-webhook-server's People

Contributors

sirawats avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.