no message

This commit is contained in:
hajimi
2026-06-11 12:15:29 +08:00
parent 10ebe39c30
commit 96efa1d905
5859 changed files with 815501 additions and 5 deletions
+117
View File
@@ -1,3 +1,120 @@
## Sport Era 服务器部署脚本
部署脚本位置:
- Windows 原生:`scripts/deploy-server.ps1`
- Bash 兼容版:`scripts/deploy-server.sh`
脚本通过 SSH 别名 `sbnews` 连接服务器,打包本地 `server` 代码并在服务器目录解压安装。Windows 下优先使用 `deploy-server.ps1`,不依赖 WSL。
### PowerShell 一次执行命令
在 Windows PowerShell 中执行:
```powershell
cd D:\www\sport-era
powershell -ExecutionPolicy Bypass -File .\scripts\deploy-server.ps1 -Sudo -RemoteDir /www/wwwroot/test-server.sbnews.net
```
如果 `sbnews` 配置在 `C:\Users\Administrator\.ssh\config` 中,默认会使用 Windows 的 `ssh.exe` 读取该配置。
### 全量发布
不传 commit id 即为全量发布:
```powershell
cd D:\www\sport-era
powershell -ExecutionPolicy Bypass -File .\scripts\deploy-server.ps1 -Sudo -RemoteDir /www/wwwroot/test-server.sbnews.net
```
全量模式默认只打包以下目录:
```text
server/app
server/config
server/extend
server/public/dongqiudi-crawler
server/route
```
默认会排除运行态或较大目录,例如 `.env``vendor``runtime``public/uploads``public/admin``public/mobile`、爬虫 `data/logs/venv`。如确实需要包含,可追加参数:
```bash
--include-env
--include-vendor
--include-runtime
--include-uploads
--include-public-builds
--include-crawler-data
```
### 增量发布
传入两个 commit id,脚本会对比差异,只打包 `--to` 版本中的变更文件,并在服务器删除两个 commit 之间被删除的文件:
```powershell
cd D:\www\sport-era
powershell -ExecutionPolicy Bypass -File .\scripts\deploy-server.ps1 -Sudo -From <old_commit> -To <new_commit> -RemoteDir /www/wwwroot/test-server.sbnews.net
```
示例:
```powershell
cd D:\www\sport-era
powershell -ExecutionPolicy Bypass -File .\scripts\deploy-server.ps1 -Sudo -From abc1234 -To def5678 -RemoteDir /www/wwwroot/test-server.sbnews.net
```
### 自动增量发布
如果只是发布最近一次提交的后端改动,可以使用 `--auto-incremental`,脚本会自动对比 `HEAD~1``HEAD`
```powershell
cd D:\www\sport-era
powershell -ExecutionPolicy Bypass -File .\scripts\deploy-server.ps1 -Sudo -AutoIncremental -RemoteDir /www/wwwroot/test-server.sbnews.net
```
先演练不上传:
```powershell
cd D:\www\sport-era
powershell -ExecutionPolicy Bypass -File .\scripts\deploy-server.ps1 -AutoIncremental -RemoteDir /www/wwwroot/test-server.sbnews.net -DryRun
```
注意:自动增量只包含最近两个提交之间已经提交到 Git 的差异,不会包含工作区未提交文件。如有多次提交需要一起发布,请继续使用 `--from <old_commit> --to <new_commit>`
如果当前根目录 Git 只把 `server` 记录为 gitlink/submodule,且本机没有 `server/.git` 子仓库元数据,脚本无法拿到 `server` 内部的文件级差异;此时会自动降级为打包当前工作区默认的 5 个 server 目录,保证可以继续发布。
### 只演练不上传
先确认打包范围、包大小和增量删除清单:
```powershell
cd D:\www\sport-era
powershell -ExecutionPolicy Bypass -File .\scripts\deploy-server.ps1 -RemoteDir /www/wwwroot/test-server.sbnews.net -DryRun
```
脚本会在打包后打印 `update log`:增量模式包含 commit 范围、commit 摘要、变更文件和删除文件;全量模式包含当前最新 commit 和默认打包目录。
每条部署日志都会带耗时,例如 `[+0.123s][total 1.456s]``+` 表示距离上一条日志的耗时,`total` 表示本次脚本启动后的累计耗时。服务器解压安装阶段会额外打印 `[deploy][remote]` 日志,用来判断耗时是在本地、上传还是远端解压。
### 发布后执行命令
如果需要解压后在服务器项目目录执行命令,可使用 `--post-install`
```powershell
cd D:\www\sport-era
powershell -ExecutionPolicy Bypass -File .\scripts\deploy-server.ps1 -Sudo -RemoteDir /www/wwwroot/test-server.sbnews.net -PostInstall "php think optimize:clear"
```
### 常见问题
1. `ssh: Could not resolve hostname sbnews`:检查 Windows 的 `C:\Users\Administrator\.ssh\config` 是否存在 `Host sbnews`,或直接用 `-HostName <服务器IP>`
2. PowerShell 禁止执行脚本:使用示例中的 `-ExecutionPolicy Bypass -File`,或以管理员身份调整当前用户执行策略。
3. `tar: Cannot utime``Cannot change mode`:远端目录权限不足,请加 `-Sudo`。脚本会使用 `sudo tar` 覆盖解压,并默认跳过还原 owner/permission,减少权限冲突。
4. 增量发布提示 commit 无效:确认 commit id 属于当前根目录 Git 可识别的历史。
5. `server is tracked as a root gitlink`:脚本检测到根 Git 只记录了 `server` 指针,无法做文件级增量,会自动改为打包当前工作区默认 5 个 server 目录。
6. 仍需使用 Bash 兼容版时,可继续运行 `bash ./scripts/deploy-server.sh ...`
<a href="https://www.likeshop.cn">![likeshop全开源商城](https://server.likeshop.cn/uploads/gitad/fastbuildAI.png)</a><br>
<a href="https://www.likeshop.cn">![likeshop全开源商城](https://server.likeshop.cn/uploads/gitad/likeshop.png)</a><br>
<a href="https://www.buildingai.cc">![BuildingAI](https://server.likeshop.cn/uploads/gitad/fastbuildAI.png)</a><br>