之前《通过github和caddy实现hugo的自动部署》,使用的是 caddy 的 http.git 插件。最近发现,那方法不管用了,不知道原因。
所以决定使用传统的 webhook 。原理是:在 vps 运行 webhook 监听程序,github 收到 push 事件后,
就通知该监听程序,由该监听程序执行相应的命令。现在记录一下。
安装 webhook
sudo apt install webhook
编写 webhook 的配置文件
我的itlaws.cn的markdown源码放在/var/www/itlaws.cn
mkdir /var/www/itlaws.cn/webhook
touch itlaws.cn-config.json
touch itlaws.cn-deploy.sh
chmod +x itlaws.cn-deploy.sh
itlaws.cn-config.json内容为:
[
  {
    "id": "itlaws.cn-deploy",
    "execute-command": "/var/www/itlaws.cn/webhook/itlaws.cn-deploy.sh",
    "command-working-directory": "/var/www/itlaws.cn"
  }
]
其中:
id是监听程序名称,自定义。execute-command是监听程序接收到 github 的通知后执行的命令,自定义。command-working-directory指定目录运行上述命令。
更多的参数见官方的Hook definition。
itlaws.cn-deploy.sh内容为:
#!/bin/bash
cd /var/www/itlaws.cn && git pull --recurse-submodules && rm -rf public && hugo
测试监听程序
webhook -hooks /var/www/itlaws.cn/webhook/itlaws.cn-config.json -verbose -port=9000
其中:
- 
-hooks后面要接配置文件; - 
-verbose表示输出日志; - 
-port表示监听端口,自定义。 
更多的内容见Webhook parameters。
上述命令将在9000端口运行webhook监听程序,即:
http://itlaws.cn:9000/hooks/itlaws.cn-deploy
在 github 端测试,正常。该监听程序目前是 http ,而不是 https 。
通过 supervisor 运行监听程序
supervisor是用于启动某种服务并监控该服务,在该服务异常退出时重启该服务。
sudo apt install supervisor
cd /etc/supervisor/conf.d/
touch webhook.conf
webhook.conf的内容为:
[program:webhook]
command=/var/www/itlaws.cn/webhook/webhook -hooks  /var/www/itlaws.cn/webhook/itlaws.cn-config.json -verbose -port=9000 -secure -cert ssl证书的路径 -key ssl密钥路径
user=www-data
directory=/var/www/itlaws.cn/webhook
autorestart=true
redirect_stderr=true
其中,-secure表示启用 https,-cert 和-key分别是 https 的证书和密钥的路径,从而把监听服务从 http 改为 https:
https://itlaws.cn:9000/hooks/itlaws.cn-deploy
好了,大功告成。