Loading...

Loading...

This page has no en version yet, showing default language content

webhook是一种可以让人远程调用的RPC指令集,可以实现更新github仓库后自动更新部署你的Hexo博客,当然几乎所有的你能想到的东西都能做,本文将详细讲解webhook自动化部署静态博客方法

adnanh/webhook

上面这个仓库支持在你的VPS 快速建立一个webhook 接口。然后利用 github webhook 通知你的 VPS 来拉取最新的代码部署

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

webhook流程

安装 webhook

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

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

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

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

法二:使用 github 下载

webhook 的 release 链接,找到对应的选项 webhook-linux-amd64.tar.gz 右键复制链接 ,当前最新版本链接为

# 下载对应的软件
$ wget https://github.com/adnanh/webhook/releases/download/2.8.1/webhook-linux-amd64.tar.gz
# 解压进入
$ tar -zxf tar -zxf webhook-linux-amd64.tar.gz && cd webhook-linux-amd64/
$ ./webhook

法三: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

我们的 hooks.json 文件现在看起来是这样的:

[
  {
    "id": "redeploy-webhook",
    "execute-command": "/var/scripts/redeploy.sh",
    "command-working-directory": "/var/webhook"
  }
]

注意:如果您更喜欢 YAML 格式,等价的 hooks.yaml 文件如下:

- id: redeploy-webhook
  execute-command: "/var/scripts/redeploy.sh"
  command-working-directory: "/var/webhook"

您现在可以使用以下命令运行 webhook:

$ /path/to/webhook -hooks hooks.json -verbose

它将在默认端口 9000 上启动,并为您提供一个 HTTP 终端:

http://yourserver:9000/hooks/redeploy-webhook

更多配置参数

webhook/docs/Webhook-Parameters.md at master

