Files
WeReadBoard/docs/deploy-master.md

90 lines
4.3 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# master Webhook 自动部署
部署链路是:GitHub `master` push → GitHub Webhook → 服务器签名校验 → 服务器本地执行 `deploy.sh`。GitHub Actions 文件 `.github/workflows/deploy.yml` 现在只负责验证,不再通过 SSH 登录服务器。
## GitHub Webhook 配置
进入仓库 `Settings -> Webhooks -> Add webhook`,配置 Payload URL `https://laohang.space/github/webhook`、Content type `application/json`、Secret(与服务器 `GITHUB_WEBHOOK_SECRET` 完全一致),事件选择 `Just the push event` 并勾选 `Active`。只允许 `KingHWJ/WeReadBoard` 的 `master` push 触发部署。
Webhook 服务配置文件 `/etc/weread-webhook.env`:
```ini
GITHUB_WEBHOOK_SECRET=服务器生成的随机密钥
GITHUB_REPOSITORY=KingHWJ/WeReadBoard
DEPLOY_PROJECT_DIR=/opt/weread-board/WeReadBoard
DEPLOY_SCRIPT=/opt/weread-board/WeReadBoard/deploy.sh
WEBHOOK_HOST=127.0.0.1
WEBHOOK_PORT=9001
```
复制 `deploy/weread-webhook.service` 到 `/etc/systemd/system/`,并执行:
```bash
python3 -c 'import secrets; print(secrets.token_urlsafe(48))'
sudo chmod 600 /etc/weread-webhook.env
sudo systemctl daemon-reload
sudo systemctl enable --now weread-webhook.service
sudo journalctl -u weread-webhook.service -f
```
## 服务器要求
服务器部署目录需要已经是本仓库的 Git checkout,并且能访问 `origin/master`。运行服务的环境变量仍按现有应用配置,例如 `WEREAD_API_KEY`、`WEREAD_DB_HOST`、`WEREAD_DB_PORT`、`WEREAD_DB_USER`、`WEREAD_DB_PASSWORD` 和 `WEREAD_DB_NAME`。
如果这些变量放在项目目录的 `.env` 文件中,部署脚本会先加载 `.env`,再执行数据库迁移。Webhook 运行用户需要能执行 `systemctl restart <服务名>`;非 root 用户需要配置免密 `sudo systemctl restart/status <服务名>`。
`deploy.sh` 会执行:
```bash
git fetch origin
git checkout master
git pull --ff-only origin master
python3 -m venv .venv # 仅 .venv 不存在时执行
.venv/bin/python -m pip install -r requirements.txt
# 读取 reading_api.MYSQL_CONFIG 后按顺序执行 migrations/*.sql
sudo systemctl restart "$SERVICE_NAME"
```
其中迁移实际通过 `reading_api.MYSQL_CONFIG` 读取数据库配置并执行 SQL 文件;SQL 使用 `CREATE TABLE IF NOT EXISTS`,可重复执行。
脚本默认部署 `master`,显式传入其他 `DEPLOY_BRANCH` 时会在拉取代码、安装依赖和重启服务之前退出。
## systemd 示例
```ini
[Unit]
Description=WeReadBoard
After=network.target
[Service]
WorkingDirectory=/opt/weread-board/WeReadBoard
Environment=WEREAD_API_KEY=wrk-xxxxxxxx
Environment=WEREAD_DB_HOST=127.0.0.1
Environment=WEREAD_DB_PORT=3306
Environment=WEREAD_DB_USER=weread
Environment=WEREAD_DB_PASSWORD=change-me
Environment=WEREAD_DB_NAME=weread
Environment=PORT=8770
Environment=WEREAD_SESSION_SECRET=<服务器生成的随机密钥>
Environment=WEREAD_AUTH_SETUP_TOKEN=<仅首次初始化使用的随机口令>
Environment=WEREAD_SESSION_COOKIE_SECURE=1
ExecStart=/opt/weread-board/WeReadBoard/.venv/bin/python app.py
Restart=always
RestartSec=3
[Install]
WantedBy=multi-user.target
```
如果服务名或部署目录不同,在仓库 Variables 中调整 `SERVICE_NAME` 和 `DEPLOY_PATH`。
登录认证还需配置 `WEREAD_SESSION_SECRET` 和首次使用的 `WEREAD_AUTH_SETUP_TOKEN`。在服务器分别运行两次 `python3 -c 'import secrets; print(secrets.token_urlsafe(48))'` 生成不同的值。迁移自动创建 `weread_auth` 表;首次访问 `/login` 时输入初始化口令、管理员账号和至少 12 位密码。初始化成功后,从 systemd 配置中删除 `WEREAD_AUTH_SETUP_TOKEN` 并重启服务。数据库中的密码只保存哈希。
`WEREAD_SESSION_COOKIE_SECURE=1` 要求浏览器通过 HTTPS 访问。仅本机 HTTP 验收可临时设为 `0`,不要在公网 HTTP 下传输登录密码。
## Nginx HTTPS 转发
将 `deploy/nginx-github-webhook.conf` 的两个 `location` 放入 `laohang.space` 的 HTTPS server 块,然后执行 `sudo nginx -t && sudo systemctl reload nginx`。Webhook 服务只监听 `127.0.0.1:9001`,不要对公网开放 9001 端口。健康检查地址为 `https://laohang.space/github/webhook-health`。
`.github/workflows/deploy.yml` 只执行 Python、前端和部署脚本检查。Webhook 模式不再需要 `SERVER_HOST`、`SERVER_PORT`、`SERVER_USER`、`SERVER_SSH_KEY` 这些 Actions Secrets。