Skip to content

Commit e06ef30

Browse files
committed
ci: 优化后端测试流程,通过 Docker Compose 管理测试数据库并使部分环境变量可选。
1 parent eb4919a commit e06ef30

4 files changed

Lines changed: 24 additions & 29 deletions

File tree

.github/workflows/ci.yml

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,6 @@ jobs:
3636
backend-test:
3737
name: Backend Tests
3838
runs-on: ubuntu-latest
39-
services:
40-
postgres:
41-
image: postgres:alpine
42-
env:
43-
POSTGRES_DB: nexquery_test
44-
POSTGRES_USER: adonis
45-
POSTGRES_PASSWORD: password
46-
ports:
47-
- 5432:5432
48-
# Set health checks to wait until postgres has started
49-
options: >-
50-
--health-cmd pg_isready
51-
--health-interval 10s
52-
--health-timeout 5s
53-
--health-retries 5
54-
5539
steps:
5640
- uses: actions/checkout@v4
5741

@@ -69,11 +53,14 @@ jobs:
6953
- name: Install dependencies
7054
run: pnpm install --frozen-lockfile
7155

56+
- name: Start Test Database
57+
run: docker compose -f docker-compose.test.yml up -d --wait
58+
7259
- name: Run Backend Tests
7360
run: pnpm backend:test
7461
env:
7562
DB_HOST: localhost
76-
DB_PORT: 5432
63+
DB_PORT: 5433
7764
DB_USER: adonis
7865
DB_PASSWORD: password
7966
DB_DATABASE: nexquery_test

backend/start/env.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ import { Env } from '@adonisjs/core/env'
1414
// eslint-disable-next-line antfu/no-top-level-await
1515
export default await Env.create(new URL('../', import.meta.url), {
1616
NODE_ENV: Env.schema.enum(['development', 'production', 'test'] as const),
17-
PORT: Env.schema.number(),
17+
PORT: Env.schema.number.optional(),
1818
APP_KEY: Env.schema.string(),
19-
HOST: Env.schema.string({ format: 'host' }),
20-
LOG_LEVEL: Env.schema.enum(['fatal', 'error', 'warn', 'info', 'debug', 'trace', 'silent']),
19+
HOST: Env.schema.string.optional({ format: 'host' }),
20+
LOG_LEVEL: Env.schema.enum.optional(['fatal', 'error', 'warn', 'info', 'debug', 'trace', 'silent']),
2121

2222
/*
2323
|----------------------------------------------------------
@@ -35,12 +35,12 @@ export default await Env.create(new URL('../', import.meta.url), {
3535
| Variables for configuring mail connection
3636
|----------------------------------------------------------
3737
*/
38-
SMTP_HOST: Env.schema.string(),
39-
SMTP_PORT: Env.schema.number(),
38+
SMTP_HOST: Env.schema.string.optional(),
39+
SMTP_PORT: Env.schema.number.optional(),
4040
SMTP_USERNAME: Env.schema.string.optional(),
4141
SMTP_PASSWORD: Env.schema.string.optional(),
4242

43-
FRONTEND_URL: Env.schema.string(),
43+
FRONTEND_URL: Env.schema.string.optional(),
4444

4545
/*
4646
|----------------------------------------------------------
@@ -65,8 +65,8 @@ export default await Env.create(new URL('../', import.meta.url), {
6565
| Variables for Vector Database (Qdrant)
6666
|----------------------------------------------------------
6767
*/
68-
QDRANT_HOST: Env.schema.string({ format: 'host' }),
69-
QDRANT_PORT: Env.schema.number(),
68+
QDRANT_HOST: Env.schema.string.optional({ format: 'host' }),
69+
QDRANT_PORT: Env.schema.number.optional(),
7070
QDRANT_API_KEY: Env.schema.string.optional(),
7171

7272
/*
@@ -92,9 +92,9 @@ export default await Env.create(new URL('../', import.meta.url), {
9292
| Variables for configuring the limiter package
9393
|----------------------------------------------------------
9494
*/
95-
LIMITER_STORE: Env.schema.enum(['database', 'memory'] as const),
95+
LIMITER_STORE: Env.schema.enum.optional(['database', 'memory'] as const),
9696

97-
REDIS_HOST: Env.schema.string({ format: 'host' }),
98-
REDIS_PORT: Env.schema.number(),
97+
REDIS_HOST: Env.schema.string.optional({ format: 'host' }),
98+
REDIS_PORT: Env.schema.number.optional(),
9999
REDIS_PASSWORD: Env.schema.string.optional(),
100100
})

docker-compose.test.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
name: nexquery-ai-test
2+
13
services:
24
postgres:
35
image: postgres:alpine
@@ -6,4 +8,9 @@ services:
68
POSTGRES_USER: adonis
79
POSTGRES_PASSWORD: password
810
ports:
9-
- '5433:5432'
11+
- "5433:5432"
12+
healthcheck:
13+
test: ["CMD-SHELL", "pg_isready -U adonis -d nexquery_test"]
14+
interval: 5s
15+
timeout: 5s
16+
retries: 5

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"backend:dev": "pnpm --filter backend dev",
1919
"backend:build": "pnpm --filter backend build",
2020
"backend:test": "pnpm --filter backend test",
21+
"backend:test:docker": "docker compose -f docker-compose.test.yml up -d --wait && DB_PORT=5433 DB_HOST=127.0.0.1 DB_USER=adonis DB_PASSWORD=password DB_DATABASE=nexquery_test pnpm backend:test && docker compose -f docker-compose.test.yml down -v",
2122
"backend:migrate": "pnpm --filter backend exec node ace migration:run",
2223
"backend:seed": "pnpm --filter backend exec node ace db:seed",
2324
"backend:fresh": "pnpm --filter backend exec node ace migration:fresh --seed",

0 commit comments

Comments
 (0)