准备
- 需要两个 github 仓库:
- 一个用于发布页面: shenbo.github.io
- 一个用于放源码: hexo-source
- 已配置好 hexo 的 VPS 服务器
1. 创建 hexo-source 仓库
- 在hexo的source目录下( ~/hexo/source)运行:
cd hexo/source
git init
git add .
git commit -m "first commit"
git remote add origin https://github.com/shenbo/hexo-source.git
git push -u origin master
2. 搭建Webhook 服务
2.1 编写 hexo 部署命令
新建文件 hexo-d.sh ,内容如下:
cd ~/hexo/source
git pull origin master
cd ~/hexo
hexo clean
hexo generate
hexo deploy
2.2 方案一:用 Python 接收 webhook
- 新建文件 webhook.py ,内容如下:
from wsgiref.simple_server import make_server
import os
def application(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/html')])
print('blog.sh....')
os.system('/bin/bash hexo-d.sh &')
return [b'Hello, webhook!']
httpd = make_server('', 9000, application)
print('Serving HTTP on port 9000...')
# start HTTP server:
httpd.serve_forever()
- 运行webhook.py服务
nohup python3 webhook.py &
2.3 方案二:直接安装 webhook 应用
- 安装 webhook 应用
sudo apt-get install webhook
- 新建配置文件 hexo-hooks.json,内容为:
[
{
"id": "blog",
"execute-command": "hexo-d.sh",
"response-message": "job done!"
}
]
- 启动 webhook 服务,默认端口为9000
nohup webhook -hooks hexo-hook.json -verbose &
- PS: 查看端口占用情况
sudo netstat -peanut
3. 配置 github 仓库的 webhook
进入 github.com -> repository -> hexo-source -> settings -> webhook
填写 webhook配置信息.
Payload URL : http://xxx.xxx.xxx.xxx:9000/- Payload URL : http://xxx.xxx.xxx.xxx:9000/hooks/blog
- Content type : aplacation/json
--- ok!
4. 测试
在笔记本上 git clone
hexo-source 仓库,然后 push 一下。
网友评论