diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..b59e8c23 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,8 @@ +.git +.playwright-cli +dist +node_modules +node_modules.broken-by-path +.env.local +npm-debug.log* +pnpm-debug.log* diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..73f2bdaa --- /dev/null +++ b/Dockerfile @@ -0,0 +1,22 @@ +# 构建 Vue 静态文件 +FROM node:22-alpine AS build + +WORKDIR /app + +# Corepack 随 Node 一起提供,用它固定使用项目的 pnpm。 +RUN corepack enable + +COPY package.json pnpm-lock.yaml ./ +COPY shared/package.json ./shared/package.json +RUN pnpm install --frozen-lockfile + +COPY . . +RUN pnpm test && pnpm build + +# 运行阶段:仅保留构建产物和 Nginx,不包含源码或 node_modules。 +FROM nginx:1.27-alpine + +COPY nginx/default.conf.template /etc/nginx/templates/default.conf.template +COPY --from=build /app/dist /usr/share/nginx/html + +EXPOSE 80 diff --git a/Jenkinsfile b/Jenkinsfile index 82aa07b9..64a735c5 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,13 +1,43 @@ pipeline { agent any + options { + timestamps() + disableConcurrentBuilds() + } + stages { - stage('拉取 release/test 代码') { + stage('确认提交版本') { steps { - checkout scm sh 'echo "Jenkins 已拉取 release/test 分支代码"' sh 'git log -1 --oneline' } } + + stage('构建并部署测试前端') { + steps { + sh ''' + set -eu + docker compose -f docker-compose.test.yml build --pull=false + docker compose -f docker-compose.test.yml up -d --remove-orphans + ''' + } + } + + stage('部署检查') { + steps { + sh ''' + set -eu + docker compose -f docker-compose.test.yml ps + docker compose -f docker-compose.test.yml exec -T admin-web wget -qO- http://127.0.0.1/healthz + ''' + } + } } -} \ No newline at end of file + + post { + failure { + sh 'docker compose -f docker-compose.test.yml logs --tail=100 || true' + } + } +} diff --git a/docker-compose.test.yml b/docker-compose.test.yml new file mode 100644 index 00000000..a1554047 --- /dev/null +++ b/docker-compose.test.yml @@ -0,0 +1,13 @@ +services: + admin-web: + build: + context: . + dockerfile: Dockerfile + image: admin-web-test:local + container_name: admin-web-test + restart: unless-stopped + ports: + - "${HOST_PORT:-18080}:80" + environment: + # 当前测试后端 Gateway;以后迁移环境时只需要改这里或 Jenkins 环境变量。 + API_UPSTREAM: "${API_UPSTREAM:-http://8.163.30.128:9000}" diff --git a/nginx/default.conf.template b/nginx/default.conf.template new file mode 100644 index 00000000..2ae3e079 --- /dev/null +++ b/nginx/default.conf.template @@ -0,0 +1,29 @@ +server { + listen 80; + listen [::]:80; + server_name _; + + root /usr/share/nginx/html; + index index.html; + + # Docker 容器运行正常的检查地址,供 Jenkins 使用。 + location = /healthz { + default_type text/plain; + return 200 "ok\n"; + } + + # 前端代码中的 API 前缀为 /api;保持原始路径转给 Gateway。 + location /api/ { + proxy_pass ${API_UPSTREAM}; + proxy_http_version 1.1; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + } + + # Vue Router 的 history 路由刷新时仍返回 index.html。 + location / { + try_files $uri $uri/ /index.html; + } +}