0%

使用 Webhook 完成 Git仓库 自动化部署

  • 系统:阿里云 Ubuntu20.04.1 LTS

写在前面

将服务器的公钥放在 Github/Gitee 上,实现免密拉取。然后在服务器上配置 Webhook,当仓库有更新时,Webhook 就会自动拉取代码并部署。

安装并配置 Webhook

1
apt install webhook

定义一个名为 blog 的 hook,它将在当前目录中执行一个名为 blog-d.sh 的脚本。

1
vim blog-d.json
1
2
3
4
5
6
7
[
{
"id": "blog",
"execute-command": "./blog-d.sh",
"response-message": "blog deploy success!"
}
]

编写部署脚本

1
vim blog-d.sh
1
2
3
4
5
6
7
8
9
#!/bin/bash

cd /www/blog
git pull

hexo clean
hexo g

exit 0
1
chmod 777 blog-d.sh

启动 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