Problem: I found myself in a situation where I wanted to deploy my landing pages after pushing changes to the main GitHub branch without connecting to the production server and without losing the possibility to review changes online (GitHub). I hardly found any ready-to-solution so I prepared one.
What Does It Do?
This is a simple install script that is:
installing curl, git, and go
downloading source code of https://github.com/adnanh/webhook and is building it
creating webhooks directory in /var/www
moving compiled webhook binary into var/www/webhooks
creating hooks.json file and moving it into /var/www/webhooks – a configuration of webhook module
creating simple-pull.sh – a bash script that executes git pull after changes are pushed to the main branch
creating webhook.service and running it – this just basically launch a webhook server (more info in docs from the link posted above).
How Does It Work?
Webhook service is starting a web server at port 9000 that is listening for the requests.
The request is fired from GitHub using webhook after pushing anything to the repository. The payload looks like this: https://developer.github.com/webhook…payloads/#push
Each request is then processed based on hooks.json conditions and if all conditions are matched, the selected bash script is executed with possible arguments from the request.
This bash script can be anything, from simple echo’s to sophisticated building pipelines, in this script’s case it is just a simple git pull that fetches recent changes on the production server (solving “Problem” stated above)
Example Webhook URL looks like this: http://yourdomain.com:9000/hooks/deploy – deploy here is an “id” from the hooks.json, this file can be updated accordingly to the docs to execute more complicated builds if you ever wished so.
- Github account.
- Production server with proper A record set to yourdomain.com
- Github repository deployed to the production server
Webhook set up in the repository settings:
Webhook URL is: http://yourdomain.com:9000/hooks/deploy
Where yourdomain.com must be replaced with the production domain that you are using to host your code.
Secret: this is the hash that you use in both install script and GitHub webhook. Both values must be THE SAME.
This is a security measure, requests without proper secrets are rejected and deployment is NOT processed.
After each push to the GitHub repository, a webhook that you created is executed, posting a request to the http://yourdomain.com:9000/hooks/deploy that deploys your code automatically (see “How Does It Work?“)
The main scenario in the current version is the following:
- You have your own server
- You added a DNS record that point to that server
- You created a repository with your codebase
- You pulled that repository on your server once
- You set up web server to serve files from that repository when accessing your domain
Now the script kicks in.
It set up a webhook module that will automatically pull any further changes from the repository when they occur.
One important note is that the GitHub repository name must match the document root directory name of your website.
The way it works right now is that it requires a repository that is already deployed once and set up in the web server.
As always – hope it helps.