4

使用 webhook 自动更新博客

 3 years ago
source link: https://blog.cugxuan.cn/2019/03/23/Git/Use-Webhook-To-Update-Blog/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

webhook_header.png

因为之前实在受不了 Coding Page 的速度(现在的 Coding Page 服务已经变成新版,使用腾讯云提供的服务,应该还行,不过不太想折腾了),几年前腾讯云的 CDN 感觉也很不好用,所以将自己的博客挂在了自己的服务器上。但是每次在写完之后都需要到服务器上执行 git pull过于麻烦,所以使用了 GitHub 上面的开源项目 webhook当然 webhook 的作用不仅仅是自动更新部署博客啊,啥都能干 O__O …

Webhook

对于 webhook 来说,可以自己写一个脚本来接受信息,思路很简单,运行一个 HTTP Server,监听服务器的某个端口,如果有消息传递过来,那么就运行事先写好的脚本,来完成 webhook 的功能即可。
webhook.png

但是为了方便使用和维护,还是选择了开源的项目

什么是 webhook?

webhook 的目标只是做他应该做的事

  1. 接受 request
  2. 解析 HTTP 首部,负载内容和请求变量
  3. 检测是否满足钩子的特定条件
  4. 通过命令行参数或环境变量将指定的参数传递给指定的命令

其他所有的事情都是 命令作者 的责任

我的博客是 hexo 构建,构建出来的结果都是静态文件,使用 Nginx 挂在服务器上的,以下的内容都需要在服务器上执行

安装 webhook

这里推荐三种安装方法,使用其中一种方法即可,github 下载速度可能很慢,推荐使用系统源安装的方式

法一:使用系统源的安装方式

如果是 Ubuntu 系统,可以直接使用下面的命令进行安装:

$ sudo apt-get update
$ sudo apt-get install webhook

法二:使用 github 下载

webhook 的 release 链接,找到对应的选项 webhook-linux-amd64.tar.gz 右键复制链接 ,当前写博客时的最新版本链接为 https://github.com/adnanh/webhook/releases/download/2.6.11/webhook-linux-amd64.tar.gz

具体操作如下:

// 下载对应的软件
$ 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

// 安装对应的工具
$ 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

下一步是定义您希望 webhook 提供的一些钩子,新建一个 hook.json 文件
该文件将包含 webhook 将提供的一系列钩子。检查 Hook 定义页面 以查看钩子可以包含的属性的详细描述以及如何使用它们。

定义和部署

下面的内容在 ~/notes-hooks 文件夹下执行
让我们定义一个名为 redeploy-webhook 的简单钩子,新建一个 redeploy.sh 文件,确保你的 bash 脚本在顶部有 #!/bin/sh

#!/bin/sh
git pull

写完之后注意需要给文件增加执行权限
$ chmod a+x redeploy.sh
然后新建一个 hooks.json 文件,看代码就可以知道意思了

[
{
"id": "redeploy-blog",
"execute-command": "/home/ubuntu/notes-hooks/redeploy.sh",
"command-working-directory": "/home/ubuntu/blog.cugxuan.cn"
}
]

这个 hooks.json 文件中各项的作用一看便知,注意 id 和监听的 URL 对应,webhook 默认监听的端口是 9000,根据上面写的可知监听的 URL 为 http://yoursite.com:9000/hooks/redeploy-blog,接下来执行命令部署即可

# 前台运行的方法,可以方便测试
$ ./webhook -hooks hooks.json -verbose
# 守护进程简单运行,这样就可以部署了
$ nohup ./webhook -hooks hooks.json -verbose &

然后将 http://yoursite.com:9000/hooks/redeploy-blog 填到你的 GitHub 网站对应项目页面 Settings → Webhooks 中即可

写好博客然后 push,GitHub 收到你的更新之后就会触发 webhook 发送到服务器的监听位置,然后服务器完成更新部署
同时你可以在 GitHub 的 webhook 页面中看到发送的情况,还可以点击 Redeliver 重新测试
webhook-redeliver


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK