chore: 新增线上一键升级脚本并完善忽略规则

This commit is contained in:
Nixevol
2026-07-20 17:03:40 +08:00
parent 733829eca0
commit a7997a2976
3 changed files with 158 additions and 0 deletions
+2
View File
@@ -55,3 +55,5 @@ venv/
.cursor/
.cursorindexingignore
.specstory/
.codex/
.agents/
+94
View File
@@ -0,0 +1,94 @@
#!/usr/bin/env bash
# artveris 远端升级序列(由 upgrade-remote.ps1 上传并调用):
# 备份(DB+代码) -> 解压新包 -> 停服 -> upgrade.sh -> 重启 -> 健康检查
# 用法: bash upgrade-remote-steps.sh <包名不含扩展名> [部署目录] [pm2项目名]
set -eu
PKG="${1:?用法: bash upgrade-remote-steps.sh <包名不含扩展名> [部署目录] [pm2项目名]}"
DEPLOY="${2:-/www/wwwroot/artveris}"
PM2_NAME="${3:-server}"
UP="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd -P)"
STAMP=$(date +%Y%m%d%H%M%S)
BK="$UP/backup"
echo "==== [0] 前置检查 ===="
[ -f "$UP/$PKG.tar.gz" ] || { echo "缺少 $UP/$PKG.tar.gz"; exit 1; }
[ -f "$DEPLOY/.env" ] || { echo "缺少 $DEPLOY/.env,目标不是有效部署"; exit 1; }
pm2 describe "$PM2_NAME" >/dev/null 2>&1 || { echo "pm2 项目 $PM2_NAME 不存在"; exit 1; }
mkdir -p "$BK"
echo "==== [1] 数据库备份 ===="
CNF=$(mktemp /tmp/artveris-dump.XXXXXX.cnf)
chmod 600 "$CNF"
DB_NAME=$(node -e "
const fs=require('fs');
const m=fs.readFileSync('$DEPLOY/.env','utf8').match(/^DATABASE_URL\s*=\s*\"?([^\"\r\n]+)\"?/m);
if(!m){console.error('未找到 DATABASE_URL');process.exit(1)}
const u=new URL(m[1]);
const esc=(s)=>decodeURIComponent(s).replace(/\"/g,'\\\\\"');
const cnf='[client]\nhost='+u.hostname+'\nport='+(u.port||3306)+'\nuser=\"'+esc(u.username)+'\"\npassword=\"'+esc(u.password)+'\"\n';
fs.writeFileSync('$CNF',cnf);
console.log(u.pathname.slice(1).split('?')[0]);
")
DB_FILE="$BK/db-$DB_NAME-$STAMP.sql.gz"
mysqldump --defaults-extra-file="$CNF" --single-transaction --no-tablespaces --default-character-set=utf8mb4 "$DB_NAME" | gzip > "$DB_FILE"
rm -f "$CNF"
[ -s "$DB_FILE" ] || { echo "数据库备份为空,中止"; exit 1; }
echo "DB 备份: $(ls -lh "$DB_FILE" | awk '{print $5, $9}')"
echo "==== [2] 代码快照备份(不含 node_modules/storage/backups) ===="
CODE_FILE="$BK/code-$STAMP.tar.gz"
tar -czf "$CODE_FILE" -C "$DEPLOY" --exclude=./node_modules --exclude=./storage --exclude=./backups .
echo "代码备份: $(ls -lh "$CODE_FILE" | awk '{print $5, $9}')"
echo "==== [3] 解压新包 ===="
cd "$UP"
rm -rf "$PKG"
tar -xzf "$PKG.tar.gz"
[ -f "$UP/$PKG/upgrade.sh" ] || { echo "包内缺少 upgrade.sh"; exit 1; }
echo "==== [4] 停止服务 ===="
pm2 stop "$PM2_NAME"
echo "==== [5] 执行升级(替换代码 + 装依赖 + prisma generate + migrate deploy) ===="
if ! sh "$UP/$PKG/upgrade.sh" "$DEPLOY"; then
echo "升级失败!服务保持停止状态。可用备份回滚:$CODE_FILE / $DB_FILE"
echo "回滚代码: tar -xzf $CODE_FILE -C $DEPLOY 后 pm2 restart $PM2_NAME"
exit 1
fi
echo "==== [6] 重启服务 ===="
pm2 restart "$PM2_NAME"
pm2 save >/dev/null 2>&1 || true
echo "==== [7] 健康检查 ===="
PORT=$(grep -E '^API_PORT=' "$DEPLOY/.env" | head -1 | sed 's/^API_PORT=//; s/\"//g')
PORT=${PORT:-3000}
OK=0
for i in 1 2 3 4 5 6 7 8 9 10; do
sleep 3
CODE=$(curl -s -o /dev/null -w '%{http_code}' "http://127.0.0.1:$PORT/api/install/status" || true)
if [ "$CODE" = "200" ]; then OK=1; break; fi
echo "等待服务启动($i)... HTTP $CODE"
done
if [ "$OK" != "1" ]; then
echo "健康检查失败!请立刻查看 pm2 logs $PM2_NAME;备份在 $BK"
pm2 logs "$PM2_NAME" --lines 30 --nostream || true
exit 1
fi
curl -s -o /dev/null -w 'install/status HTTP %{http_code}\n' "http://127.0.0.1:$PORT/api/install/status"
curl -s -o /dev/null -w 'admin HTTP %{http_code}\n' "http://127.0.0.1:$PORT/admin/"
curl -s -o /dev/null -w 'trace HTTP %{http_code}\n' "http://127.0.0.1:$PORT/trace/"
echo "==== [8] 清理:保留最近 3 份升级包与备份 ===="
cd "$UP"
ls -1t artveris-upgrade-*.tar.gz 2>/dev/null | tail -n +4 | while read -r old; do
rm -rf "$old" "${old%.tar.gz}"
echo "已清理旧包 $old"
done
ls -1dt artveris-upgrade-*/ 2>/dev/null | tail -n +4 | xargs -r rm -rf
cd "$BK"
ls -1t db-*.sql.gz 2>/dev/null | tail -n +4 | xargs -r rm -f
ls -1t code-*.tar.gz 2>/dev/null | tail -n +4 | xargs -r rm -f
echo "==== 升级完成 ===="
+62
View File
@@ -0,0 +1,62 @@
# artveris one-click remote upgrade (BaoTa/PM2 deployment).
# Flow: build upgrade package locally -> upload to server -> remote auto backup (DB + code)
# -> stop pm2 -> upgrade.sh (replace code / install deps / migrate) -> restart -> health check -> prune old.
#
# Usage:
# .\scripts\upgrade-remote.ps1 # build + deploy (recommended)
# .\scripts\upgrade-remote.ps1 -SkipBuild # deploy the latest package already in .release
#
# Requires: passwordless SSH (key auth) to the server.
[CmdletBinding()]
param(
[string]$Server = 'root@8.134.255.136',
[string]$RemoteDir = '/www/upgrade',
[string]$DeployDir = '/www/wwwroot/artveris',
[string]$Pm2Name = 'server',
[switch]$SkipBuild
)
$ErrorActionPreference = 'Stop'
Set-StrictMode -Version Latest
# Remote output is UTF-8; align console decoding to avoid mojibake on Chinese text.
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$root = Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Path)
Set-Location $root
if (-not $SkipBuild) {
Write-Host '[deploy] Building upgrade package (NOTE: rebuilds apps/api/dist and will break a running local dev API)...'
$python = Join-Path $root '.venv\Scripts\python.exe'
if (-not (Test-Path -LiteralPath $python)) { $python = 'python' }
& $python (Join-Path $root 'scripts\build.py') --upgrade
if ($LASTEXITCODE -ne 0) { throw "Build failed (exit code $LASTEXITCODE)." }
}
$pkg = Get-ChildItem (Join-Path $root '.release') -Filter 'artveris-upgrade-*.tar.gz' -ErrorAction SilentlyContinue |
Sort-Object LastWriteTime -Descending | Select-Object -First 1
if ($null -eq $pkg) { throw 'No upgrade package found. Run without -SkipBuild to build one.' }
$pkgName = $pkg.Name -replace '\.tar\.gz$', ''
Write-Host ("[deploy] Package: {0} ({1} MB)" -f $pkg.Name, [math]::Round($pkg.Length / 1MB, 2))
# Upload the remote step script as LF, no BOM (CRLF breaks bash).
$stepsSrc = Join-Path $root 'scripts\upgrade-remote-steps.sh'
if (-not (Test-Path -LiteralPath $stepsSrc)) { throw "Missing $stepsSrc" }
$stepsTmp = Join-Path $env:TEMP 'upgrade-remote-steps.sh'
$stepsText = (Get-Content -LiteralPath $stepsSrc -Raw) -replace "`r`n", "`n"
[System.IO.File]::WriteAllText($stepsTmp, $stepsText, (New-Object System.Text.UTF8Encoding($false)))
Write-Host "[deploy] Uploading to ${Server}:${RemoteDir}/ ..."
ssh -o BatchMode=yes $Server "mkdir -p '$RemoteDir'"
if ($LASTEXITCODE -ne 0) { throw 'SSH connection failed. Check key-based login.' }
scp -o BatchMode=yes $pkg.FullName $stepsTmp "${Server}:${RemoteDir}/"
if ($LASTEXITCODE -ne 0) { throw 'Upload failed.' }
Write-Host '[deploy] Running remote upgrade (auto backup first; aborts and keeps backups on failure)...'
ssh -o BatchMode=yes $Server "bash '$RemoteDir/upgrade-remote-steps.sh' '$pkgName' '$DeployDir' '$Pm2Name'"
if ($LASTEXITCODE -ne 0) {
throw "Remote upgrade failed (exit code $LASTEXITCODE). Service may be stopped. Backups are in $RemoteDir/backup/."
}
Write-Host '[deploy] Done. Remote health check passed.'
if (-not $SkipBuild) {
Write-Host '[deploy] Tip: restart local dev API with "pnpm dev:api" if it was running before the build.'
}