From c06b152e5f5936d407f39ff1e80888ae2f0f1fbc Mon Sep 17 00:00:00 2001 From: zpc Date: Wed, 25 Mar 2026 09:50:25 +0800 Subject: [PATCH] =?UTF-8?q?=E9=85=8D=E7=BD=AE=20CI/CD=20=E6=B5=81=E6=B0=B4?= =?UTF-8?q?=E7=BA=BF=EF=BC=9ADrone=20CI=20+=20Docker=20+=20Harbor=20?= =?UTF-8?q?=E5=86=85=E7=BD=91=E9=83=A8=E7=BD=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .drone.yml | 63 ++++++++++++++++++++++++++++++++++++++++++++++ admin/Dockerfile | 9 ++++--- admin/nginx.conf | 20 +++++++++++++++ docker-compose.yml | 24 ++++++++++++++++++ server/Dockerfile | 8 ++++-- 5 files changed, 119 insertions(+), 5 deletions(-) create mode 100644 .drone.yml create mode 100644 admin/nginx.conf create mode 100644 docker-compose.yml diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..7d427c9 --- /dev/null +++ b/.drone.yml @@ -0,0 +1,63 @@ +--- +kind: pipeline +type: docker +name: campus-errand + +trigger: + branch: + - main + event: + - push + +steps: + # ==================== 构建后端 API 镜像 ==================== + - name: build-server + image: plugins/docker + settings: + registry: 192.168.195.25:19900 + repo: 192.168.195.25:19900/campus-errand/server + dockerfile: server/Dockerfile + context: server + tags: + - latest + - ${DRONE_COMMIT_SHA:0:8} + username: + from_secret: harbor_username + password: + from_secret: harbor_password + insecure: true + + # ==================== 构建后台管理前端镜像 ==================== + - name: build-admin + image: plugins/docker + settings: + registry: 192.168.195.25:19900 + repo: 192.168.195.25:19900/campus-errand/admin + dockerfile: admin/Dockerfile + context: admin + tags: + - latest + - ${DRONE_COMMIT_SHA:0:8} + username: + from_secret: harbor_username + password: + from_secret: harbor_password + insecure: true + + # ==================== 部署到服务器 ==================== + - name: deploy + image: appleboy/drone-ssh + settings: + host: 192.168.195.15 + username: + from_secret: ssh_username + password: + from_secret: ssh_password + port: 22 + script: + - cd /disk/docker-compose/campus-errand + - docker compose pull + - docker compose up -d + depends_on: + - build-server + - build-admin diff --git a/admin/Dockerfile b/admin/Dockerfile index a3577d5..909717a 100644 --- a/admin/Dockerfile +++ b/admin/Dockerfile @@ -1,11 +1,14 @@ -FROM node:20-alpine AS build +# ==================== 构建阶段 ==================== +FROM 192.168.195.25:19900/library/node:20-alpine AS build WORKDIR /app COPY package*.json ./ -RUN rm -f package-lock.json && npm install +RUN npm ci COPY . . RUN npm run build -FROM nginx:alpine +# ==================== 运行阶段 ==================== +FROM 192.168.195.25:19900/library/nginx:alpine COPY --from=build /app/dist /usr/share/nginx/html +COPY nginx.conf /etc/nginx/conf.d/default.conf EXPOSE 80 CMD ["nginx", "-g", "daemon off;"] diff --git a/admin/nginx.conf b/admin/nginx.conf new file mode 100644 index 0000000..bfdd80b --- /dev/null +++ b/admin/nginx.conf @@ -0,0 +1,20 @@ +server { + listen 80; + server_name _; + + # 前端静态文件 + location / { + root /usr/share/nginx/html; + index index.html; + try_files $uri $uri/ /index.html; + } + + # 反向代理后端 API + location /api/ { + proxy_pass http://server:8080/api/; + 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; + } +} diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..c5be59e --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,24 @@ +version: "3.8" + +services: + # 后端 API 服务 + server: + image: 192.168.195.25:19900/campus-errand/server:latest + container_name: campus-errand-server + ports: + - "5099:8080" + environment: + - ASPNETCORE_ENVIRONMENT=Production + volumes: + - ./appsettings.Production.json:/app/appsettings.Production.json + restart: unless-stopped + + # 后台管理前端 + admin: + image: 192.168.195.25:19900/campus-errand/admin:latest + container_name: campus-errand-admin + ports: + - "5098:80" + depends_on: + - server + restart: unless-stopped diff --git a/server/Dockerfile b/server/Dockerfile index 948e74d..6f6b8e4 100644 --- a/server/Dockerfile +++ b/server/Dockerfile @@ -1,9 +1,11 @@ -FROM mcr.microsoft.com/dotnet/aspnet:10.0-preview AS base +# ==================== 运行时基础镜像 ==================== +FROM 192.168.195.25:19900/library/dotnet/aspnet:10.0-preview AS base USER $APP_UID WORKDIR /app EXPOSE 8080 -FROM mcr.microsoft.com/dotnet/sdk:10.0-preview AS build +# ==================== 构建阶段 ==================== +FROM 192.168.195.25:19900/library/dotnet/sdk:10.0-preview AS build ARG BUILD_CONFIGURATION=Release WORKDIR /src COPY ["CampusErrand.csproj", "."] @@ -11,10 +13,12 @@ RUN dotnet restore "./CampusErrand.csproj" COPY . . RUN dotnet build "./CampusErrand.csproj" -c $BUILD_CONFIGURATION -o /app/build +# ==================== 发布阶段 ==================== FROM build AS publish ARG BUILD_CONFIGURATION=Release RUN dotnet publish "./CampusErrand.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false +# ==================== 最终镜像 ==================== FROM base AS final WORKDIR /app COPY --from=publish /app/publish .