From 3ec80f82da2285afee5336c74deb96346408229d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=BF=94?= Date: Wed, 6 May 2026 16:57:26 +0800 Subject: [PATCH] =?UTF-8?q?ci(e2e):=20=E5=A2=9E=E5=BC=BA=20E2E=20=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=20stage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 启动独立 PostgreSQL 容器 + 后端服务容器作为测试环境; 等待数据库和后端就绪后再执行 Playwright 测试; E2E 测试支持重试机制(RETRY_COUNT 次); post.always 中自动清理测试容器。 --- Jenkinsfile | 68 +++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 64 insertions(+), 4 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 31ff415..f8914b6 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -181,20 +181,81 @@ pipeline { stage('E2E测试') { steps { echo '🎭 执行E2E测试...' + sh ''' + # 启动测试数据库 + docker run -d --name e2e-postgres-${BUILD_NUMBER} \ + -e POSTGRES_DB=${DB_NAME} \ + -e POSTGRES_USER=${DB_USER} \ + -e POSTGRES_PASSWORD=${DB_PASSWORD} \ + -p 5433:5432 \ + postgres:16-alpine + + # 等待数据库就绪 + for i in $(seq 1 30); do + if docker exec e2e-postgres-${BUILD_NUMBER} pg_isready -U ${DB_USER} -d ${DB_NAME} > /dev/null 2>&1; then + echo "数据库已就绪" + break + fi + echo "等待数据库启动... ($i/30)" + sleep 2 + done + + # 启动后端服务 + docker run -d --name e2e-backend-${BUILD_NUMBER} \ + --link e2e-postgres-${BUILD_NUMBER}:postgres \ + -e SPRING_R2DBC_URL=r2dbc:postgresql://postgres:5432/${DB_NAME} \ + -e SPRING_R2DBC_USERNAME=${DB_USER} \ + -e SPRING_R2DBC_PASSWORD=${DB_PASSWORD} \ + -e SPRING_FLYWAY_URL=jdbc:postgresql://postgres:5432/${DB_NAME} \ + -e SPRING_FLYWAY_USER=${DB_USER} \ + -e SPRING_FLYWAY_PASSWORD=${DB_PASSWORD} \ + -p 8081:8080 \ + ${DOCKER_REGISTRY}/${DOCKER_IMAGE_BACKEND}:latest || true + + # 等待后端就绪 + for i in $(seq 1 60); do + if curl -sf http://localhost:8081/actuator/health > /dev/null 2>&1; then + echo "后端服务已就绪" + break + fi + echo "等待后端启动... ($i/60)" + sleep 3 + done + ''' + dir(FRONTEND_DIR) { sh ''' # 安装Playwright浏览器 pnpm exec playwright install --with-deps chromium - # 执行E2E测试 - pnpm run test:e2e:journeys + # 执行E2E测试(带重试) + RETRY=0 + MAX_RETRY=${RETRY_COUNT} + until [ $RETRY -ge $MAX_RETRY ]; do + pnpm run test:e2e:journeys && break + RETRY=$((RETRY+1)) + echo "E2E测试第${RETRY}次重试..." + sleep 10 + done + + if [ $RETRY -ge $MAX_RETRY ]; then + echo "E2E测试在${MAX_RETRY}次重试后仍然失败" + exit 1 + fi ''' } } post { always { + sh ''' + # 清理E2E测试容器 + docker stop e2e-backend-${BUILD_NUMBER} 2>/dev/null || true + docker rm e2e-backend-${BUILD_NUMBER} 2>/dev/null || true + docker stop e2e-postgres-${BUILD_NUMBER} 2>/dev/null || true + docker rm e2e-postgres-${BUILD_NUMBER} 2>/dev/null || true + ''' + dir(FRONTEND_DIR) { - // 发布E2E测试报告 publishHTML(target: [ allowMissing: false, alwaysLinkToLastBuild: true, @@ -204,7 +265,6 @@ pipeline { reportName: 'E2E测试报告' ]) - // 归档测试失败截图和视频 archiveArtifacts artifacts: 'test-results/**/*.png, test-results/**/*.webm', allowEmptyArchive: true } }