# GitHub 自动部署配置说明

## ✅ 已完成的配置

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

### 2. 部署脚本
- **位置**: `/root/file_tag_manager/auto-deploy.sh`
- **功能**: git pull → docker compose build → docker compose up -d

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

### 4. Webhook Secret
```
6b1279b99efd2e661f2099cae2afdf70aa9ff7bd
```

---

## 🔧 GitHub Webhook 配置步骤

### 步骤 1: 进入仓库设置

访问你的仓库：https://github.com/liupeizhi/file_tag_manager

点击 **Settings** → **Webhooks** → **Add webhook**

### 步骤 2: 填写 Webhook 配置

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

### 步骤 3: 添加 Webhook

点击 **Add webhook** 保存配置

### 步骤 4: 测试 Webhook

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

---

## 📊 查看部署日志

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

### 查看部署脚本日志
```bash
tail -50 /var/log/file-tag-webhook.log
```

### 查看服务状态
```bash
systemctl status file-tag-webhook
docker compose ps
```

### 查看 Docker 构建日志
```bash
docker compose logs -f
```

---

## 🎯 工作流程

```
1. 本地修改代码
   ↓
2. git push 到 GitHub main 分支
   ↓
3. GitHub 发送 Webhook 到服务器
   ↓
4. 服务器接收并验证 Webhook
   ↓
5. 执行 auto-deploy.sh
   ↓
6. git pull 最新代码
   ↓
7. docker compose down 停止服务
   ↓
8. docker compose build 重新构建
   ↓
9. docker compose up -d 启动服务
   ↓
10. ✅ 部署完成！
```

---

## 📝 部署流程说明

### auto-deploy.sh 执行内容：

```bash
📥 拉取最新代码...
🛑 停止服务...
🔨 重新构建镜像...
🚀 启动服务...
🧹 清理旧镜像...
✅ 检查服务状态...
```

### 部署时间

根据项目大小，通常需要 2-5 分钟：
- Git pull: ~10 秒
- Docker build: ~1-3 分钟
- Docker up: ~30 秒

---

## 🔒 安全建议

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

为 Webhook 端点配置 HTTPS：

```bash
# 申请证书
/usr/sbin/certbot certonly --standalone -d webhook.tag.liupeizhi.top
```

然后更新 Nginx 配置使用 HTTPS。

### 2. 限制 IP 访问

在 Nginx 配置中添加 GitHub IP 白名单：

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

---

## 🐛 故障排查

### Webhook 不触发

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

### 部署失败

1. 查看部署日志：`tail -50 /var/log/file-tag-webhook.log`
2. 检查 git 权限：`cd /root/file_tag_manager && git status`
3. 检查 Docker：`docker compose logs`
4. 手动测试部署：`bash /root/file_tag_manager/auto-deploy.sh`

### 服务无法访问

1. 检查 Docker 容器：`docker compose ps`
2. 检查 Nginx 服务：`systemctl status nginx`
3. 查看 Nginx 日志：`tail -50 /var/log/nginx/error.log`
4. 检查端口占用：`netstat -tlnp | grep :80`

---

## 📱 通知配置（可选）

### 飞书通知

修改 `auto-deploy.sh` 添加飞书通知：

```bash
# 在部署完成后添加
curl -X POST -H "Content-Type: application/json" "https://open.feishu.cn/open-apis/bot/v2/hook/YOUR_WEBHOOK" \
  -d "{
    \"msg_type\": \"text\",
    \"content\": {
      \"text\": \"🎉 File Tag Manager 自动部署成功！\\n时间：$(date '+%Y-%m-%d %H:%M:%S')\\n提交：$(git rev-parse --short HEAD)\"
    }
  }"
```

---

## 🔧 常用命令

```bash
# 手动触发部署
bash /root/file_tag_manager/auto-deploy.sh

# 重启 Webhook 服务
systemctl restart file-tag-webhook

# 停止 Webhook 服务
systemctl stop file-tag-webhook

# 查看 Webhook 日志
journalctl -u file-tag-webhook -f

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

# 查看 Docker 服务状态
docker compose ps

# 查看 Docker 日志
docker compose logs -f

# 重启服务
docker compose restart
```

---

## 📋 服务信息

| 服务 | 端口 | 状态 |
|------|------|------|
| Webhook | 9001 | ✅ 运行中 |
| Nginx Webhook | 80 | ✅ 运行中 |
| Backend | 8080 | Docker 管理 |
| Frontend | 3000 | Docker 管理 |
| MySQL | 3306 | Docker 管理 |

**访问地址**: https://tag.liupeizhi.top

---

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