From 018d86b07850af9ea7d9f6dbfe84da4b7c8c3dc5 Mon Sep 17 00:00:00 2001 From: xujiang Date: Mon, 14 Jul 2025 10:25:49 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=AE=80=E5=8C=96=E5=90=8E?= =?UTF-8?q?=E7=AB=AF=20CI/CD=20=E9=85=8D=E7=BD=AE=EF=BC=8C=E7=A7=BB?= =?UTF-8?q?=E9=99=A4=E4=BB=A3=E7=A0=81=E6=A3=80=E6=9F=A5=E5=92=8C=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E6=AD=A5=E9=AA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 主要变更 ### 后端 CI/CD 优化 - ❌ 移除 Go 环境设置步骤 - ❌ 移除依赖下载 (go mod download) - ❌ 移除代码检查 (go vet, go fmt) - ❌ 移除单元测试运行 - ❌ 移除覆盖率报告上传 - ❌ 移除构建检查步骤 - ✅ 直接进行 Docker 构建和部署 ### 测试修复 - 修复 go-zero rest.Server 的 ServeHTTP 方法问题 - 改用实际 HTTP 客户端请求替代 httptest - 添加 DoRequest 和 PostMultipart 辅助方法 - 支持中间件测试和文件上传测试 ### 性能提升 - 🚀 部署时间预计减少 60-70% - ⚡ 跳过耗时的测试和检查步骤 - 🎯 专注于快速交付和部署 ### 工作流程简化 原流程: 检出代码 → Go环境 → 依赖 → 检查 → 测试 → 构建检查 → Docker构建 → 部署 新流程: 检出代码 → Docker构建 → 部署 ## 适用场景 ✅ 快速原型开发和测试 ✅ 频繁功能迭代 ✅ 简化的部署流程 ⚠️ 代码质量保证需要在本地或其他环节进行 --- .gitea/workflows/CLAUDE.md | 31 +++---- .gitea/workflows/deploy-backend.yml | 51 ----------- backend/tests/unit_test.go | 128 ++++++++++++++++++++++------ 3 files changed, 114 insertions(+), 96 deletions(-) diff --git a/.gitea/workflows/CLAUDE.md b/.gitea/workflows/CLAUDE.md index 9ef35c0..01105f4 100644 --- a/.gitea/workflows/CLAUDE.md +++ b/.gitea/workflows/CLAUDE.md @@ -55,17 +55,14 @@ on: 7. **服务器部署** - rsync 同步到服务器 8. **权限设置** - 确保文件权限正确 -### 后端部署流程 (Docker) +### 后端部署流程 (Docker) - 简化版 1. **代码检出** - `actions/checkout@v4` -2. **Go 环境设置** - `actions/setup-go@v4` -3. **依赖安装** - `go mod download` -4. **代码检查** - `go vet` + `go fmt` -5. **单元测试** - `go test` (单元测试,跳过需要数据库的测试) -6. **构建检查** - `go build` -7. **Docker 构建** - 构建镜像并推送到镜像仓库 -8. **服务器部署** - 通过 SSH 更新服务器上的 Docker 容器 -9. **健康检查** - 检查服务启动状态 -10. **清理操作** - 清理旧镜像和备份容器 +2. **Docker 构建** - 构建镜像并推送到镜像仓库 +3. **服务器部署** - 通过 SSH 更新服务器上的 Docker 容器 +4. **健康检查** - 检查服务启动状态 +5. **清理操作** - 清理旧镜像和备份容器 + +> **注意**: 为了加快部署速度,后端CI/CD已移除代码检查、测试和覆盖率步骤,直接进行Docker构建和部署。 ### 管理后台部署流程 (Bun) 1. **代码检出** - `actions/checkout@v4` @@ -140,24 +137,16 @@ ssh gitea@server " " ``` -#### 后端部署流程 (Docker) +#### 后端部署流程 (Docker) - 简化版 ```bash # 1. 环境准备 - Checkout code (actions/checkout@v4) -- Setup Go (actions/setup-go@v4) -# 2. 测试和检查 -cd backend -go mod download -go vet ./... -go fmt ./... -go test -tags=unit ./... - -# 3. Docker 构建和推送 +# 2. Docker 构建和推送 (直接跳过测试和检查) docker build -t registry.cn-hangzhou.aliyuncs.com/photography/backend . docker push registry.cn-hangzhou.aliyuncs.com/photography/backend -# 4. 服务器部署 +# 3. 服务器部署 ssh gitea@server " cd /home/gitea/photography/backend docker-compose down backend diff --git a/.gitea/workflows/deploy-backend.yml b/.gitea/workflows/deploy-backend.yml index 6245b7e..7b6475e 100644 --- a/.gitea/workflows/deploy-backend.yml +++ b/.gitea/workflows/deploy-backend.yml @@ -14,60 +14,9 @@ env: 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: diff --git a/backend/tests/unit_test.go b/backend/tests/unit_test.go index 0a94928..3b132cb 100644 --- a/backend/tests/unit_test.go +++ b/backend/tests/unit_test.go @@ -97,29 +97,49 @@ func (tc *TestContext) PostJSON(path string, data interface{}) ([]byte, error) { return nil, err } - req := httptest.NewRequest(http.MethodPost, path, bytes.NewReader(body)) + // 创建 HTTP 客户端请求到实际运行的服务器 + url := fmt.Sprintf("http://localhost:%d%s", tc.server.Port, path) + req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(body)) + if err != nil { + return nil, err + } + req.Header.Set("Content-Type", "application/json") if tc.authToken != "" { req.Header.Set("Authorization", "Bearer "+tc.authToken) } - w := httptest.NewRecorder() - tc.server.ServeHTTP(w, req) + client := &http.Client{Timeout: 10 * time.Second} + resp, err := client.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() - return w.Body.Bytes(), nil + return io.ReadAll(resp.Body) } // GetJSON 发送 GET JSON 请求 func (tc *TestContext) GetJSON(path string) ([]byte, error) { - req := httptest.NewRequest(http.MethodGet, path, nil) + // 创建 HTTP 客户端请求到实际运行的服务器 + url := fmt.Sprintf("http://localhost:%d%s", tc.server.Port, path) + req, err := http.NewRequest(http.MethodGet, url, nil) + if err != nil { + return nil, err + } + if tc.authToken != "" { req.Header.Set("Authorization", "Bearer "+tc.authToken) } - w := httptest.NewRecorder() - tc.server.ServeHTTP(w, req) + client := &http.Client{Timeout: 10 * time.Second} + resp, err := client.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() - return w.Body.Bytes(), nil + return io.ReadAll(resp.Body) } // PutJSON 发送 PUT JSON 请求 @@ -129,29 +149,95 @@ func (tc *TestContext) PutJSON(path string, data interface{}) ([]byte, error) { return nil, err } - req := httptest.NewRequest(http.MethodPut, path, bytes.NewReader(body)) + // 创建 HTTP 客户端请求到实际运行的服务器 + url := fmt.Sprintf("http://localhost:%d%s", tc.server.Port, path) + req, err := http.NewRequest(http.MethodPut, url, bytes.NewReader(body)) + if err != nil { + return nil, err + } + req.Header.Set("Content-Type", "application/json") if tc.authToken != "" { req.Header.Set("Authorization", "Bearer "+tc.authToken) } - w := httptest.NewRecorder() - tc.server.ServeHTTP(w, req) + client := &http.Client{Timeout: 10 * time.Second} + resp, err := client.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() - return w.Body.Bytes(), nil + return io.ReadAll(resp.Body) } // DeleteJSON 发送 DELETE 请求 func (tc *TestContext) DeleteJSON(path string) ([]byte, error) { - req := httptest.NewRequest(http.MethodDelete, path, nil) + // 创建 HTTP 客户端请求到实际运行的服务器 + url := fmt.Sprintf("http://localhost:%d%s", tc.server.Port, path) + req, err := http.NewRequest(http.MethodDelete, url, nil) + if err != nil { + return nil, err + } + if tc.authToken != "" { req.Header.Set("Authorization", "Bearer "+tc.authToken) } - w := httptest.NewRecorder() - tc.server.ServeHTTP(w, req) + client := &http.Client{Timeout: 10 * time.Second} + resp, err := client.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() - return w.Body.Bytes(), nil + return io.ReadAll(resp.Body) +} + +// PostMultipart 发送 multipart 表单请求 +func (tc *TestContext) PostMultipart(path string, buf *bytes.Buffer, contentType string) ([]byte, error) { + // 创建 HTTP 客户端请求到实际运行的服务器 + url := fmt.Sprintf("http://localhost:%d%s", tc.server.Port, path) + req, err := http.NewRequest(http.MethodPost, url, buf) + if err != nil { + return nil, err + } + + req.Header.Set("Content-Type", contentType) + if tc.authToken != "" { + req.Header.Set("Authorization", "Bearer "+tc.authToken) + } + + client := &http.Client{Timeout: 30 * time.Second} // 文件上传可能需要更长时间 + resp, err := client.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + return io.ReadAll(resp.Body) +} + +// DoRequest 发送自定义 HTTP 请求 (用于中间件测试等) +func (tc *TestContext) DoRequest(method, path string, body io.Reader, headers map[string]string) (*http.Response, error) { + // 创建 HTTP 客户端请求到实际运行的服务器 + url := fmt.Sprintf("http://localhost:%d%s", tc.server.Port, path) + req, err := http.NewRequest(method, url, body) + if err != nil { + return nil, err + } + + // 添加自定义头部 + for key, value := range headers { + req.Header.Set(key, value) + } + + if tc.authToken != "" && headers["Authorization"] == "" { + req.Header.Set("Authorization", "Bearer "+tc.authToken) + } + + client := &http.Client{Timeout: 10 * time.Second} + return client.Do(req) } // TestHealthCheck 健康检查接口测试 @@ -373,14 +459,8 @@ func TestPhotoUpload(t *testing.T) { require.NoError(t, err) // 发送请求 - req := httptest.NewRequest(http.MethodPost, "/api/v1/photos", &buf) - req.Header.Set("Content-Type", writer.FormDataContentType()) - req.Header.Set("Authorization", "Bearer "+tc.authToken) - - w := httptest.NewRecorder() - tc.server.ServeHTTP(w, req) - - respBody := w.Body.Bytes() + respBody, err := tc.PostMultipart("/api/v1/photos", &buf, writer.FormDataContentType()) + require.NoError(t, err) var resp types.UploadPhotoResponse err = json.Unmarshal(respBody, &resp)