107 lines
3.3 KiB
Markdown
107 lines
3.3 KiB
Markdown
# 生产部署(GitHub Actions,非 Docker)
|
||
|
||
本文档用于 `wx_service` 在宝塔服务器上的自动化发布:
|
||
- 触发:`main` 分支 push 或手动触发
|
||
- 流程:GitHub Actions SSH 触发服务器发布脚本 -> 服务器 git pull + go build -> 重启并健康检查
|
||
- 特点:不依赖 Docker
|
||
|
||
## 1. 服务器约定
|
||
|
||
- 代码目录:`/www/wwwroot/wx_service`
|
||
- 运行目录:`/www/wwwroot/wx_service/dist`
|
||
- 二进制:`/www/wwwroot/wx_service/dist/wx_service`
|
||
- 进程端口:`8080`
|
||
- 反向代理:宝塔 Nginx -> `127.0.0.1:8080`
|
||
|
||
> 远程脚本:`scripts/ops/deploy_from_source.sh`(内部调用 `scripts/ops/deploy_binary.sh`)
|
||
|
||
## 2. GitHub Secrets
|
||
|
||
在仓库 `Settings -> Secrets and variables -> Actions` 新增:
|
||
|
||
- `PROD_HOST`:生产机 IP 或域名
|
||
- `PROD_PORT`:SSH 端口(默认 `22`)
|
||
- `PROD_USER`:SSH 用户(建议 `root` 或具备发布权限的用户)
|
||
- `PROD_SSH_KEY`:私钥内容(建议单独部署密钥)
|
||
|
||
## 3. 工作流文件
|
||
|
||
- `/.github/workflows/deploy-prod.yml`
|
||
|
||
已默认发布到:`/www/wwwroot/wx_service/dist/wx_service`。
|
||
|
||
## 4. 首次上线注意事项
|
||
|
||
1. 服务器必须已存在:`/www/wwwroot/wx_service/.git`(可正常 `git fetch`)
|
||
2. 服务器上配置好生产 `.env`(建议放两份):
|
||
- `/www/wwwroot/wx_service/.env`
|
||
- `/www/wwwroot/wx_service/dist/.env`
|
||
3. 开放 `8080` 本地监听,并由 Nginx 反代
|
||
4. 服务器无需 Docker;第一次执行会自动安装 Go 1.23.6(若缺失)并尝试创建 `systemd` 服务 `wx_service`
|
||
|
||
## 5. 发布行为
|
||
|
||
每次发布会执行:
|
||
|
||
1. GitHub Actions 通过 SSH 调用服务器发布脚本
|
||
2. 服务器执行:
|
||
- `git fetch` + `git reset --hard <commit_sha>`(同步代码)
|
||
- 安装/校验 Go 1.23.6
|
||
- `go mod download` + `go build`
|
||
3. 远程发布脚本继续执行:
|
||
- 备份旧二进制到 `/www/wwwroot/wx_service/backups/`
|
||
- 原子替换 `dist/wx_service`
|
||
- 重启服务
|
||
- 健康检查 `http://127.0.0.1:8080/healthz`
|
||
4. 若健康检查失败,自动回滚旧二进制并重启
|
||
|
||
## 6. 手动发布(应急)
|
||
|
||
在服务器执行(仅二进制手动发布场景):
|
||
|
||
```bash
|
||
cd /www/wwwroot/wx_service
|
||
APP_DIR=/www/wwwroot/wx_service \
|
||
DIST_DIR=/www/wwwroot/wx_service/dist \
|
||
SOURCE_BIN=/tmp/wx_service-manual \
|
||
RELEASE_ID=manual-$(date +%Y%m%d%H%M%S) \
|
||
SERVICE_NAME=wx_service \
|
||
RUN_USER=www RUN_GROUP=www \
|
||
PORT=8080 \
|
||
SYNC_CODE=true DEPLOY_REF=main \
|
||
INSTALL_SERVICE=true \
|
||
bash scripts/ops/deploy_binary.sh
|
||
```
|
||
|
||
## 7. 回滚
|
||
|
||
```bash
|
||
ls -lt /www/wwwroot/wx_service/backups/
|
||
cp -f /www/wwwroot/wx_service/backups/wx_service.<backup_id>.bak /www/wwwroot/wx_service/dist/wx_service
|
||
chown www:www /www/wwwroot/wx_service/dist/wx_service
|
||
systemctl restart wx_service
|
||
```
|
||
|
||
若机器未使用 systemd,可改为:
|
||
|
||
```bash
|
||
pkill -f /www/wwwroot/wx_service/dist/wx_service
|
||
su -s /bin/bash - www -c "cd /www/wwwroot/wx_service/dist && nohup ./wx_service >> /www/wwwlogs/wx_service.stdout.log 2>&1 &"
|
||
```
|
||
|
||
|
||
## 8. 手动发布(从源码构建)
|
||
|
||
```bash
|
||
cd /www/wwwroot/wx_service
|
||
APP_DIR=/www/wwwroot/wx_service \
|
||
DIST_DIR=/www/wwwroot/wx_service/dist \
|
||
DEPLOY_REF=main \
|
||
RELEASE_ID=manual-$(date +%Y%m%d%H%M%S) \
|
||
GO_VERSION=1.23.6 \
|
||
SERVICE_NAME=wx_service \
|
||
RUN_USER=www RUN_GROUP=www \
|
||
PORT=8080 \
|
||
bash scripts/ops/deploy_from_source.sh
|
||
```
|