写在前面
将服务器的公钥放在 Github/Gitee 上,实现免密拉取。然后在服务器上配置 Webhook,当仓库有更新时,Webhook 就会自动拉取代码并部署。
安装并配置 Webhook
定义一个名为 blog 的 hook,它将在当前目录中执行一个名为 blog-d.sh 的脚本。
1 2 3 4 5 6 7
| [ { "id": "blog", "execute-command": "./blog-d.sh", "response-message": "blog deploy success!" } ]
|
编写部署脚本
1 2 3 4 5 6 7 8 9
| #!/bin/bash
cd /www/blog git pull
hexo clean hexo g
exit 0
|
启动 Webhook 在后台运行
1
| nohup webhook -hooks blog-d.json -verbose &
|
配置 Github/Gitee Webhook
在 Github/Gitee 仓库的 Settings -> Webhooks 中添加 Webhook
- Payload URL:http://服务器ip:9000/hooks/hook的id
- Content type:application/json