guanghulab/.forgejo/workflows/deploy-preview.yaml

134 lines
4.1 KiB
YAML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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.

# ════════════════════════════════════════════════════════════════
# 光湖 · 自动部署到预览环境
# Trigger: push to main
# 部署到测试+预览环境正式环境需冰朔在Gitea点Merge
# ════════════════════════════════════════════════════════════════
name: 自动部署预览
on:
push:
branches: [main]
paths:
- 'server/**'
- 'frontend/**'
- 'scripts/**'
- '.gitea/workflows/**'
workflow_dispatch:
jobs:
# ── 第一阶段:测试 ──────────────────────────────────
test:
runs-on: ubuntu
steps:
- name: 拉取最新代码
run: |
cd /data/guanghulab 2>/dev/null || exit 0
git fetch origin
git reset --hard origin/main
- name: 安装依赖
run: |
cd /data/guanghulab/server/secrets-vault 2>/dev/null || exit 0
npm install --production 2>/dev/null || true
- name: 健康检查
run: |
echo "═══ 服务健康检查 ═══"
FAILED=""
# Nginx
if systemctl is-active --quiet nginx; then
echo "✅ Nginx"
else
echo "❌ Nginx 宕机"
FAILED="$FAILED nginx"
fi
# Gitea
if curl -sf http://127.0.0.1:3000/ > /dev/null 2>&1; then
echo "✅ Gitea"
else
echo "❌ Gitea 宕机"
FAILED="$FAILED gitea"
fi
# Vault
if curl -sf http://127.0.0.1:8080/admin/__healthz > /dev/null 2>&1; then
echo "✅ Vault"
else
echo "⚠️ Vault 未运行"
fi
# Runner
if systemctl is-active --quiet gitea-runner; then
echo "✅ Runner"
else
echo "⚠️ Runner 未运行"
fi
if [[ -n "$FAILED" ]]; then
echo "❌ 关键服务宕机: $FAILED"
echo "⚠️ 跳过部署,等待修复"
exit 1
fi
echo "═══ 测试通过 ✅ ═══"
# ── 第二阶段:预览部署 ──────────────────────────────
preview:
needs: test
runs-on: ubuntu
steps:
- name: 同步代码到预览目录
run: |
REPO="/data/guanghulab"
PREVIEW="/data/guanghulab-preview"
if [[ ! -d "$REPO" ]]; then
echo "仓库目录不存在,跳过"
exit 0
fi
# 创建预览目录
mkdir -p "$PREVIEW"
# 同步代码(排除.git和node_modules
rsync -a --delete \
--exclude='.git' \
--exclude='node_modules' \
--exclude='.runtime' \
"$REPO/" "$PREVIEW/"
echo "✅ 预览环境已同步"
- name: 重启预览服务
run: |
PREVIEW="/data/guanghulab-preview"
if [[ ! -d "$PREVIEW/server/secrets-vault" ]]; then
exit 0
fi
cd "$PREVIEW/server/secrets-vault"
# 安装依赖
npm install --production 2>/dev/null || true
# 如果vault在跑重启它
if pm2 describe guanghulab-vault &>/dev/null; then
pm2 restart guanghulab-vault
echo "✅ Vault 已重启(使用最新代码)"
else
echo " Vault 未运行,不自动启动"
fi
- name: 部署完成通知
run: |
echo "═════════════════════════"
echo "✅ 预览环境已更新"
echo "时间: $(date '+%Y-%m-%d %H:%M:%S')"
echo "冰朔可在浏览器查看预览效果"
echo "正式部署需在Gitea创建PR并Merge"
echo "═════════════════════════"