1-
2- Webhook receiver for automations
3- ================================
1+ Webhook server for triggering automations
2+ =========================================
43
54This project started as a dirty & quick hack to perform some deployment actions.
65Initially, the focus was on Git webhooks, but after a while this project was
@@ -10,57 +9,94 @@ I looked a little bit at other projects and didn't found any one that suited my
109needs, so I started this project... and then it grew a little bit and become
1110something more versatile than the quick hack originally intended.
1211
13- This project is aimed at DevOps or sysadmin that have git repositories, typically
14- with a VIP-branch, and automatic deployment. You may want to have different branches
15- for stage and production or set up push permissions differently to the different
16- branches. This utility, when a webhook is received, will update the local Git repository
17- and perform the commands in the settings.
12+ This project is aimed at DevOps or sysadmin that have git repositories in GitHub, Gitlab,
13+ or any other kind of webhook provider (git or otherwise).
1814
19- Quickstart
20- ----------
15+ This utility starts a server that listens to webhooks and when a webhook is received,
16+ it will perform the actions in the settings.
2117
22- - Create and activate a Virtual Environment:
18+ Quickstart demo with GitHub and ngrok
19+ -------------------------------------
2320
24- ``` bash
25- $ virtualenv --python=/usr/bin/python3 /path/to/venv
26- $ source /path/to/venv/bin/activate
27- ```
21+ Using [ ngrok] ( https://ngrok.com/ ) is a quick way to obtain a publicly reachable HTTPS endpoint.
22+ We will be using that for this demo.
2823
29- - Install the package: ` pip install webhooks_automata `
30- - Create a ` settings.yaml `
31- - Set up a service (e.g. a systemd _ service_ file) that does something along:
24+ - Create and activate a Virtual Environment in a Python 3.11 environment.
25+ - Install the package: ` pip install webhooks-automata `
26+ - If you want to use ` main ` branch, do
27+ ` pip install git+https://github.com/alexbarcelo/webhooks-automata `
28+ instead.
29+ - Create a ` settings.yaml ` , like the one shown in [ examples] ( examples/github_sample.yaml ) .
30+ - Start the server:
3231
3332``` bash
34- $ /path/to/venv/bin/wh-automatad /path/to/settings.yaml
33+ $ AUTOMATA_SETTINGS=examples/github_sample.yaml uvicorn webhooks_automata.app:app
34+ INFO: Started server process [57971]
35+ INFO: Waiting for application startup.
36+ Current endpoints active:
37+ /my_webhook
38+ INFO: Application startup complete.
39+ INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
3540```
3641
42+ - Run the ngrok ingress to this service with ` ngrok http 8000 `
43+ - The webhook endpoint will be something like ` https://c94e-2600-70ff-f2f3-0-13ef-f451-8b4f-cc76.ngrok-free.app/my_webhook `
44+ (the host on this URL is given by ngrok and the path is set in the settings YAML file).
45+ - Go to your GitHub repository > Settings > Webhooks > Add webhook.
46+ - Fill the form:
47+ - ** Payload URL** : The webhook endpoint.
48+ - ** Content type** : Does not matter, but use JSON for future proofing it.
49+ - ** Secret** : Put the shared secret in the settings YAML file, in this demo it is ` secret_token `
50+ - _ Add webhook_
51+ - When a webhook is created, GitHub makes a "ping delivery". You can check in the Recent Deliveries
52+ tab if there was a problem with the webhook endpoint.
53+
54+ Deploying the webhook server
55+ ----------------------------
56+
57+ Instead of using ngrok, you may want to deploy this application into a
58+ domain/server under your control. The steps are similar like on the previous
59+ demo, but you will need to include a reverse proxy (and you probably want
60+ to include SSL termination on it).
61+
62+ Take a look into [ nginx] ( https://nginx.org/ ) , [ Traefik] ( https://traefik.io/ )
63+ or [ HAProxy] ( https://www.haproxy.org/ ) .
64+
65+ In addition to that, you will want to automatically start the Uvicorn server,
66+ which may done with either systemd (or whatever is used by your OS) or
67+ [ supervisord] ( http://supervisord.org/ ) .
68+
3769Settings
3870--------
3971
4072Extra CLI features
4173------------------
4274
43- To force a manul trigger, you can use
44- the built-in ` wh-automata-trigger ` command. Example usage:
75+ The ` wh-automatactl ` is a CLI that can be used to aid and complement the webhook server.
4576
46- ``` bash
47- $ wh-git-trigger /path/to/setings.yaml entryname
48- ```
49-
50- This will react just as if a trigger had been received. Future versions of this tool will
51- include more fine-grain control --e.g. dry-run, display status information...
77+ You can check its documentation by calling ` wh-automatactl --help ` . Keep in mind that this
78+ CLI will be accessible from within the virtual environment; that typically means that the
79+ script can be found in /pat/to/venv/bin/wh-automatactl.
5280
5381Implementation details
5482----------------------
5583
56- This project contains a minimal Flask server that answers the POST webhooks. A typical source
57- of those webhooks call are Git servers such as GitLab, GitHub or Gogs. The server is started
58- through the Flask's ` app.run ` method.
59-
60- Not a lot of traffic is expected, but you may want to set up a reverse proxy in front
61- of the Flask server, or add some fancier method like a WSGI or uWSGI or similar layer.
62-
63- Typical webhooks expect the a quick reply (in general, HTTP connections are intended to be short
64- lived) so there is a worker/tasks approach. There is a very simple implementation base on
65- ` Threading ` and a shared ` Queue ` . More complex implementations may be added in the future
66- (pull requests are welcome).
84+ This project contains a minimal [ Starlette] ( https://www.starlette.io/ ) ASGI
85+ application that answers the POST webhooks. My recommendation is to start the
86+ application through [ uvicorn] ( http://www.uvicorn.org/ ) .
87+
88+ The actions are run as [ Starlette Background Tasks] ( https://www.starlette.io/background/ )
89+ because generally providers (GitHub, Gitlab, etc.) expect a quick webhook and
90+ do not care on the return. A 200 success response is given for all properly
91+ authorized requests and actions are run after that; a 200 success response
92+ ** does not mean** that actions have run successfully (in fact, the actions start
93+ ** after** the response).
94+
95+ Most internal code assumes that things can be made asynchronously, so the
96+ webhook server should be a lightweight but capable process --the heavy-lifting
97+ will be performed by asynchronous actions. This is assured by using
98+ [ ` asyncio-subprocess ` ] ( https://docs.python.org/3/library/asyncio-subprocess.html )
99+ and other async-centric tools.
100+
101+ Feel free to use other ASGI servers, or embed the Starlette routes into a more
102+ complex application.
0 commit comments