实现步骤:
-
宝塔安装
宝塔WebHook 2.5
插件。
-
github
上配置网页仓库(或可在服务器的网页根目录clone
)。 -
配置
宝塔WebHook 2.5
- 添加
hook
脚本;
- 编辑添加
syncJC
脚本;
- 添加
bash
#!/bin/bash
# 定义网站根目录
WEBROOT="/www/wwwroot/jessi53"
# 定义日志文件路径
LOGFILE="/www/server/panel/plugin/webhook/log/webhook.log"
# 设置 HOME 环境变量
export HOME=/root
# 将调试信息写入日志文件
echo "[$(date)] Webhook triggered. Starting deployment process..." >> $LOGFILE
# 切换到网站根目录
echo "[$(date)] Switching to directory: $WEBROOT" >> $LOGFILE
cd $WEBROOT
# 设置 Git 安全目录
echo "[$(date)] Setting Git safe directory: $WEBROOT" >> $LOGFILE
git config --global --add safe.directory $WEBROOT >> $LOGFILE 2>&1
git config --system --add safe.directory $WEBROOT >> $LOGFILE 2>&1
# 检查是否是 Git 仓库
if [ -d "$WEBROOT/.git" ]; then
echo "[$(date)] Fetching latest changes from GitHub..." >> $LOGFILE
git fetch origin >> $LOGFILE 2>&1
echo "[$(date)] Resetting local branch to match remote branch..." >> $LOGFILE
git reset --hard origin/main >> $LOGFILE 2>&1
else
echo "[$(date)] Initializing Git repository and pulling latest changes..." >> $LOGFILE
git init >> $LOGFILE 2>&1
git remote add origin https://github.com/Jsvi53/jessi53.git >> $LOGFILE 2>&1
git fetch origin >> $LOGFILE 2>&1
git reset --hard origin/main >> $LOGFILE 2>&1
fi
# 设置目录权限
echo "[$(date)] Setting permissions for: $WEBROOT" >> $LOGFILE
chown -R www:www $WEBROOT >> $LOGFILE 2>&1
# 将请求体写入日志文件
echo "[$(date)] Webhook triggered. Pulled latest changes from GitHub." >> $LOGFILE
注意,网站的根目录为jessi53。
以上,宝塔上
webhook
配置完毕,可点击测试
进行测试,然后查看log
。
- 配置
github
上的WebHooks
:
开启
ssl
,开启导致配置不成功,不知道为什么。
以上配置完成,即可。