deploy: 本地预构建发布(push-build),对齐 1Panel 与 backend_v2 目录

- 新增 Dockerfile.prebuilt / scripts/push-build.sh:本机构建、只上传产物,服务器零编译
- deploy.sh 增加 --prebuilt(自动 --no-pull + 产物校验),并保留 --no-cache
- compose 支持 BACKEND_DOCKERFILE / WEB_DOCKERFILE 切换;预构建时用 prebuilt 变体
- init-db.sh / scripts/backup-db.sh 改为 docker exec 1Panel 的 Postgres 容器
- WEB_DIR 默认改为 ../frontend_v2;nginx 切换文件改到 .deploy/ 避免弄脏工作区
- 新增 .gitattributes 固定脚本与配置为 LF
This commit is contained in:
toom1996
2026-10-21 00:02:18 +08:00
parent 822dd69911
commit 2e76507d37
11 changed files with 308 additions and 25 deletions

View File

@ -275,7 +275,43 @@ docker exec 1Panel-postgresql-4Ndp pg_dump -U user_JA83HF fashion \
---
## 10. 排障(真实案例)
## 10. 本地构建发布(前后端都不在服务器编译)
服务器资源紧张时用这条路径:**构建全在本机完成**,服务器只负责打镜像层 + 蓝绿切换。
```bash
# 本机(后端仓)执行,一条命令跑完
SERVER=user@server ./scripts/push-build.sh
```
脚本做五件事:
| 步 | 动作 | 说明 |
|---|---|---|
| 1 | 建 SSH 隧道 `本地18091 → 服务器 127.0.0.1:18091` | 前端构建期靠它读**线上** SSG 数据,公网零暴露 |
| 2 | 本机 `npm run build` | 静态页内容与线上一致,不吃服务器内存 |
| 3 | 本机 `CGO_ENABLED=0 GOOS=linux go build` | 产出 35MB 的 `server-linux-amd64` |
| 4 | `tar`/`scp` 上传 `dist/` 与二进制 | 只传产物,不传源码 |
| 5 | 远程 `./deploy.sh all --prebuilt` | 用 `Dockerfile.prebuilt` 打层,蓝绿切换 |
前置条件:
- 本机有 `node` / `npm` / `go` / `ssh` / `scp` / `tar`
- 本机后端仓放一份 `.env`(复制服务器那份,用于取 `SSG_TOKEN`)
- 服务器上 `proxy` 正在跑(18091 是它的端口)
与「服务器构建」的区别:
| | 服务器构建 | 本地构建(push-build) |
|---|---|---|
| 服务器 CPU/内存 | 前端 2~4 分钟 + 1GB 内存 | 只 `COPY` 产物,秒级 |
| 构建期数据 | 线上(天然一致) | 线上(走隧道,同样一致) |
| git 拉取 | 会拉两个仓库 | **跳过**(`--prebuilt` 自动 `--no-pull`) |
| 发布号 | 服务器仓库的 sha | 本机 sha,通过 `RELEASE` 传入 |
两种方式可以混用:后端改动小、想走服务器构建,就 `./deploy.sh backend`;想省资源就 `push-build.sh`。
## 11. 排障(真实案例)
```bash
cd /opt/fashion/backend_v2