Files
soul-yongping/.github/workflows/README.md

109 lines
3.4 KiB
Markdown
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.

# GitHub Actions 自动化部署配置说明
## 📋 概述
本项目已配置 GitHub Actions 工作流,支持在推送代码到 `soul-content` 分支时自动部署到宝塔服务器。
## ✅ 项目兼容性
当前项目**完全支持** GitHub Actions 部署方式,因为:
- ✅ 使用 `standalone` 模式,构建产物独立完整
- ✅ 使用 pnpm 包管理器
- ✅ 已配置 PM2 启动方式(`node server.js`
- ✅ 端口配置为 3006
## 🔧 配置步骤
### 1. 在服务器上生成 SSH 密钥对
```bash
ssh root@42.194.232.22
ssh-keygen -t rsa -b 4096 -C "github-actions-deploy"
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
cat ~/.ssh/id_rsa # 复制私钥内容
```
### 2. 在 GitHub 仓库添加 Secrets
进入 GitHub 仓库:`Settings``Secrets and variables``Actions``New repository secret`
添加以下三个 Secrets
| Secret 名称 | 值 | 说明 |
|------------|-----|------|
| `SSH_HOST` | `42.194.232.22` | 服务器 IP |
| `SSH_USERNAME` | `root` | SSH 用户名 |
| `SSH_PRIVATE_KEY` | `-----BEGIN OPENSSH PRIVATE KEY-----...` | 服务器 SSH 私钥(完整内容) |
### 3. 修改工作流分支(如需要)
编辑 `.github/workflows/deploy.yml`,修改触发分支:
```yaml
on:
push:
branches:
- soul-content # 改为你的分支名
```
### 4. 提交并推送
```bash
git add .github/workflows/deploy.yml
git commit -m "添加 GitHub Actions 自动化部署"
git push origin soul-content
```
## 🚀 工作流程
1. **构建阶段**
- 安装 Node.js 22
- 安装 pnpm
- 安装项目依赖
- 执行 `pnpm build`(生成 standalone 输出)
2. **打包阶段**
- 复制 `.next/standalone` 内容
- 复制 `.next/static` 静态资源
- 复制 `public` 目录
- 复制 `ecosystem.config.cjs` PM2 配置
- 打包为 `deploy.tar.gz`
3. **部署阶段**
- 通过 SCP 上传到服务器 `/tmp/`
- SSH 连接到服务器
- 备份当前版本(可选)
- 解压到 `/www/wwwroot/soul`
- 重启 PM2 应用 `soul`
## 📊 与当前部署方式对比
| 特性 | GitHub Actions | deploy_soul.py |
|------|---------------|----------------|
| **触发方式** | 自动Push 代码) | 手动执行脚本 |
| **构建环境** | GitHub Ubuntu | 本地环境 |
| **构建速度** | 较慢(每次安装依赖) | 较快(本地缓存) |
| **适用场景** | 团队协作、CI/CD | 本地开发、快速部署 |
| **Windows 兼容** | ✅ 完美(云端构建) | ⚠️ 需处理符号链接 |
## ⚠️ 注意事项
1. **首次部署**:确保服务器上 `/www/wwwroot/soul` 目录存在且 PM2 已配置项目
2. **环境变量**:如果项目需要环境变量,需要在服务器上配置(宝塔面板或 `.env` 文件)
3. **数据库连接**:确保服务器能访问数据库
4. **构建时间**:首次构建可能需要 5-10 分钟后续会更快GitHub Actions 缓存)
## 🔍 查看部署日志
1. 在 GitHub 仓库点击 `Actions` 标签
2. 选择最新的工作流运行
3. 查看各步骤的详细日志
## 🆚 两种部署方式选择
- **使用 GitHub Actions**:适合团队协作,代码推送即自动部署
- **使用 deploy_soul.py**:适合本地快速测试,需要手动控制部署时机
两种方式可以并存,根据场景选择使用。