From a6afbcffebbdcec938b632cbbb86f91cda9669b8 Mon Sep 17 00:00:00 2001 From: SkyJourney Date: Wed, 8 Apr 2026 17:36:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20CI/CD=20=E6=9E=84=E5=BB=BA?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E4=B8=8E=E9=83=A8=E7=BD=B2=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - deploy/Dockerfile:多阶段构建,node:24-alpine(阿里镜像)构建 dist,nginx:alpine 作为成品镜像;构建上下文为仓库根目录 - deploy/nginx.conf:Vue Router history 模式 SPA 配置,含 gzip 压缩和 /assets/ 长期缓存 - deploy/compose.yaml:以 zhican-admin:latest 启动服务,对外暴露 12801 端口 - .teamcity/pipeline.yaml:main 分支触发,build/deploy 独立 step Co-Authored-By: Claude Sonnet 4.6 --- .teamcity/pipeline.yaml | 25 ++++++++++++++++++++++++ deploy/Dockerfile | 42 +++++++++++++++++++++++++++++++++++++++++ deploy/compose.yaml | 6 ++++++ deploy/nginx.conf | 34 +++++++++++++++++++++++++++++++++ 4 files changed, 107 insertions(+) create mode 100644 .teamcity/pipeline.yaml create mode 100644 deploy/Dockerfile create mode 100644 deploy/compose.yaml create mode 100644 deploy/nginx.conf diff --git a/.teamcity/pipeline.yaml b/.teamcity/pipeline.yaml new file mode 100644 index 0000000..388131b --- /dev/null +++ b/.teamcity/pipeline.yaml @@ -0,0 +1,25 @@ +name: zhican-admin + +on: + push: + branches: + - main + +jobs: + build: + name: 构建 Docker 镜像 + steps: + - name: Docker Build + script: | + docker build \ + -f deploy/Dockerfile \ + -t zhican-admin:latest \ + . + + deploy: + name: 启动服务 + needs: build + steps: + - name: Compose Up + script: | + docker compose -f deploy/compose.yaml up -d diff --git a/deploy/Dockerfile b/deploy/Dockerfile new file mode 100644 index 0000000..a9ec822 --- /dev/null +++ b/deploy/Dockerfile @@ -0,0 +1,42 @@ +# 构建上下文为仓库根目录 +# docker build -f deploy/Dockerfile -t zhican-admin:latest . + +# ──────────────────────────────────────────── +# Stage 1: 构建阶段 +# ──────────────────────────────────────────── +FROM node:24-alpine AS builder + +WORKDIR /app + +# 配置阿里 npm 镜像,加速依赖安装 +RUN npm config set registry https://registry.npmmirror.com + +# 安装 pnpm 并配置镜像 +RUN npm install -g pnpm && pnpm config set registry https://registry.npmmirror.com + +# 优先复制依赖描述文件,利用 Docker 层缓存 +COPY zhican/admin/package.json zhican/admin/pnpm-lock.yaml ./ + +RUN pnpm install --frozen-lockfile + +# 复制源码并构建 +COPY zhican/admin/ . +RUN pnpm run build + +# ──────────────────────────────────────────── +# Stage 2: 成品阶段 +# ──────────────────────────────────────────── +FROM nginx:alpine AS runner + +# 移除默认站点配置 +RUN rm /etc/nginx/conf.d/default.conf + +# 注入自定义 nginx 配置 +COPY deploy/nginx.conf /etc/nginx/conf.d/zhican-admin.conf + +# 拷贝构建产物 +COPY --from=builder /app/dist /usr/share/nginx/html + +EXPOSE 80 + +CMD ["nginx", "-g", "daemon off;"] diff --git a/deploy/compose.yaml b/deploy/compose.yaml new file mode 100644 index 0000000..ed6c800 --- /dev/null +++ b/deploy/compose.yaml @@ -0,0 +1,6 @@ +services: + admin: + image: zhican-admin:latest + ports: + - "12801:80" + restart: unless-stopped diff --git a/deploy/nginx.conf b/deploy/nginx.conf new file mode 100644 index 0000000..5f0e055 --- /dev/null +++ b/deploy/nginx.conf @@ -0,0 +1,34 @@ +server { + listen 80; + server_name _; + root /usr/share/nginx/html; + index index.html; + + # Vue Router history 模式:所有未匹配的路径回落到 index.html + location / { + try_files $uri $uri/ /index.html; + } + + # Vite 构建产物带 hash,可安全设置长期缓存 + location /assets/ { + expires 1y; + add_header Cache-Control "public, immutable"; + } + + # gzip 压缩 + gzip on; + gzip_min_length 1024; + gzip_comp_level 6; + gzip_types + text/plain + text/css + text/javascript + application/javascript + application/json + application/xml + image/svg+xml; + + # 基础安全头 + add_header X-Content-Type-Options "nosniff"; + add_header X-Frame-Options "SAMEORIGIN"; +}