配置 CI/CD 流水线:Drone CI + Docker + Harbor 内网部署

This commit is contained in:
zpc 2026-03-25 09:50:25 +08:00
parent 88200aeca5
commit c06b152e5f
5 changed files with 119 additions and 5 deletions

63
.drone.yml Normal file
View File

@ -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

View File

@ -1,11 +1,14 @@
FROM node:20-alpine AS build # ==================== 构建阶段 ====================
FROM 192.168.195.25:19900/library/node:20-alpine AS build
WORKDIR /app WORKDIR /app
COPY package*.json ./ COPY package*.json ./
RUN rm -f package-lock.json && npm install RUN npm ci
COPY . . COPY . .
RUN npm run build 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 --from=build /app/dist /usr/share/nginx/html
COPY nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80 EXPOSE 80
CMD ["nginx", "-g", "daemon off;"] CMD ["nginx", "-g", "daemon off;"]

20
admin/nginx.conf Normal file
View File

@ -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;
}
}

24
docker-compose.yml Normal file
View File

@ -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

View File

@ -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 USER $APP_UID
WORKDIR /app WORKDIR /app
EXPOSE 8080 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 ARG BUILD_CONFIGURATION=Release
WORKDIR /src WORKDIR /src
COPY ["CampusErrand.csproj", "."] COPY ["CampusErrand.csproj", "."]
@ -11,10 +13,12 @@ RUN dotnet restore "./CampusErrand.csproj"
COPY . . COPY . .
RUN dotnet build "./CampusErrand.csproj" -c $BUILD_CONFIGURATION -o /app/build RUN dotnet build "./CampusErrand.csproj" -c $BUILD_CONFIGURATION -o /app/build
# ==================== 发布阶段 ====================
FROM build AS publish FROM build AS publish
ARG BUILD_CONFIGURATION=Release ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./CampusErrand.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false RUN dotnet publish "./CampusErrand.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
# ==================== 最终镜像 ====================
FROM base AS final FROM base AS final
WORKDIR /app WORKDIR /app
COPY --from=publish /app/publish . COPY --from=publish /app/publish .