ci: add non-docker production deployment workflow

This commit is contained in:
root
2026-03-10 00:12:27 +08:00
parent 386877da9a
commit 8b3ca2fd62
5 changed files with 323 additions and 0 deletions
+1
View File
@@ -10,6 +10,7 @@
- `docs/common/upload_qiniu.md`
- `docs/common/wechat_official.md`
- `docs/common/redis.md`
- `docs/common/deploy_ci.md`GitHub Actions 非 Docker 自动化发布)
## 去水印小程序
+4
View File
@@ -34,3 +34,7 @@
## Redis
- `docs/common/redis.md`
## 自动化部署(非 Docker
- `docs/common/deploy_ci.md`
+88
View File
@@ -0,0 +1,88 @@
# 生产部署(GitHub Actions,非 Docker
本文档用于 `wx_service` 在宝塔服务器上的自动化发布:
- 触发:`main` 分支 push 或手动触发
- 流程:GitHub Actions 构建二进制 -> SSH 上传到服务器 -> 远程发布脚本重启并健康检查
- 特点:不依赖 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_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. 第一次执行会自动尝试创建 `systemd` 服务 `wx_service`
## 5. 发布行为
每次发布会执行:
1. GitHub Actions 构建 Linux 二进制
2. 上传到服务器 `/tmp/wx_service-<commit_sha>`
3. 远程脚本执行:
- `git fetch` + `git reset --hard <commit_sha>`(同步代码)
- 备份旧二进制到 `/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 &"
```