Use github webhooks to automatically update hexo post
I used dropbox to sync my blog posts so that I can write my blog anywhere, instead of logging on to my vps. But dropbox’s instant sync makes it hard to control post event. Github is a great version control tool, if server side can automatically pull changed posts when pushing events happened on other device, then the github push event is kind of like posting your blogs to remote server. This should be a better way to using hexo. Luckily github’s webhooks service makes this possible to implement.
On the vps server side, first thing is init your post directory as a Git repository.
Second write a PHP to excute github pull, this php file is called by Github webhooks service.
This php program is very simple, but it actually does the pull trick, and you can read the response from the github page to see if pull excuted properly. As pull excution is called by apache service, so we should change the folder owner to www-data and make php excutable.
chown -R www-data:www-data you_post_directory
Then push post folder to remote repository on you github. Also you should create an empty repository on your github page.
git remote add origin remote_repository_URL
Then on your hexo blog folder, you should let hexo to monitor your post changes by using hexo generate. Here I also use screen to let it run on the background.
screen -S hexo
Final thing to do on vps server side is to create an apahce vhost to let this php can be called from outside. Below is a simple example, you should configure you dns profider to let this ServerName accessible.
Then you can test it on your local machine. Just clone the repository to your local machine, make some changes or add a post, then push it to remote, you should see that your site is being updated simutaneusly.
git clone remote_repository_url local_post_dir
Above methods can only work on public repository, but it’s obviously not a good idea making your blog posts a public repository that everyone can mess with. But when you change to private repository, the pull action cannot be performed due to lack of password, we need to add ssh keys to github settings.
First we should generate ssh keys, since this php programm is called by apache, so we should generate ssh keys for www-data user.
sudo mkdir /var/www/.ssh
sudo chown -R apache:apache /var/www/.ssh/
sudo -Hu apache ssh-keygen -t rsa # choose "no passphrase"
sudo cat /var/www/.ssh/id_rsa.pub
Then add above SSH keys to your github account. Then we should change remote repository on the server to SSH style.
git remote set-url origin email@example.com:USERNAME/REPOSITORY.git
After this, we should perform git pull once manually, because there is a promote message to ask if you accept the keys when first time use. If we don’t perform git pull once manually, the auto-pull script will never perform successfully.
This php program is too simple, should add some functions to let it only respond to github webhook post, for example we can parse the json posted by github to verify this post request is from github.