gongxiangp2/scripts/README.md

149 lines
4.0 KiB
Markdown
Raw 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.

# 脚本说明
## ⚠️ 重要声明
**本项目强制使用 `deploy-remote-build.ps1` 脚本进行部署**,除非项目负责人明确要求使用其他方式。
**详细说明**:[部署规范](../docs/部署规范.md)
---
## 核心部署脚本
### `scripts/git-incremental-deploy.ps1` ⭐⭐⭐ **Git 增量部署**
**新推荐方向**:以 Git 仓库为唯一源码入口,构建机增量拉取源码并构建镜像,生产服务器只拉取镜像并替换容器。
特点:
- 不从本机上传源码压缩包
- 构建机执行 `git fetch/reset`
- Docker 构建继续复用缓存
- 镜像按提交打标签,便于回滚
- 生产机不构建源码,只拉镜像运行
- 默认失败时尝试回滚旧镜像
预检:
```powershell
.\scripts\git-incremental-deploy.ps1 -DryRun
```
正式部署由维护人员执行,不要求普通用户手动运行。
### `deploy-remote-build.ps1` ⭐⭐⭐ **唯一推荐**
**专用构建机部署脚本** - 保护本地 SSD,提高构建效率
**文件位置**:项目根目录(`deploy-remote-build.ps1`)
**架构流程**:
```
本地电脑 → SSH 上传代码(1-5MB) → 构建机(192.168.42.134) → 构建镜像 → 推送到Registry → 生产服务器拉取 → 启动容器
```
**核心优势**:
1. 保护本地 SSD(构建在远程)
2. 构建速度快(Linux 原生)
3. 可视化管理(Registry UI: http://192.168.42.134:5001)
4. 镜像版本管理
5. 内网高速传输(1Gbps)
**使用方式:**
```powershell
# 在项目根目录执行
# 部署到正式系统(端口 4320)
.\deploy-remote-build.ps1
# 部署到影子系统(端口 5320)
.\deploy-remote-build.ps1 -Target shadow
# 跳过代码同步
.\deploy-remote-build.ps1 -SkipSync
# 跳过镜像构建
.\deploy-remote-build.ps1 -SkipBuild
# 不使用构建缓存
.\deploy-remote-build.ps1 -NoCache
# 保留旧镜像
.\deploy-remote-build.ps1 -KeepOldImages
```
**参数说明:**
- `-Target`: 部署目标,`production`(正式系统,端口4320)或 `shadow`(影子系统,端口5320)
- `-BuildServerIP`: 构建机 IP(默认:192.168.42.134)
- `-ProdServerIP`: 生产服务器 IP(默认:192.168.58.20)
- `-SkipSync`: 跳过代码同步
- `-SkipBuild`: 跳过镜像构建
- `-NoCache`: 不使用构建缓存
- `-KeepOldImages`: 保留旧镜像
---
## 🚫 其他部署方式(不推荐)
以下脚本仅作为参考,**不推荐使用**,除非项目负责人明确要求:
### `deploy-local-build.ps1` ❌
**本地构建部署** - 会在本地产生大量 Docker 缓存,磨损 SSD
**问题**:
- 本地构建占用大量磁盘空间(~2GB)
- SSD 磨损严重(频繁读写)
- 构建速度慢(Windows Docker Desktop 性能差)
- 上传镜像慢(~180MB 通过网络)
### `deploy-git-build.ps1` ⚠️
**Git 工作流部署** - 需要配置 Git 仓库,复杂度高
**问题**:
- 需要配置 Git 仓库
- 流程复杂(本地提交 → 推送 → 服务器拉取 → 构建)
- 不适合快速迭代
### `deploy-public-build.ps1` ⚠️
**公网穿透部署** - 仅在特殊情况下使用(外网访问生产环境)
**用途**:从公网访问生产环境进行部署(非常规操作)
---
**⚠️ 再次提醒**:请使用 `deploy-remote-build.ps1` 进行部署,除非项目负责人明确要求其他方式。
---
## 基础镜像管理
### `save-base-images.ps1`
下载并保存基础镜像到本地缓存
```powershell
.\save-base-images.ps1
```
### `load-base-images.ps1`
从本地加载基础镜像(部署脚本会自动调用)
```powershell
.\load-base-images.ps1
```
### `save-npm-packages.ps1`(可选)
缓存 npm 包到本地(网络不稳定时使用)
```powershell
.\save-npm-packages.ps1
```
## 数据库迁移脚本
### `execute-migration.ps1`
在生产库上执行数据库迁移(Windows PowerShell)
```powershell
.\execute-migration.ps1
```
### `execute-migration.sh`
在生产库上执行数据库迁移(Linux/Mac)
```bash
chmod +x execute-migration.sh
./execute-migration.sh
```