feat: 完成后端-管理后台集成及部署配置
🚀 主要功能: - 完善后端API服务层,实现完整的CRUD操作 - 开发管理后台所有核心页面 (仪表板、照片、分类、标签、用户、设置) - 完成前后端完全集成,所有API接口正常对接 - 配置完整的CI/CD流水线,支持自动化部署 🎯 后端完善: - 实现PhotoService, CategoryService, TagService, UserService - 添加完整的API处理器和路由配置 - 支持Docker容器化部署 - 添加数据库迁移和健康检查 🎨 管理后台完成: - 仪表板: 实时统计数据展示 - 照片管理: 完整的CRUD操作,支持批量处理 - 分类管理: 树形结构展示和管理 - 标签管理: 颜色标签和统计信息 - 用户管理: 角色权限控制 - 系统设置: 多标签配置界面 - 添加pre-commit代码质量检查 🔧 部署配置: - Docker Compose完整配置 - 后端CI/CD流水线 (Docker部署) - 管理后台CI/CD流水线 (静态文件部署) - 前端CI/CD流水线优化 - 自动化脚本: 部署、备份、监控 - 完整的部署文档和运维指南 ✅ 集成完成: - 所有API接口正常连接 - 认证系统完整集成 - 数据获取和状态管理 - 错误处理和用户反馈 - 响应式设计优化
This commit is contained in:
336
.gitea/workflows/deploy-admin.yml
Normal file
336
.gitea/workflows/deploy-admin.yml
Normal file
@ -0,0 +1,336 @@
|
||||
name: 部署管理后台
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ main ]
|
||||
paths:
|
||||
- 'admin/**'
|
||||
- '.gitea/workflows/deploy-admin.yml'
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
test-and-build:
|
||||
name: 🧪 测试和构建
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: 📥 检出代码
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: 📦 设置 Node.js 环境
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: '18'
|
||||
cache: 'npm'
|
||||
cache-dependency-path: admin/package-lock.json
|
||||
|
||||
- name: 📦 安装依赖
|
||||
working-directory: ./admin
|
||||
run: npm ci
|
||||
|
||||
- name: 🔍 代码检查
|
||||
working-directory: ./admin
|
||||
run: |
|
||||
npm run lint
|
||||
npm run type-check
|
||||
|
||||
- name: 🎨 格式检查
|
||||
working-directory: ./admin
|
||||
run: npm run format
|
||||
|
||||
- name: 🧪 运行测试
|
||||
working-directory: ./admin
|
||||
run: npm run test
|
||||
|
||||
- name: 🔒 安全审计
|
||||
working-directory: ./admin
|
||||
run: npm audit --audit-level moderate
|
||||
|
||||
- name: 🏗️ 构建生产版本
|
||||
working-directory: ./admin
|
||||
env:
|
||||
VITE_APP_TITLE: 摄影作品集管理后台
|
||||
VITE_API_BASE_URL: https://api.photography.iriver.top
|
||||
VITE_UPLOAD_URL: https://api.photography.iriver.top/upload
|
||||
run: npm run build
|
||||
|
||||
- name: 📊 构建分析
|
||||
working-directory: ./admin
|
||||
run: |
|
||||
echo "📦 构建产物分析:"
|
||||
du -sh dist/
|
||||
echo "📁 文件列表:"
|
||||
find dist/ -type f -name "*.js" -o -name "*.css" | head -10
|
||||
echo "📈 文件大小统计:"
|
||||
find dist/ -type f \( -name "*.js" -o -name "*.css" \) -exec ls -lh {} + | awk '{print $5, $9}' | sort -hr | head -10
|
||||
|
||||
- name: 📦 打包构建产物
|
||||
uses: actions/upload-artifact@v3
|
||||
with:
|
||||
name: admin-dist
|
||||
path: admin/dist/
|
||||
retention-days: 7
|
||||
|
||||
deploy:
|
||||
name: 🚀 部署到生产环境
|
||||
runs-on: ubuntu-latest
|
||||
needs: test-and-build
|
||||
if: github.ref == 'refs/heads/main'
|
||||
|
||||
steps:
|
||||
- name: 📥 检出代码
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: 📦 设置 Node.js 环境
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: '18'
|
||||
cache: 'npm'
|
||||
cache-dependency-path: admin/package-lock.json
|
||||
|
||||
- name: 📦 安装依赖
|
||||
working-directory: ./admin
|
||||
run: npm ci
|
||||
|
||||
- name: 🏗️ 构建生产版本
|
||||
working-directory: ./admin
|
||||
env:
|
||||
VITE_APP_TITLE: 摄影作品集管理后台
|
||||
VITE_API_BASE_URL: https://api.photography.iriver.top
|
||||
VITE_UPLOAD_URL: https://api.photography.iriver.top/upload
|
||||
run: npm run build
|
||||
|
||||
- name: 📊 压缩构建产物
|
||||
working-directory: ./admin
|
||||
run: |
|
||||
tar -czf admin-dist.tar.gz -C dist .
|
||||
echo "压缩完成: $(ls -lh admin-dist.tar.gz)"
|
||||
|
||||
- name: 🚀 部署到服务器
|
||||
uses: appleboy/ssh-action@v1.0.0
|
||||
with:
|
||||
host: ${{ secrets.HOST }}
|
||||
username: ${{ secrets.USERNAME }}
|
||||
key: ${{ secrets.SSH_KEY }}
|
||||
port: ${{ secrets.PORT }}
|
||||
script: |
|
||||
# 设置变量
|
||||
ADMIN_DIR="/home/gitea/www/photography-admin"
|
||||
BACKUP_DIR="/home/gitea/backups/photography-admin"
|
||||
TEMP_DIR="/tmp/photography-admin-deploy"
|
||||
|
||||
echo "🚀 开始部署管理后台..."
|
||||
|
||||
# 创建临时目录
|
||||
mkdir -p "$TEMP_DIR"
|
||||
|
||||
# 创建备份目录
|
||||
mkdir -p "$BACKUP_DIR"
|
||||
|
||||
# 备份当前版本
|
||||
if [ -d "$ADMIN_DIR" ] && [ "$(ls -A $ADMIN_DIR)" ]; then
|
||||
echo "📦 备份当前版本..."
|
||||
BACKUP_NAME="admin-$(date +%Y%m%d-%H%M%S).tar.gz"
|
||||
tar -czf "$BACKUP_DIR/$BACKUP_NAME" -C "$ADMIN_DIR" .
|
||||
echo "✅ 备份完成: $BACKUP_NAME"
|
||||
|
||||
# 保留最近10个备份
|
||||
cd "$BACKUP_DIR"
|
||||
ls -t admin-*.tar.gz | tail -n +11 | xargs -r rm
|
||||
echo "🧹 清理旧备份完成"
|
||||
fi
|
||||
|
||||
echo "📁 准备部署目录..."
|
||||
mkdir -p "$ADMIN_DIR"
|
||||
|
||||
- name: 📤 上传构建产物
|
||||
uses: appleboy/scp-action@v0.1.4
|
||||
with:
|
||||
host: ${{ secrets.HOST }}
|
||||
username: ${{ secrets.USERNAME }}
|
||||
key: ${{ secrets.SSH_KEY }}
|
||||
port: ${{ secrets.PORT }}
|
||||
source: admin/admin-dist.tar.gz
|
||||
target: /tmp/photography-admin-deploy/
|
||||
strip_components: 1
|
||||
|
||||
- name: 🔄 解压并部署
|
||||
uses: appleboy/ssh-action@v1.0.0
|
||||
with:
|
||||
host: ${{ secrets.HOST }}
|
||||
username: ${{ secrets.USERNAME }}
|
||||
key: ${{ secrets.SSH_KEY }}
|
||||
port: ${{ secrets.PORT }}
|
||||
script: |
|
||||
# 设置变量
|
||||
ADMIN_DIR="/home/gitea/www/photography-admin"
|
||||
TEMP_DIR="/tmp/photography-admin-deploy"
|
||||
|
||||
echo "🔄 解压新版本..."
|
||||
cd "$TEMP_DIR"
|
||||
tar -xzf admin-dist.tar.gz
|
||||
|
||||
echo "📂 部署新版本..."
|
||||
# 清空目标目录
|
||||
rm -rf "$ADMIN_DIR"/*
|
||||
|
||||
# 复制新文件
|
||||
cp -r * "$ADMIN_DIR/"
|
||||
|
||||
echo "🔐 设置文件权限..."
|
||||
chown -R gitea:gitea "$ADMIN_DIR"
|
||||
chmod -R 755 "$ADMIN_DIR"
|
||||
|
||||
# 设置正确的文件权限
|
||||
find "$ADMIN_DIR" -type f -name "*.html" -o -name "*.js" -o -name "*.css" -o -name "*.json" | xargs chmod 644
|
||||
find "$ADMIN_DIR" -type d | xargs chmod 755
|
||||
|
||||
echo "🧹 清理临时文件..."
|
||||
rm -rf "$TEMP_DIR"
|
||||
|
||||
echo "✅ 管理后台部署完成!"
|
||||
echo "📊 部署统计:"
|
||||
echo "文件数量: $(find $ADMIN_DIR -type f | wc -l)"
|
||||
echo "目录大小: $(du -sh $ADMIN_DIR)"
|
||||
|
||||
- name: 🔍 健康检查
|
||||
uses: appleboy/ssh-action@v1.0.0
|
||||
with:
|
||||
host: ${{ secrets.HOST }}
|
||||
username: ${{ secrets.USERNAME }}
|
||||
key: ${{ secrets.SSH_KEY }}
|
||||
port: ${{ secrets.PORT }}
|
||||
script: |
|
||||
echo "🔍 执行健康检查..."
|
||||
|
||||
# 检查文件是否存在
|
||||
if [ -f "/home/gitea/www/photography-admin/index.html" ]; then
|
||||
echo "✅ index.html 文件存在"
|
||||
else
|
||||
echo "❌ index.html 文件不存在"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 检查网站是否可访问 (本地检查)
|
||||
sleep 5
|
||||
if curl -f -s -o /dev/null https://admin.photography.iriver.top; then
|
||||
echo "✅ 管理后台访问正常"
|
||||
else
|
||||
echo "⚠️ 管理后台访问异常,请检查 Caddy 配置"
|
||||
fi
|
||||
|
||||
# 重新加载 Caddy (确保新文件被正确服务)
|
||||
sudo systemctl reload caddy
|
||||
echo "🔄 Caddy 配置已重新加载"
|
||||
|
||||
- name: 📧 发送部署通知
|
||||
if: always()
|
||||
uses: appleboy/telegram-action@master
|
||||
with:
|
||||
to: ${{ secrets.TELEGRAM_TO }}
|
||||
token: ${{ secrets.TELEGRAM_TOKEN }}
|
||||
message: |
|
||||
🎨 摄影作品集管理后台部署
|
||||
|
||||
📦 项目: ${{ github.repository }}
|
||||
🌿 分支: ${{ github.ref_name }}
|
||||
👤 提交者: ${{ github.actor }}
|
||||
📝 提交信息: ${{ github.event.head_commit.message }}
|
||||
|
||||
${{ job.status == 'success' && '✅ 部署成功' || '❌ 部署失败' }}
|
||||
|
||||
🌐 管理后台: https://admin.photography.iriver.top
|
||||
📱 前端: https://photography.iriver.top
|
||||
|
||||
rollback:
|
||||
name: 🔄 回滚部署
|
||||
runs-on: ubuntu-latest
|
||||
if: failure() && github.ref == 'refs/heads/main'
|
||||
needs: deploy
|
||||
|
||||
steps:
|
||||
- name: 🔄 执行回滚
|
||||
uses: appleboy/ssh-action@v1.0.0
|
||||
with:
|
||||
host: ${{ secrets.HOST }}
|
||||
username: ${{ secrets.USERNAME }}
|
||||
key: ${{ secrets.SSH_KEY }}
|
||||
port: ${{ secrets.PORT }}
|
||||
script: |
|
||||
ADMIN_DIR="/home/gitea/www/photography-admin"
|
||||
BACKUP_DIR="/home/gitea/backups/photography-admin"
|
||||
|
||||
echo "🔄 开始回滚管理后台..."
|
||||
|
||||
# 查找最新的备份
|
||||
LATEST_BACKUP=$(ls -t "$BACKUP_DIR"/admin-*.tar.gz 2>/dev/null | head -n 1)
|
||||
|
||||
if [ -n "$LATEST_BACKUP" ]; then
|
||||
echo "📦 找到备份文件: $LATEST_BACKUP"
|
||||
|
||||
# 清空当前目录
|
||||
rm -rf "$ADMIN_DIR"/*
|
||||
|
||||
# 恢复备份
|
||||
tar -xzf "$LATEST_BACKUP" -C "$ADMIN_DIR"
|
||||
|
||||
# 设置权限
|
||||
chown -R gitea:gitea "$ADMIN_DIR"
|
||||
chmod -R 755 "$ADMIN_DIR"
|
||||
|
||||
# 重新加载 Caddy
|
||||
sudo systemctl reload caddy
|
||||
|
||||
echo "✅ 回滚完成"
|
||||
else
|
||||
echo "❌ 未找到备份文件,无法回滚"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
security-scan:
|
||||
name: 🔒 安全扫描
|
||||
runs-on: ubuntu-latest
|
||||
needs: test-and-build
|
||||
|
||||
steps:
|
||||
- name: 📥 检出代码
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: 📦 设置 Node.js 环境
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: '18'
|
||||
cache: 'npm'
|
||||
cache-dependency-path: admin/package-lock.json
|
||||
|
||||
- name: 📦 安装依赖
|
||||
working-directory: ./admin
|
||||
run: npm ci
|
||||
|
||||
- name: 🔒 运行安全扫描
|
||||
working-directory: ./admin
|
||||
run: |
|
||||
echo "🔍 扫描已知漏洞..."
|
||||
npm audit --audit-level high --production
|
||||
|
||||
echo "📊 依赖分析..."
|
||||
npx license-checker --summary
|
||||
|
||||
echo "🔍 检查过时依赖..."
|
||||
npx npm-check-updates
|
||||
|
||||
- name: 📊 生成安全报告
|
||||
working-directory: ./admin
|
||||
run: |
|
||||
echo "# 安全扫描报告" > security-report.md
|
||||
echo "## 日期: $(date)" >> security-report.md
|
||||
echo "## 依赖统计" >> security-report.md
|
||||
npm ls --depth=0 --json | jq -r '.dependencies | keys | length' | xargs -I {} echo "依赖数量: {}" >> security-report.md
|
||||
echo "## 许可证检查" >> security-report.md
|
||||
npx license-checker --csv >> security-report.md
|
||||
|
||||
- name: 📤 上传安全报告
|
||||
uses: actions/upload-artifact@v3
|
||||
with:
|
||||
name: security-report
|
||||
path: admin/security-report.md
|
||||
261
.gitea/workflows/deploy-backend.yml
Normal file
261
.gitea/workflows/deploy-backend.yml
Normal file
@ -0,0 +1,261 @@
|
||||
name: 部署后端服务
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ main ]
|
||||
paths:
|
||||
- 'backend/**'
|
||||
- 'docker-compose.yml'
|
||||
- '.env.example'
|
||||
- '.gitea/workflows/deploy-backend.yml'
|
||||
workflow_dispatch:
|
||||
|
||||
env:
|
||||
REGISTRY: registry.cn-hangzhou.aliyuncs.com
|
||||
IMAGE_NAME: photography/backend
|
||||
|
||||
jobs:
|
||||
test:
|
||||
name: 🧪 测试后端
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
services:
|
||||
postgres:
|
||||
image: postgres:15-alpine
|
||||
env:
|
||||
POSTGRES_PASSWORD: postgres
|
||||
POSTGRES_DB: photography_test
|
||||
options: >-
|
||||
--health-cmd pg_isready
|
||||
--health-interval 10s
|
||||
--health-timeout 5s
|
||||
--health-retries 5
|
||||
ports:
|
||||
- 5432:5432
|
||||
|
||||
steps:
|
||||
- name: 📥 检出代码
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: 🐹 设置 Go 环境
|
||||
uses: actions/setup-go@v4
|
||||
with:
|
||||
go-version: '1.21'
|
||||
cache-dependency-path: backend/go.sum
|
||||
|
||||
- name: 📦 下载依赖
|
||||
working-directory: ./backend
|
||||
run: go mod download
|
||||
|
||||
- name: 🔍 代码检查
|
||||
working-directory: ./backend
|
||||
run: |
|
||||
go vet ./...
|
||||
go fmt ./...
|
||||
# 检查是否有格式化变更
|
||||
if [ -n "$(git status --porcelain)" ]; then
|
||||
echo "代码格式不符合规范,请运行 go fmt"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
- name: 🧪 运行测试
|
||||
working-directory: ./backend
|
||||
env:
|
||||
DB_HOST: localhost
|
||||
DB_PORT: 5432
|
||||
DB_USER: postgres
|
||||
DB_PASSWORD: postgres
|
||||
DB_NAME: photography_test
|
||||
JWT_SECRET: test_jwt_secret_for_ci_cd_testing_only
|
||||
run: |
|
||||
go test -v -race -coverprofile=coverage.out ./...
|
||||
go tool cover -html=coverage.out -o coverage.html
|
||||
|
||||
- name: 📊 上传覆盖率报告
|
||||
uses: actions/upload-artifact@v3
|
||||
with:
|
||||
name: coverage-report
|
||||
path: backend/coverage.html
|
||||
|
||||
- name: 🏗️ 构建检查
|
||||
working-directory: ./backend
|
||||
run: |
|
||||
CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main cmd/server/main.go
|
||||
echo "构建成功"
|
||||
|
||||
build-and-deploy:
|
||||
name: 🚀 构建并部署
|
||||
runs-on: ubuntu-latest
|
||||
needs: test
|
||||
if: github.ref == 'refs/heads/main'
|
||||
|
||||
steps:
|
||||
- name: 📥 检出代码
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: 🐳 设置 Docker Buildx
|
||||
uses: docker/setup-buildx-action@v3
|
||||
|
||||
- name: 🔑 登录到镜像仓库
|
||||
uses: docker/login-action@v3
|
||||
with:
|
||||
registry: ${{ env.REGISTRY }}
|
||||
username: ${{ secrets.DOCKER_USERNAME }}
|
||||
password: ${{ secrets.DOCKER_PASSWORD }}
|
||||
|
||||
- name: 📝 提取元数据
|
||||
id: meta
|
||||
uses: docker/metadata-action@v5
|
||||
with:
|
||||
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
|
||||
tags: |
|
||||
type=ref,event=branch
|
||||
type=ref,event=pr
|
||||
type=sha,prefix={{branch}}-
|
||||
type=raw,value=latest,enable={{is_default_branch}}
|
||||
|
||||
- name: 🏗️ 构建并推送镜像
|
||||
uses: docker/build-push-action@v5
|
||||
with:
|
||||
context: ./backend
|
||||
file: ./backend/Dockerfile
|
||||
platforms: linux/amd64
|
||||
push: true
|
||||
tags: ${{ steps.meta.outputs.tags }}
|
||||
labels: ${{ steps.meta.outputs.labels }}
|
||||
cache-from: type=gha
|
||||
cache-to: type=gha,mode=max
|
||||
|
||||
- name: 🚀 部署到生产环境
|
||||
uses: appleboy/ssh-action@v1.0.0
|
||||
with:
|
||||
host: ${{ secrets.HOST }}
|
||||
username: ${{ secrets.USERNAME }}
|
||||
key: ${{ secrets.SSH_KEY }}
|
||||
port: ${{ secrets.PORT }}
|
||||
script: |
|
||||
# 切换到项目目录
|
||||
cd /home/gitea/photography
|
||||
|
||||
# 拉取最新代码
|
||||
git pull origin main
|
||||
|
||||
# 备份当前运行的容器 (如果存在)
|
||||
if docker ps -q -f name=photography_backend; then
|
||||
echo "📦 备份当前后端容器..."
|
||||
docker commit photography_backend photography_backend_backup_$(date +%Y%m%d_%H%M%S)
|
||||
fi
|
||||
|
||||
# 停止现有服务
|
||||
echo "🛑 停止现有服务..."
|
||||
docker-compose down backend || true
|
||||
|
||||
# 拉取最新镜像
|
||||
echo "📥 拉取最新镜像..."
|
||||
docker-compose pull backend
|
||||
|
||||
# 启动数据库 (如果未运行)
|
||||
echo "🗄️ 确保数据库运行..."
|
||||
docker-compose up -d postgres redis
|
||||
|
||||
# 等待数据库就绪
|
||||
echo "⏳ 等待数据库就绪..."
|
||||
sleep 10
|
||||
|
||||
# 运行数据库迁移
|
||||
echo "🔄 运行数据库迁移..."
|
||||
docker-compose run --rm backend ./main migrate || echo "迁移完成或已是最新"
|
||||
|
||||
# 启动后端服务
|
||||
echo "🚀 启动后端服务..."
|
||||
docker-compose up -d backend
|
||||
|
||||
# 等待服务启动
|
||||
echo "⏳ 等待服务启动..."
|
||||
sleep 30
|
||||
|
||||
# 健康检查
|
||||
echo "🔍 执行健康检查..."
|
||||
for i in {1..30}; do
|
||||
if curl -f http://localhost:8080/health > /dev/null 2>&1; then
|
||||
echo "✅ 后端服务健康检查通过"
|
||||
break
|
||||
fi
|
||||
echo "等待后端服务启动... ($i/30)"
|
||||
sleep 10
|
||||
done
|
||||
|
||||
# 检查服务状态
|
||||
echo "📊 检查服务状态..."
|
||||
docker-compose ps
|
||||
|
||||
# 清理旧镜像 (保留最近3个)
|
||||
echo "🧹 清理旧镜像..."
|
||||
docker images ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} --format "table {{.Repository}}:{{.Tag}}\t{{.CreatedAt}}" | tail -n +2 | sort -k2 -r | tail -n +4 | awk '{print $1}' | xargs -r docker rmi || true
|
||||
|
||||
# 清理旧备份容器 (保留最近5个)
|
||||
docker images photography_backend_backup_* --format "table {{.Repository}}:{{.Tag}}\t{{.CreatedAt}}" | tail -n +2 | sort -k2 -r | tail -n +6 | awk '{print $1}' | xargs -r docker rmi || true
|
||||
|
||||
echo "🎉 后端部署完成!"
|
||||
|
||||
- name: 📧 发送部署通知
|
||||
if: always()
|
||||
uses: appleboy/telegram-action@master
|
||||
with:
|
||||
to: ${{ secrets.TELEGRAM_TO }}
|
||||
token: ${{ secrets.TELEGRAM_TOKEN }}
|
||||
message: |
|
||||
🔧 摄影作品集后端部署
|
||||
|
||||
📦 项目: ${{ github.repository }}
|
||||
🌿 分支: ${{ github.ref_name }}
|
||||
👤 提交者: ${{ github.actor }}
|
||||
📝 提交信息: ${{ github.event.head_commit.message }}
|
||||
|
||||
${{ job.status == 'success' && '✅ 部署成功' || '❌ 部署失败' }}
|
||||
|
||||
🌐 API: https://api.photography.iriver.top/health
|
||||
📊 监控: https://admin.photography.iriver.top
|
||||
|
||||
rollback:
|
||||
name: 🔄 回滚部署
|
||||
runs-on: ubuntu-latest
|
||||
if: failure() && github.ref == 'refs/heads/main'
|
||||
needs: build-and-deploy
|
||||
|
||||
steps:
|
||||
- name: 🔄 执行回滚
|
||||
uses: appleboy/ssh-action@v1.0.0
|
||||
with:
|
||||
host: ${{ secrets.HOST }}
|
||||
username: ${{ secrets.USERNAME }}
|
||||
key: ${{ secrets.SSH_KEY }}
|
||||
port: ${{ secrets.PORT }}
|
||||
script: |
|
||||
cd /home/gitea/photography
|
||||
|
||||
echo "🔄 开始回滚后端服务..."
|
||||
|
||||
# 查找最新的备份容器
|
||||
BACKUP_IMAGE=$(docker images photography_backend_backup_* --format "table {{.Repository}}:{{.Tag}}\t{{.CreatedAt}}" | tail -n +2 | sort -k2 -r | head -n 1 | awk '{print $1}')
|
||||
|
||||
if [ -n "$BACKUP_IMAGE" ]; then
|
||||
echo "📦 找到备份镜像: $BACKUP_IMAGE"
|
||||
|
||||
# 停止当前服务
|
||||
docker-compose down backend
|
||||
|
||||
# 标记备份镜像为最新
|
||||
docker tag $BACKUP_IMAGE photography_backend:rollback
|
||||
|
||||
# 修改 docker-compose 使用回滚镜像
|
||||
sed -i 's|build: .*|image: photography_backend:rollback|g' docker-compose.yml
|
||||
|
||||
# 启动回滚版本
|
||||
docker-compose up -d backend
|
||||
|
||||
echo "✅ 回滚完成"
|
||||
else
|
||||
echo "❌ 未找到备份镜像,无法回滚"
|
||||
exit 1
|
||||
fi
|
||||
@ -1,78 +1,142 @@
|
||||
name: Deploy Frontend
|
||||
name: 部署前端网站
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ main ]
|
||||
paths: [ 'frontend/**' ]
|
||||
pull_request:
|
||||
branches: [ main ]
|
||||
paths: [ 'frontend/**' ]
|
||||
paths:
|
||||
- 'frontend/**'
|
||||
- '.gitea/workflows/deploy-frontend.yml'
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
deploy:
|
||||
test-and-build:
|
||||
name: 🧪 测试和构建
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Setup Bun
|
||||
uses: oven-sh/setup-bun@v1
|
||||
with:
|
||||
bun-version: latest
|
||||
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
cd frontend
|
||||
bun install
|
||||
|
||||
- name: Run type check
|
||||
run: |
|
||||
cd frontend
|
||||
bun run type-check
|
||||
|
||||
- name: Run lint
|
||||
run: |
|
||||
cd frontend
|
||||
bun run lint
|
||||
|
||||
- name: Build project
|
||||
run: |
|
||||
cd frontend
|
||||
bun run build
|
||||
|
||||
- name: Deploy to VPS
|
||||
run: |
|
||||
# 安装 SSH 客户端、rsync 和 sshpass
|
||||
sudo apt-get update && sudo apt-get install -y openssh-client rsync sshpass
|
||||
|
||||
# 设置 SSH 选项以禁用主机密钥检查(用于密码认证)
|
||||
export SSHPASS=${{ secrets.ALIYUN_PWD }}
|
||||
|
||||
# 测试 SSH 连接
|
||||
sshpass -e ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 ${{ secrets.ALIYUN_USER_NAME }}@${{ secrets.ALIYUN_IP }} "echo 'SSH 连接成功'"
|
||||
|
||||
# 在服务器上创建用户目录下的部署目录
|
||||
sshpass -e ssh -o StrictHostKeyChecking=no ${{ secrets.ALIYUN_USER_NAME }}@${{ secrets.ALIYUN_IP }} "mkdir -p ~/www/photography"
|
||||
|
||||
# 上传构建文件到服务器用户目录(使用密码认证)
|
||||
sshpass -e rsync -avz --delete --progress -e "ssh -o StrictHostKeyChecking=no" frontend/out/ ${{ secrets.ALIYUN_USER_NAME }}@${{ secrets.ALIYUN_IP }}:~/www/photography/
|
||||
|
||||
# 设置文件权限(用户目录无需 sudo)
|
||||
sshpass -e ssh -o StrictHostKeyChecking=no ${{ secrets.ALIYUN_USER_NAME }}@${{ secrets.ALIYUN_IP }} "chmod -R 755 ~/www/photography"
|
||||
|
||||
# 显示部署信息(Caddy 配置需要手动配置指向新路径)
|
||||
sshpass -e ssh -o StrictHostKeyChecking=no ${{ secrets.ALIYUN_USER_NAME }}@${{ secrets.ALIYUN_IP }} "echo '提示:请确保 Web 服务器配置指向 ~/www/photography/ 目录'"
|
||||
|
||||
echo "✅ 部署完成!"
|
||||
echo "📁 部署路径:~/www/photography/"
|
||||
echo "🌐 访问地址:https://photography.iriver.top"
|
||||
|
||||
- name: Notify success
|
||||
if: success()
|
||||
run: |
|
||||
echo "✅ 前端项目部署成功!"
|
||||
|
||||
- name: Notify failure
|
||||
if: failure()
|
||||
run: |
|
||||
echo "❌ 前端项目部署失败!"
|
||||
- name: 📥 检出代码
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: 🦀 设置 Bun 环境
|
||||
uses: oven-sh/setup-bun@v1
|
||||
with:
|
||||
bun-version: latest
|
||||
|
||||
- name: 📦 安装依赖
|
||||
working-directory: ./frontend
|
||||
run: bun install
|
||||
|
||||
- name: 🔍 代码检查
|
||||
working-directory: ./frontend
|
||||
run: |
|
||||
bun run lint
|
||||
bun run type-check
|
||||
|
||||
- name: 🧪 运行测试
|
||||
working-directory: ./frontend
|
||||
run: bun run test
|
||||
|
||||
- name: 🏗️ 构建生产版本
|
||||
working-directory: ./frontend
|
||||
env:
|
||||
NEXT_PUBLIC_API_URL: https://api.photography.iriver.top
|
||||
NEXT_PUBLIC_SITE_URL: https://photography.iriver.top
|
||||
NEXT_PUBLIC_SITE_NAME: 摄影作品集
|
||||
run: bun run build
|
||||
|
||||
- name: 📦 打包构建产物
|
||||
uses: actions/upload-artifact@v3
|
||||
with:
|
||||
name: frontend-dist
|
||||
path: frontend/out/
|
||||
retention-days: 7
|
||||
|
||||
deploy:
|
||||
name: 🚀 部署到生产环境
|
||||
runs-on: ubuntu-latest
|
||||
needs: test-and-build
|
||||
if: github.ref == 'refs/heads/main'
|
||||
|
||||
steps:
|
||||
- name: 📥 检出代码
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: 🦀 设置 Bun 环境
|
||||
uses: oven-sh/setup-bun@v1
|
||||
with:
|
||||
bun-version: latest
|
||||
|
||||
- name: 📦 安装依赖
|
||||
working-directory: ./frontend
|
||||
run: bun install
|
||||
|
||||
- name: 🏗️ 构建生产版本
|
||||
working-directory: ./frontend
|
||||
env:
|
||||
NEXT_PUBLIC_API_URL: https://api.photography.iriver.top
|
||||
NEXT_PUBLIC_SITE_URL: https://photography.iriver.top
|
||||
NEXT_PUBLIC_SITE_NAME: 摄影作品集
|
||||
run: bun run build
|
||||
|
||||
- name: 🚀 部署到服务器
|
||||
run: |
|
||||
# 安装部署工具
|
||||
sudo apt-get update && sudo apt-get install -y openssh-client rsync sshpass
|
||||
|
||||
# 设置 SSH 环境
|
||||
export SSHPASS=${{ secrets.ALIYUN_PWD }}
|
||||
|
||||
echo "🔗 测试 SSH 连接..."
|
||||
sshpass -e ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 ${{ secrets.ALIYUN_USER_NAME }}@${{ secrets.ALIYUN_IP }} "echo 'SSH 连接成功'"
|
||||
|
||||
echo "📁 创建部署目录..."
|
||||
sshpass -e ssh -o StrictHostKeyChecking=no ${{ secrets.ALIYUN_USER_NAME }}@${{ secrets.ALIYUN_IP }} "mkdir -p /home/gitea/www/photography"
|
||||
|
||||
echo "📦 备份当前版本..."
|
||||
sshpass -e ssh -o StrictHostKeyChecking=no ${{ secrets.ALIYUN_USER_NAME }}@${{ secrets.ALIYUN_IP }} "
|
||||
if [ -d '/home/gitea/www/photography' ] && [ \"\$(ls -A /home/gitea/www/photography)\" ]; then
|
||||
mkdir -p /home/gitea/backups/photography-frontend
|
||||
tar -czf /home/gitea/backups/photography-frontend/frontend-\$(date +%Y%m%d-%H%M%S).tar.gz -C /home/gitea/www/photography .
|
||||
echo '✅ 备份完成'
|
||||
fi
|
||||
"
|
||||
|
||||
echo "🚀 部署新版本..."
|
||||
sshpass -e rsync -avz --delete --progress -e "ssh -o StrictHostKeyChecking=no" frontend/out/ ${{ secrets.ALIYUN_USER_NAME }}@${{ secrets.ALIYUN_IP }}:/home/gitea/www/photography/
|
||||
|
||||
echo "🔐 设置文件权限..."
|
||||
sshpass -e ssh -o StrictHostKeyChecking=no ${{ secrets.ALIYUN_USER_NAME }}@${{ secrets.ALIYUN_IP }} "
|
||||
chown -R gitea:gitea /home/gitea/www/photography
|
||||
chmod -R 755 /home/gitea/www/photography
|
||||
find /home/gitea/www/photography -type f -name '*.html' -o -name '*.js' -o -name '*.css' -o -name '*.json' | xargs chmod 644
|
||||
"
|
||||
|
||||
echo "🔄 重新加载 Web 服务器..."
|
||||
sshpass -e ssh -o StrictHostKeyChecking=no ${{ secrets.ALIYUN_USER_NAME }}@${{ secrets.ALIYUN_IP }} "sudo systemctl reload caddy"
|
||||
|
||||
echo "✅ 前端部署完成!"
|
||||
echo "📁 部署路径:/home/gitea/www/photography/"
|
||||
echo "🌐 访问地址:https://photography.iriver.top"
|
||||
|
||||
- name: 🔍 健康检查
|
||||
run: |
|
||||
echo "🔍 执行健康检查..."
|
||||
sleep 10
|
||||
|
||||
# 检查网站是否可访问
|
||||
if curl -f -s -o /dev/null https://photography.iriver.top; then
|
||||
echo "✅ 前端网站访问正常"
|
||||
else
|
||||
echo "⚠️ 前端网站访问异常"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
- name: 📧 发送部署通知
|
||||
if: always()
|
||||
run: |
|
||||
if [ "${{ job.status }}" = "success" ]; then
|
||||
echo "✅ 摄影作品集前端部署成功!"
|
||||
echo "🌐 访问地址: https://photography.iriver.top"
|
||||
else
|
||||
echo "❌ 摄影作品集前端部署失败!"
|
||||
fi
|
||||
Reference in New Issue
Block a user