# GitHub 自动部署配置指南

## ✅ 已完成的配置

### 1. Webhook 接收服务
- **服务名**: hexo-webhook
- **监听端口**: 9000
- **访问地址**: http://deploy.blog.liupeizhi.top
- **状态**: 已启动并开机自启

### 2. 部署脚本
- **位置**: `/root/hexo-blog/deploy.sh`
- **功能**: git pull → hexo generate → systemctl restart hexo-blog

### 3. Nginx 反向代理
- **配置**: `/etc/nginx/conf.d/hexo-webhook.conf`
- **域名**: deploy.blog.liupeizhi.top

### 4. Webhook Secret
```
169ad0cfe52cb6009564d6756938063bc2776a08
```

---

## 🔧 GitHub 配置步骤

### 步骤 1: 创建 GitHub 仓库

1. 访问 https://github.com/new
2. 创建新仓库，例如：`your-username/hexo-blog`
3. **不要**勾选 "Add a README file"
4. 点击 "Create repository"

### 步骤 2: 关联远程仓库

```bash
cd /root/hexo-blog
git remote add origin https://github.com/your-username/hexo-blog.git
git push -u origin main
```

⚠️ **注意**: 将 `your-username` 替换为你的 GitHub 用户名

### 步骤 3: 配置 GitHub Webhook

1. 进入你的 GitHub 仓库
2. 点击 **Settings** → **Webhooks** → **Add webhook**
3. 填写以下信息：

| 字段 | 值 |
|------|-----|
| **Payload URL** | `http://deploy.blog.liupeizhi.top/` |
| **Content type** | `application/json` |
| **Secret** | `169ad0cfe52cb6009564d6756938063bc2776a08` |
| **SSL verification** | ❌ 取消勾选（因为是 HTTP） |
| **Events** | ✅ Just the push event |

4. 点击 **Add webhook**

### 步骤 4: 测试 Webhook

1. 在 GitHub 仓库中修改任意文件（如 README.md）
2. 提交更改到 main 分支
3. 查看 Webhook 交付日志：Settings → Webhooks → 点击刚创建的 webhook → Recent Deliveries
4. 应该看到 ✅ 200 OK 响应

---

## 📊 查看部署日志

### 实时查看 Webhook 日志
```bash
journalctl -u hexo-webhook -f
```

### 查看部署脚本日志
```bash
tail -50 /var/log/hexo-deploy.log
```

### 查看服务状态
```bash
systemctl status hexo-webhook
systemctl status hexo-blog
```

---

## 🔒 安全建议

### 1. 配置 HTTPS（推荐）

为 Webhook 端点配置 HTTPS，防止 Secret 泄露：

```bash
# 申请证书（如果还没有）
/usr/sbin/certbot certonly --standalone -d deploy.blog.liupeizhi.top

# 更新 Nginx 配置使用 HTTPS
```

### 2. 限制 IP 访问

在 `/etc/nginx/conf.d/hexo-webhook.conf` 中添加：

```nginx
# 仅允许 GitHub 的 IP 范围
allow 140.82.112.0/20;
allow 140.82.113.0/24;
allow 140.82.114.0/24;
allow 140.82.121.0/24;
allow 143.55.64.0/20;
allow 185.199.108.0/22;
allow 185.199.109.0/24;
allow 185.199.110.0/24;
allow 185.199.111.0/24;
allow 192.30.252.0/22;
deny all;
```

### 3. 使用 Token 访问（可选）

修改 deploy.sh 使用 GitHub Token 拉取私有仓库：

```bash
git pull https://YOUR_TOKEN@github.com/your-username/hexo-blog.git main
```

---

## 🎯 工作流程

```
1. 本地修改代码
   ↓
2. git push 到 GitHub
   ↓
3. GitHub 发送 Webhook 到服务器
   ↓
4. 服务器接收 Webhook
   ↓
5. 执行 deploy.sh
   ↓
6. git pull 最新代码
   ↓
7. hexo generate 生成静态文件
   ↓
8. systemctl restart hexo-blog 重启服务
   ↓
9. ✅ 网站更新完成
```

---

## 📱 通知配置（可选）

### 飞书通知

在 `deploy.sh` 中已预留飞书 Webhook 通知，设置环境变量：

```bash
export FEISHU_WEBHOOK="https://open.feishu.cn/open-apis/bot/v2/hook/xxx"
```

### 邮件通知

修改 `deploy.sh` 添加邮件发送：

```bash
echo "部署成功" | mail -s "博客部署通知" your-email@example.com
```

---

## 🐛 故障排查

### Webhook 不触发

1. 检查 GitHub Webhook 配置是否正确
2. 检查 Nginx 配置：`/usr/sbin/nginx -t`
3. 检查服务状态：`systemctl status hexo-webhook`
4. 查看日志：`journalctl -u hexo-webhook`

### 部署失败

1. 查看部署日志：`tail -50 /var/log/hexo-deploy.log`
2. 检查 git 权限：`cd /root/hexo-blog && git status`
3. 检查 hexo 命令：`which hexo`

### 网站无法访问

1. 检查 Hexo 服务：`systemctl status hexo-blog`
2. 检查 Nginx 服务：`systemctl status nginx`
3. 查看 Nginx 日志：`tail -50 /var/log/nginx/error.log`

---

## 📝 常用命令

```bash
# 手动触发部署
bash /root/hexo-blog/deploy.sh

# 重启 Webhook 服务
systemctl restart hexo-webhook

# 停止 Webhook 服务
systemctl stop hexo-webhook

# 查看 Webhook 日志
journalctl -u hexo-webhook -f

# 测试 Webhook 端点
curl -X POST http://deploy.blog.liupeizhi.top/ \
  -H "Content-Type: application/json" \
  -H "X-GitHub-Event: ping" \
  -d '{"zen":"test"}'
```

---

配置完成后，每次 `git push` 到 main 分支都会自动部署！🎉