Usage of webhook:
  -cert string
        path to the HTTPS certificate pem file (default "cert.pem")
  -cipher-suites string
        comma-separated list of supported TLS cipher suites
  -debug
        show debug output
  -header value
        response header to return, specified in format name=value, use multiple times to set multiple headers
  -hooks value
        path to the json file containing defined hooks the webhook should serve, use multiple times to load from different files
  -hotreload
        watch hooks file for changes and reload them automatically
  -http-methods string
        globally restrict allowed HTTP methods; separate methods with comma
  -ip string
        ip the webhook should serve hooks on (default "0.0.0.0")
  -key string
        path to the HTTPS certificate private key pem file (default "key.pem")
  -list-cipher-suites
        list available TLS cipher suites
  -logfile string
        send log output to a file; implicitly enables verbose logging
  -nopanic
        do not panic if hooks cannot be loaded when webhook is not running in verbose mode
  -pidfile string
        create PID file at the given path
  -port int
        port the webhook should serve hooks on (default 9000)
  -secure
        use HTTPS instead of HTTP
  -setgid int
        set group ID after opening listening port; must be used with setuid
  -setuid int
        set user ID after opening listening port; must be used with setgid
  -template
        parse hooks file as a Go template
  -tls-min-version string
        minimum TLS version (1.0, 1.1, 1.2, 1.3) (default "1.2")
  -urlprefix string
        url prefix to use for served hooks (protocol://yourserver:port/PREFIX/:hook-id) (default "hooks")
  -verbose
        show verbose output
  -version
        display webhook version and quit
  -x-request-id
        use X-Request-Id header, if present, as request ID
  -x-request-id-limit int
        truncate X-Request-Id header to limit; default no limit

实战

创建各种配置文件

# pwd
/www/docker/webhook

# tree .
.
├── bin
│   └── webhook
├── xxx
└── webhook-linux-amd64.tar.gz

2 directories, 2 files

利用 uuidgen 生成随机 ID

# uuidgen
ecbfc10e-6b2d-4d32-98da-bdd2c3c29fc7

hooks.yaml

- id: redeploy-hblog-xxxxx-7e86-4f24-8667-23xxxxxxx1
  execute-command: "/www/docker/webhook/github-webhooks/depHblog.sh"
  command-working-directory: "/www/wwwroot/blog.17lai.site"

depHblog.sh

#!/bin/sh

# 创建临时目录
temp_dir='/www/docker/webhook/tmp/hexoblog'
dst_dir='/www/wwwroot/blog.17lai.site'
repo_url='https://github.com/appotry/hexo.git'
data_dir=''

# 在这里进行你的操作,例如复制文件、下载内容等


# 检查目标目录是否存在 .git 目录
if [ -d "$temp_dir/.git" ]; then
    # 如果存在 .git 目录,则执行 git pull 命令进行更新
    echo "Repository already cloned. Performing git pull..."
    cd "$temp_dir"
    git pull origin main  # 如果使用了默认的主分支名字为 main
    git reset --hard origin/main
else
    # 如果不存在 .git 目录,则执行 git clone 命令进行克隆
    rm $temp_dir -rf
    echo "Cloning repository into $temp_dir..."
    git clone --progress -v --depth 1 "$repo_url" "$temp_dir"
fi


echo "rsync 目录同步"

# -a:表示以归档模式同步,保留文件的所有属性,包括权限、时间戳等。
# -v:表示详细模式,显示同步过程中的详细信息。
# --delete:表示删除目标目录中源目录没有的文件。
rsync --delete -r --exclude='.git' --link-dest="$dst_dir" ${temp_dir}/${data_dir} ${dst_dir}

echo "设置文件权限"
chown www:www ${dst_dir}/${data_dir}  -R

vim depHblog.sh
chmod +x depHblog.sh
curl -X POST https://xxxx.17lai.site/hooks/redeploy-hblog-xxxx8fa-7e86-4f24-8667-23axxxx
# 运行测试
su root

/www/docker/webhook/bin/webhook -hooks /www/docker/webhook/github-webhooks/hooks.json -verbose -port <通信端口>

/www/docker/webhook/bin/webhook -hooks /www/docker/webhook/github-webhooks/hooks.yaml -verbose -port 18xxx

后台运行

nohup webhook -hooks /usr/local/bin/github-webhooks/hooks.json -verbose -port <通信端口> &

反向代理

可以直接端口运行,当然上SSL 安全性更高!

这里使用 反向代理 复用443

宝塔面板 go 项目

go project webhook

调用测试

curl -X POST https://xxx.17lai.site/hooks/redeploy-hblog-xxxa-7e86-4f24-8667-2xxxx3xx

关联github仓库

上面命令测试成功之后就可以正式和 github 仓库关联起来了

github webhooks

后话

webhook 实际应用非常强大,只要你有想象力,几乎无所不能!

本文也在 hexo博客增强部署篇 一文中有提到。hexo 博客 系列文章越写越长,有兴趣可以再去看看,更新了很多东西了

参考&致谢

系列教程

全部文章RSS订阅

Hexo系列

HexoRSS分类订阅

[十万字图文教程]基于Hexo的matery主题搭建博客并深度优化完全一站式教程

  • markdown 各种其它语法插件,latex公式支持,mermaid图表,plant uml图表,URL卡片,bilibili卡片,github卡片,豆瓣卡片,插入音乐和视频,插入脑图,插入PDF,嵌入iframe

笔记系列

Note分类RSS订阅

Gitbook使用系列

Gitbook分类RSS订阅

Gitlab 使用系列

Gitlab RSS 分类订阅


webhook部署Hexo静态博客指南
Posted on
April 24, 2024
reprint policy. If reproduced, please indicate source
Support
Comments
Data Loading...
 Previous

Read More

如何防止密码泄露,保护密码安全?
如何防止密码泄露,保护密码安全? 如何防止密码泄露,保护密码安全?
关于如何安全使用密码,一文搞定!在当今数字化时代,密码安全变得尤为重要。随着我们在网上创建的账户越来越多,记住所有复杂的密码变得几乎不可能。这时候,密码管理工具就显得尤为重要了。本文将介绍几款流行的密码管理工具,并着重介绍bitwarden
2024-05-31
Next 

Read More

写了些浏览器增强脚本
写了些浏览器增强脚本 写了些浏览器增强脚本
平时创造写代码的时候,写了一些辅助工具,可以极大的提高工作效率!浏览器配合 tampermonkey 使用,真神器也!现在有繁简转换外挂,CSDN zhihu创作助手!
2024-04-19