Files
sanguo_vnpy_v2/.gitea/workflows/vps-deploy.yml
T

106 lines
4.9 KiB
YAML

name: VPS Deploy
# VPS 生产部署(人工 UI 点 Run workflow 或 agent 通过 API dispatch 触发)。
# 与 ci-cd.yml 分离: ci-cd(push) 只到 NAS 验证; VPS 必须 nas-verify 绿后由此 workflow 主动触发。
#
# 严格版本一致: 接收 sha input, checkout 该 commit(= NAS 验证通过的那个), 部署到 VPS。
# 不会部署 nas-verify 未验证过的代码。
#
# 触发方式(2026-08-14 起带口令闸门,见 inputs.confirm):
# 人工: Gitea → Actions → vps-deploy.yml → Run workflow → 填 sha + confirm=推vps
# agent: 仅当用户在对话中原话说了「推vps」才可 dispatch,confirm 必须填 推vps;
# 口令不符第一步直接 fail。不得代填/推断/沿用旧授权。
on:
workflow_dispatch:
inputs:
sha:
description: "NAS 验证通过的 commit SHA(完整 40 位, ci-cd 的 nas-verify 输出)"
required: true
type: string
confirm:
description: "部署确认口令: 只有用户在对话中说了「推vps」才可填(用户原话,agent 不得代填/推断)"
required: true
type: string
jobs:
vps:
runs-on: macos-arm64
env:
HOME: /Users/chufeng
steps:
- name: 口令闸门(用户没说「推vps」就不许部署)
run: |
if [ "${{ inputs.confirm }}" != "推vps" ]; then
echo "❌ 口令不符——部署未获用户确认,退出。"
exit 1
fi
echo "✅ 口令确认,继续部署。"
- name: checkout 指定 commit(= NAS 验证过的版本)
env:
TOKEN: ${{ github.token }}
run: |
set -e
SHA="${{ inputs.sha }}"
git init .
git config --local http.proxy ""
git config --local https.proxy ""
git remote add origin "http://oauth2:${TOKEN}@192.168.2.154:3000/${GITHUB_REPOSITORY}.git"
git fetch --depth 1 origin "$SHA"
git checkout FETCH_HEAD
echo "部署 commit: $(git rev-parse HEAD)"
- name: promote 代码到 VPS (只推 VPS, 不动 NAS)
run: bash scripts/nas_sync/promote.sh --target vps
- name: VPS 冒烟 (LocalUnifiedProvider 真数据)
run: ssh 49.232.102.198 'cd C:\sanguo_vnpy_v2 && C:\Python310\python.exe -X utf8 scripts/data_platform/verify_unified_e2e.py'
- name: 前端构建 + 推 dist 到 VPS (公网前端无管线, 此处补齐)
run: |
set -e
cd frontend
npm ci
npm run build
cd ..
tar czf /tmp/dist_latest.tgz -C frontend/dist .
scp -o ConnectTimeout=20 /tmp/dist_latest.tgz 49.232.102.198:C:/sanguo_vnpy_v2/dist_latest.tgz
- name: VPS 替换 dist + 重启 sanguo-api (等端口释放防 10048 竞态, 再等起服)
run: |
set -e
ssh 49.232.102.198 'C:\Python310\python.exe -X utf8 C:\sanguo_vnpy_v2\scripts\nas_sync\vps_update_dist.py' | tee /tmp/dist_swap.log
grep -q DIST_UPDATE_DONE /tmp/dist_swap.log
# 停 API → 等端口彻底释放(旧实例被杀后端口有滞留, 立即重启会 10048 静默崩)
ssh 49.232.102.198 'schtasks /end /tn sanguo-api' || true
for i in $(seq 1 30); do
ssh 49.232.102.198 'netstat -ano -p tcp' | tr -d '\r' | grep -q ':8000.*LISTEN' || { echo "port freed after $((i*2))s"; break; }
sleep 2
done
ssh 49.232.102.198 'schtasks /run /tn sanguo-api'
# 等新实例监听 + /docs 可用(VPS 启动需数分钟, 上限 10 分钟)
for i in $(seq 1 60); do
if ssh 49.232.102.198 'netstat -ano -p tcp' | tr -d '\r' | grep -q ':8000.*LISTEN'; then
echo "API listening after ~$((i*10))s"; break
fi
sleep 10
if [ "$i" = "60" ]; then echo "API 10 分钟未监听 8000"; exit 1; fi
done
for i in $(seq 1 12); do
ssh 49.232.102.198 'powershell -NoProfile -Command "(Invoke-WebRequest -UseBasicParsing http://127.0.0.1:8000/docs).StatusCode"' 2>/dev/null | grep -q 200 && { echo "API /docs 200"; exit 0; }
sleep 10
done
echo "API 监听但 /docs 2 分钟未返回 200"; exit 1
- name: 重启 VPS 常驻进程 (supervisor/影子柜台吃新代码; 盘中自动跳过)
run: bash scripts/nas_sync/vps_restart_resident.sh
- name: mark vps-deployed tag (VPS 版本真相, session 用 git log vps-deployed..HEAD 查未部署)
env:
TOKEN: ${{ github.token }}
run: |
set -e
SHA="${{ inputs.sha }}"
git tag -f vps-deployed "$SHA"
git push -f "http://oauth2:${TOKEN}@192.168.2.154:3000/${GITHUB_REPOSITORY}.git" refs/tags/vps-deployed
echo "✅ vps-deployed → ${SHA::7}"
- name: 关闭 [待推VPS] issue (tag=HEAD → gap 清空 = 绿)
env:
GTOKEN: ${{ github.token }}
run: |
export VPS_LOG=""
python3 scripts/ci/vps_pending_check.py