Compare commits
2 Commits
5dd0bc19e4
...
e46d8f28d1
| Author | SHA1 | Date | |
|---|---|---|---|
| e46d8f28d1 | |||
| 018d86b078 |
@ -55,17 +55,14 @@ on:
|
|||||||
7. **服务器部署** - rsync 同步到服务器
|
7. **服务器部署** - rsync 同步到服务器
|
||||||
8. **权限设置** - 确保文件权限正确
|
8. **权限设置** - 确保文件权限正确
|
||||||
|
|
||||||
### 后端部署流程 (Docker)
|
### 后端部署流程 (Docker) - 简化版
|
||||||
1. **代码检出** - `actions/checkout@v4`
|
1. **代码检出** - `actions/checkout@v4`
|
||||||
2. **Go 环境设置** - `actions/setup-go@v4`
|
2. **Docker 构建** - 构建镜像并推送到镜像仓库
|
||||||
3. **依赖安装** - `go mod download`
|
3. **服务器部署** - 通过 SSH 更新服务器上的 Docker 容器
|
||||||
4. **代码检查** - `go vet` + `go fmt`
|
4. **健康检查** - 检查服务启动状态
|
||||||
5. **单元测试** - `go test` (单元测试,跳过需要数据库的测试)
|
5. **清理操作** - 清理旧镜像和备份容器
|
||||||
6. **构建检查** - `go build`
|
|
||||||
7. **Docker 构建** - 构建镜像并推送到镜像仓库
|
> **注意**: 为了加快部署速度,后端CI/CD已移除代码检查、测试和覆盖率步骤,直接进行Docker构建和部署。
|
||||||
8. **服务器部署** - 通过 SSH 更新服务器上的 Docker 容器
|
|
||||||
9. **健康检查** - 检查服务启动状态
|
|
||||||
10. **清理操作** - 清理旧镜像和备份容器
|
|
||||||
|
|
||||||
### 管理后台部署流程 (Bun)
|
### 管理后台部署流程 (Bun)
|
||||||
1. **代码检出** - `actions/checkout@v4`
|
1. **代码检出** - `actions/checkout@v4`
|
||||||
@ -140,24 +137,16 @@ ssh gitea@server "
|
|||||||
"
|
"
|
||||||
```
|
```
|
||||||
|
|
||||||
#### 后端部署流程 (Docker)
|
#### 后端部署流程 (Docker) - 简化版
|
||||||
```bash
|
```bash
|
||||||
# 1. 环境准备
|
# 1. 环境准备
|
||||||
- Checkout code (actions/checkout@v4)
|
- Checkout code (actions/checkout@v4)
|
||||||
- Setup Go (actions/setup-go@v4)
|
|
||||||
|
|
||||||
# 2. 测试和检查
|
# 2. Docker 构建和推送 (直接跳过测试和检查)
|
||||||
cd backend
|
|
||||||
go mod download
|
|
||||||
go vet ./...
|
|
||||||
go fmt ./...
|
|
||||||
go test -tags=unit ./...
|
|
||||||
|
|
||||||
# 3. Docker 构建和推送
|
|
||||||
docker build -t registry.cn-hangzhou.aliyuncs.com/photography/backend .
|
docker build -t registry.cn-hangzhou.aliyuncs.com/photography/backend .
|
||||||
docker push registry.cn-hangzhou.aliyuncs.com/photography/backend
|
docker push registry.cn-hangzhou.aliyuncs.com/photography/backend
|
||||||
|
|
||||||
# 4. 服务器部署
|
# 3. 服务器部署
|
||||||
ssh gitea@server "
|
ssh gitea@server "
|
||||||
cd /home/gitea/photography/backend
|
cd /home/gitea/photography/backend
|
||||||
docker-compose down backend
|
docker-compose down backend
|
||||||
|
|||||||
@ -14,60 +14,9 @@ env:
|
|||||||
IMAGE_NAME: photography/backend
|
IMAGE_NAME: photography/backend
|
||||||
|
|
||||||
jobs:
|
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:
|
build-and-deploy:
|
||||||
name: 🚀 构建并部署
|
name: 🚀 构建并部署
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
needs: test
|
|
||||||
if: github.ref == 'refs/heads/main'
|
if: github.ref == 'refs/heads/main'
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
|
|||||||
@ -32,6 +32,7 @@ export class ErrorBoundary extends Component<Props, State> {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// 这里可以将错误发送到日志服务
|
// 这里可以将错误发送到日志服务
|
||||||
|
// eslint-disable-next-line no-console
|
||||||
console.error('ErrorBoundary caught an error:', error, errorInfo)
|
console.error('ErrorBoundary caught an error:', error, errorInfo)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -18,14 +18,12 @@ const TestApi: React.FC = () => {
|
|||||||
username: 'admin',
|
username: 'admin',
|
||||||
password: 'admin123'
|
password: 'admin123'
|
||||||
})
|
})
|
||||||
console.log('Login response:', response)
|
|
||||||
|
|
||||||
if (response.code === 0) {
|
if (response.code === 0) {
|
||||||
login(response.data.token, response.data.user)
|
login(response.data.token, response.data.user)
|
||||||
toast.success('登录成功')
|
toast.success('登录成功')
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch {
|
||||||
console.error('Login error:', error)
|
|
||||||
toast.error('登录失败')
|
toast.error('登录失败')
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false)
|
setLoading(false)
|
||||||
@ -37,14 +35,12 @@ const TestApi: React.FC = () => {
|
|||||||
try {
|
try {
|
||||||
setLoading(true)
|
setLoading(true)
|
||||||
const response = await categoryService.getCategories()
|
const response = await categoryService.getCategories()
|
||||||
console.log('Categories response:', response)
|
|
||||||
|
|
||||||
if (response.code === 200) {
|
if (response.code === 200) {
|
||||||
setCategories(response.data.categories || [])
|
setCategories(response.data.categories || [])
|
||||||
toast.success('获取分类成功')
|
toast.success('获取分类成功')
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch {
|
||||||
console.error('Fetch categories error:', error)
|
|
||||||
toast.error('获取分类失败')
|
toast.error('获取分类失败')
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false)
|
setLoading(false)
|
||||||
@ -61,15 +57,13 @@ const TestApi: React.FC = () => {
|
|||||||
try {
|
try {
|
||||||
setLoading(true)
|
setLoading(true)
|
||||||
const response = await categoryService.createCategory(newCategory)
|
const response = await categoryService.createCategory(newCategory)
|
||||||
console.log('Create category response:', response)
|
|
||||||
|
|
||||||
if (response.code === 200) {
|
if (response.code === 200) {
|
||||||
toast.success('创建分类成功')
|
toast.success('创建分类成功')
|
||||||
setNewCategory({ name: '', description: '' })
|
setNewCategory({ name: '', description: '' })
|
||||||
fetchCategories() // 重新获取分类列表
|
fetchCategories() // 重新获取分类列表
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch {
|
||||||
console.error('Create category error:', error)
|
|
||||||
toast.error('创建分类失败')
|
toast.error('创建分类失败')
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false)
|
setLoading(false)
|
||||||
|
|||||||
@ -97,29 +97,49 @@ func (tc *TestContext) PostJSON(path string, data interface{}) ([]byte, error) {
|
|||||||
return nil, err
|
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")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
if tc.authToken != "" {
|
if tc.authToken != "" {
|
||||||
req.Header.Set("Authorization", "Bearer "+tc.authToken)
|
req.Header.Set("Authorization", "Bearer "+tc.authToken)
|
||||||
}
|
}
|
||||||
|
|
||||||
w := httptest.NewRecorder()
|
client := &http.Client{Timeout: 10 * time.Second}
|
||||||
tc.server.ServeHTTP(w, req)
|
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 请求
|
// GetJSON 发送 GET JSON 请求
|
||||||
func (tc *TestContext) GetJSON(path string) ([]byte, error) {
|
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 != "" {
|
if tc.authToken != "" {
|
||||||
req.Header.Set("Authorization", "Bearer "+tc.authToken)
|
req.Header.Set("Authorization", "Bearer "+tc.authToken)
|
||||||
}
|
}
|
||||||
|
|
||||||
w := httptest.NewRecorder()
|
client := &http.Client{Timeout: 10 * time.Second}
|
||||||
tc.server.ServeHTTP(w, req)
|
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 请求
|
// PutJSON 发送 PUT JSON 请求
|
||||||
@ -129,29 +149,95 @@ func (tc *TestContext) PutJSON(path string, data interface{}) ([]byte, error) {
|
|||||||
return nil, err
|
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")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
if tc.authToken != "" {
|
if tc.authToken != "" {
|
||||||
req.Header.Set("Authorization", "Bearer "+tc.authToken)
|
req.Header.Set("Authorization", "Bearer "+tc.authToken)
|
||||||
}
|
}
|
||||||
|
|
||||||
w := httptest.NewRecorder()
|
client := &http.Client{Timeout: 10 * time.Second}
|
||||||
tc.server.ServeHTTP(w, req)
|
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 请求
|
// DeleteJSON 发送 DELETE 请求
|
||||||
func (tc *TestContext) DeleteJSON(path string) ([]byte, error) {
|
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 != "" {
|
if tc.authToken != "" {
|
||||||
req.Header.Set("Authorization", "Bearer "+tc.authToken)
|
req.Header.Set("Authorization", "Bearer "+tc.authToken)
|
||||||
}
|
}
|
||||||
|
|
||||||
w := httptest.NewRecorder()
|
client := &http.Client{Timeout: 10 * time.Second}
|
||||||
tc.server.ServeHTTP(w, req)
|
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 健康检查接口测试
|
// TestHealthCheck 健康检查接口测试
|
||||||
@ -373,14 +459,8 @@ func TestPhotoUpload(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// 发送请求
|
// 发送请求
|
||||||
req := httptest.NewRequest(http.MethodPost, "/api/v1/photos", &buf)
|
respBody, err := tc.PostMultipart("/api/v1/photos", &buf, writer.FormDataContentType())
|
||||||
req.Header.Set("Content-Type", writer.FormDataContentType())
|
require.NoError(t, err)
|
||||||
req.Header.Set("Authorization", "Bearer "+tc.authToken)
|
|
||||||
|
|
||||||
w := httptest.NewRecorder()
|
|
||||||
tc.server.ServeHTTP(w, req)
|
|
||||||
|
|
||||||
respBody := w.Body.Bytes()
|
|
||||||
|
|
||||||
var resp types.UploadPhotoResponse
|
var resp types.UploadPhotoResponse
|
||||||
err = json.Unmarshal(respBody, &resp)
|
err = json.Unmarshal(respBody, &resp)
|
||||||
|
|||||||
Reference in New Issue
Block a user