fix: CI动态端口+超时控制,避免e2e卡死
All checks were successful
Test / unit-test (push) Successful in 6s
Test / build-check (push) Successful in 4s
Test / e2e-test (push) Successful in 51s

- 后端端口随机9000-9999,前端4000-4999
- 数据库文件按端口号隔离
- vite proxy支持VITE_API_PORT环境变量
- 服务启动超时30秒,失败即退出
- Cypress: defaultCommandTimeout=5s, pageLoadTimeout=10s
- 整个e2e job timeout 5分钟

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-11 11:08:19 +00:00
parent 83078f8f8b
commit d458bd05cd
2 changed files with 32 additions and 15 deletions

View File

@@ -12,6 +12,7 @@ jobs:
e2e-test:
runs-on: test
needs: unit-test
timeout-minutes: 5
steps:
- uses: actions/checkout@v4
@@ -23,24 +24,41 @@ jobs:
- name: E2E tests
run: |
# Dynamic ports to avoid conflicts
BE_PORT=$(shuf -i 9000-9999 -n 1)
FE_PORT=$(shuf -i 4000-4999 -n 1)
DB_FILE="/tmp/ci_oil_test_${BE_PORT}.db"
echo "Using backend=$BE_PORT frontend=$FE_PORT db=$DB_FILE"
# Start backend
DB_PATH=/tmp/ci_oil_test.db FRONTEND_DIR=/dev/null \
/tmp/ci-venv/bin/uvicorn backend.main:app --port 8000 &
DB_PATH="$DB_FILE" FRONTEND_DIR=/dev/null \
/tmp/ci-venv/bin/uvicorn backend.main:app --port $BE_PORT &
BE_PID=$!
# Start frontend (in subshell to not change cwd)
(cd frontend && npx vite --port 5173) &
# Start frontend with proxy to dynamic backend port
(cd frontend && VITE_API_PORT=$BE_PORT npx vite --port $FE_PORT) &
FE_PID=$!
# Wait for both servers
# Wait for both servers (max 30s, fail fast)
READY=0
for i in $(seq 1 30); do
if curl -sf http://localhost:8000/api/version > /dev/null 2>&1 && \
curl -sf http://localhost:5173/ > /dev/null 2>&1; then
echo "Both servers ready"
if curl -sf http://localhost:$BE_PORT/api/oils > /dev/null 2>&1 && \
curl -sf http://localhost:$FE_PORT/ > /dev/null 2>&1; then
echo "Both servers ready in ${i}s"
READY=1
break
fi
sleep 1
done
# Run core cypress specs (proven stable)
if [ "$READY" = "0" ]; then
echo "ERROR: Servers failed to start within 30s"
kill $BE_PID $FE_PID 2>/dev/null
rm -f "$DB_FILE"
exit 1
fi
# Run core cypress specs with timeouts
cd frontend
npx cypress run --spec "\
cypress/e2e/recipe-detail.cy.js,\
@@ -50,13 +68,12 @@ jobs:
cypress/e2e/category-modules.cy.js,\
cypress/e2e/notification-flow.cy.js,\
cypress/e2e/registration-flow.cy.js\
" --config video=false
" --config "video=false,defaultCommandTimeout=5000,pageLoadTimeout=10000,baseUrl=http://localhost:$FE_PORT"
EXIT_CODE=$?
# Cleanup
pkill -f "uvicorn backend" || true
pkill -f "node.*vite" || true
rm -f /tmp/ci_oil_test.db
kill $BE_PID $FE_PID 2>/dev/null
rm -f "$DB_FILE"
exit $EXIT_CODE
build-check:

View File

@@ -10,8 +10,8 @@ export default defineConfig({
plugins: [vue()],
server: {
proxy: {
'/api': 'http://localhost:8000',
'/uploads': 'http://localhost:8000'
'/api': `http://localhost:${process.env.VITE_API_PORT || 8000}`,
'/uploads': `http://localhost:${process.env.VITE_API_PORT || 8000}`
}
},
build: {