Coding Pagegit pull
WebhookHTTP Server
但是为了方便使用和维护,还是选择了开源的项目
什么是 webhook? 如何使用我的博客是 hexo 构建,构建出来的结果都是静态文件,使用 Nginx 挂在服务器上的,以下的内容都需要在服务器上执行
安装 webhook
这里推荐三种安装方法,使用其中一种方法即可,github 下载速度可能很慢,推荐使用系统源安装的方式
法一:使用系统源的安装方式
如果是 Ubuntu 系统,可以直接使用下面的命令进行安装:
1
2
$ sudo apt-get update
$ sudo apt-get install webhook
法二:使用 github 下载
webhook-linux-amd64.tar.gz
具体操作如下:
1
2
3
4
5
6
7
8
// 下载对应的软件
$ wget https://github.com/adnanh/webhook/releases/download/2.6.11/webhook-linux-amd64.tar.gz
// 解压进入
$ tar -zxf tar -zxf webhook-linux-amd64.tar.gz && cd webhook-linux-amd64/
$ ./webhook
[webhook] 2020/02/23 22:44:43 couldn't load any hooks from file!
aborting webhook execution since the -verbose flag is set to false.
If, for some reason, you want webhook to start without the hooks, either use -verbose flag, or -nopanic
法三:Golang 环境下安装
首先安装 Golang 环境(这里略过),然后安装 webhook,这里注意需要已经设置了 GOPATH
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// 安装对应的工具
$ go get github.com/adnanh/webhook
// 安装完成之后可以在 $GOPATH/bin 下找到对应的执行文件
$ echo $GOPATH
/root/go
$ ls /root/go/bin
webhook
// 将路径写入到 shell 可以直接执行
$ vi ~/.bash_profile
// 在文件最后一行加入对应内容
export PATH="/root/go/bin:$PATH"
// 更新文件使其生效
$ source ~/.bash_profile
// 测试命令是否已经成功安装,得到输出说明安装完成
$ webhook
[webhook] 2020/02/23 22:44:43 couldn't load any hooks from file!
aborting webhook execution since the -verbose flag is set to false.
If, for some reason, you want webhook to start without the hooks, either use -verbose flag, or -nopanic
配置 webhook
hook.json
定义和部署
~/notes-hooksredeploy-webhookredeploy.sh#!/bin/sh
1
2
#!/bin/sh
git pull
$ chmod a+x redeploy.shhooks.json
1
2
3
4
5
6
7
[
{
"id": "redeploy-blog",
"execute-command": "/home/ubuntu/notes-hooks/redeploy.sh",
"command-working-directory": "/home/ubuntu/blog.cugxuan.cn"
}
]
hooks.jsonhttp://yoursite.com:9000/hooks/redeploy-blog
1
2
3
4
5
# 前台运行的方法,可以方便测试
$ ./webhook -hooks hooks.json -verbose
# 守护进程简单运行,这样就可以部署了
$ nohup ./webhook -hooks hooks.json -verbose &
http://yoursite.com:9000/hooks/redeploy-blogSettings → Webhooks
测试
写好博客然后 push,GitHub 收到你的更新之后就会触发 webhook 发送到服务器的监听位置,然后服务器完成更新部署
同时你可以在 GitHub 的 webhook 页面中看到发送的情况,还可以点击 Redeliver 重新测试