Writing GitHub WebHooks with Bash
For the past year on the grounds that Microsoft has obtained GitHub, I’ve been web hosting my Git repositories on a private server. Although I relished the possibility and mission of setting all of it up, and the quit product works well for my wishes, doing this turned into no longer without its sacrifices. GitHub offers a clean interface for configuring many Git features that otherwise would require extra time and effort than absolutely clicking a button. One of the features made simpler to put in force via GitHub that I changed into most keen on becoming net hooks. An internet hook is executed while a selected occasion takes place inside the GitHub application. Upon execution, statistics are despatched via an HTTP POST to a unique URL.
This article walks thru a way to set up a custom internet hook, including configuring a web server, processing the POST records from GitHub and growing some primary webhooks the usage of Bash.
For the reason of this mission, permit’s use the Apache internet server to host the net hook scripts. The module that Apache uses to run server-aspect shell scripts is mod_cgi, that’s to be had on foremost Linux distributions.
Once the module is enabled, it is time to configure the listing permissions and digital host inside Apache. Use the /decide/hooks listing to host the webhooks, and deliver possession of this listing to the consumer that runs Apache. To decide the user going for walks an Apache example, run the subsequent command (furnished Apache is currently running):
These commands will return a -column output containing the call of the person going for walks Apache and the call of the Apache binary (typically both httpd or apache2). Grant listing permission with the following chown command (where USER is the call of the user proven in the preceding PlayStation command):
Within this listing, sub-directories could be created: HTML and CGI-bin. The HTML folder may be used as a webroot for the digital host, and CGI-bin will incorporate all shell scripts for the virtual host. Be aware that as new sub-directories and documents are created beneath /decide/hooks, you could need to rerun the above chown to verify right get right of entry to to documents and sub-directories.
Change the cost of the ServerName directive from SERVERNAME to the call of the host so one can be accessed thru the webhook. This configuration affords base functionality to host files and executes shell scripts. The DocumentRoot directive specifies the basis of the virtual host the usage of an absolute path at the local system. The ScriptAlias directive takes two arguments: an absolute course inside the virtual host and an absolute course at the nearby system. The route inside the virtual host is mapped to the neighborhood system direction. Mod_cgi handles all requests made to the course distinctive in the ScriptAlias directive. (Note: any additional configuration such as SSL or logging isn’t covered in this article.)
You’ll want simple know-how of the HTTP protocol and Bash scripting to recognize how CGI scripts work. When a request is made to an HTTP server, a response is generated and despatched again to the client. The HTTP request carries headers that teach the server a way to manage the request. Likewise, the HTTP response carries headers that educate the customer the way to deal with the response. Viewing and reading HTTP traffic can be very simple using the developer tools on any contemporary browser. Here’s a simple instance of an HTTP request and response: