Files
gongxue-base/.gitea/workflows/deploy.yml
wangziqi 151ef06ec2 refactor: 精简部署流程,移除 MySQL 管理
MySQL 已在服务器运行,部署只需三步:
1. 本地构建 → rsync 同步
2. npm ci(首次)+ migration:run
3. pm2 startOrReload
2026-07-20 15:35:45 +08:00

81 lines
2.4 KiB
YAML
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.

# PM2 手动部署工作流
# 手动触发 → 构建 → rsync → 安装依赖 → 数据库迁移 → PM2 重载
#
# 前置准备:
# 1. 服务器 MySQL 已在运行
# 2. 服务器已安装 PM2: npm i -g pm2
# 3. Gitea 仓库 Settings → Secrets 配置:
# - SSH_PRIVATE_KEY : 部署用 SSH 私钥
# - SSH_HOST : 服务器地址
# - SSH_USER : SSH 用户名
# - SSH_PORT : SSH 端口(默认 22可选
# - REMOTE_DIR : 服务器项目目录,如 /opt/gongxue
name: PM2 部署
on:
workflow_dispatch:
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: 检出代码
uses: actions/checkout@v4
- name: 安装 Node.js
uses: actions/setup-node@v4
with:
node-version: '22'
- name: 安装依赖 & 构建
run: |
npm ci
npm run build -w @gongxue/server
npm run build -w @gongxue/admin
- name: 配置 SSH
run: |
mkdir -p ~/.ssh
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/deploy_key
chmod 600 ~/.ssh/deploy_key
cat >> ~/.ssh/config <<'EOF'
Host deploy-server
HostName ${{ secrets.SSH_HOST }}
User ${{ secrets.SSH_USER }}
Port ${{ secrets.SSH_PORT || '22' }}
IdentityFile ~/.ssh/deploy_key
StrictHostKeyChecking accept-new
EOF
- name: 同步代码到服务器
run: |
rsync -avz --delete \
--exclude='node_modules' \
--exclude='.git' \
--exclude='*.db' \
--exclude='.DS_Store' \
--exclude='logs/' \
--exclude='.turbo/' \
--exclude='.claude/' \
--exclude='.codegraph/' \
./ deploy-server:${{ secrets.REMOTE_DIR }}/
- name: 安装依赖 → 迁移 → PM2 重载
run: |
ssh deploy-server "
cd ${{ secrets.REMOTE_DIR }}
mkdir -p logs
if [ ! -d node_modules ]; then
echo '首次部署,安装生产依赖...'
npm ci --omit=dev
fi
echo '执行数据库迁移...'
npm run migration:run -w @gongxue/server
echo 'PM2 重载...'
pm2 startOrReload ecosystem.config.cjs --update-env
pm2 save
echo '=== PM2 状态 ==='
pm2 status
"