Files
photography/.gitea/workflows/deploy-backend.yml
xujiang 0651b6626a fix: 优化后端部署配置,适配现有服务架构
主要修改:
- 移除 postgres:15-alpine 和 redis 容器依赖
- 优化 docker-compose.yml 配置,使用 host 网络模式
- 移除 CI/CD 中的自动数据库迁移,改为手动执行
- 更新环境变量配置,连接到现有的 PostgreSQL 和 Redis 服务
- 完善部署文档,增加现有服务集成说明

配置优化:
- 修正 docker-compose.yml 位置到 backend 目录
- 简化 CI/CD 测试流程,跳过需要数据库的测试
- 增加数据库迁移安全策略说明
- 完善部署流程文档和故障排除指南
2025-07-09 16:42:22 +08:00

241 lines
8.0 KiB
YAML

name: 部署后端服务
on:
push:
branches: [ main ]
paths:
- 'backend/**'
- '.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
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:
JWT_SECRET: test_jwt_secret_for_ci_cd_testing_only
run: |
# 运行单元测试 (跳过需要数据库的测试)
go test -v -race -coverprofile=coverage.out -tags=unit ./...
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
# 切换到后端目录
cd backend
# 备份当前运行的容器 (如果存在)
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 "⚠️ 数据库迁移需要手动执行,请在部署后运行:"
echo " docker-compose exec backend ./main migrate"
# 启动后端服务
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 "🎉 后端部署完成!"
echo "📋 请记住手动运行数据库迁移:"
echo " docker-compose exec backend ./main migrate"
- 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' && '✅ 部署成功' || '❌ 部署失败' }}
${{ 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/backend
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