hexo博客之旅 [2]: 利用webhooks自动部署hexo博客

以前觉得hook是个深奥的技术,的确,到现在我还没写过一个hook,网上有很多文章讲hook都是用的win32 API,不知何时我竟然看不下去这种代码了,不知是喜是忧。

Github给出的解释搬运一段如下:

Webhooks allow you to build or set up GitHub Apps which subscribe to certain events on GitHub.com. When one of those events is triggered, we’ll send a HTTP POST payload to the webhook’s configured URL. Webhooks can be used to update an external issue tracker, trigger CI builds, update a backup mirror, or even deploy to your production server. You’re only limited by your imagination.

webhooks就是这样一种机制,当你的repo有某个特定的事件发生时,比如push/PR/issue/fork等,github在后台会向你填写的URL发送一个POST请求,里面包含了很多信息,比如issue会有sender和相应的repo,还有issue的地址等。

另外这段话里面也说明了update a backup mirror这个用法,vps上面的镜像博客给百度索引,github.io给谷歌索引。

根据webhooks的用法,我们在github pages的repo里面订阅对push事件的钩子,vps后台开启一个简单的web server监听此请求,然后使用git pull更新github pages里面的静态博客文件到本地。

设置Webhooks

之前参考的一篇博客已经记不起来了,只记得是用node写的,我用python重写了一下,见github