fix: 修复 Drone 配置和 nginx 多环境支持
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
- 分离测试/生产环境的前端镜像构建 - nginx 配置使用 BACKEND_HOST 变量区分环境 - 生产环境添加独立的 Docker network - 生产环境使用独立的密钥配置 (xxx_prod) - 修复前端空白问题:确保前后端在同一 network
This commit is contained in:
39
.drone.yml
39
.drone.yml
@@ -20,15 +20,31 @@ steps:
|
|||||||
- docker build -t platform-backend:${DRONE_COMMIT_SHA:0:8} -f deploy/Dockerfile.backend .
|
- docker build -t platform-backend:${DRONE_COMMIT_SHA:0:8} -f deploy/Dockerfile.backend .
|
||||||
- docker tag platform-backend:${DRONE_COMMIT_SHA:0:8} platform-backend:latest
|
- docker tag platform-backend:${DRONE_COMMIT_SHA:0:8} platform-backend:latest
|
||||||
|
|
||||||
# 构建前端镜像
|
# 构建前端镜像(测试环境)
|
||||||
- name: build-frontend
|
- name: build-frontend-test
|
||||||
image: docker:dind
|
image: docker:dind
|
||||||
volumes:
|
volumes:
|
||||||
- name: docker-sock
|
- name: docker-sock
|
||||||
path: /var/run/docker.sock
|
path: /var/run/docker.sock
|
||||||
commands:
|
commands:
|
||||||
- docker build -t platform-frontend:${DRONE_COMMIT_SHA:0:8} -f deploy/Dockerfile.frontend .
|
- docker build -t platform-frontend-test:${DRONE_COMMIT_SHA:0:8} -f deploy/Dockerfile.frontend --build-arg BACKEND_HOST=platform-backend-test .
|
||||||
- docker tag platform-frontend:${DRONE_COMMIT_SHA:0:8} platform-frontend:latest
|
- docker tag platform-frontend-test:${DRONE_COMMIT_SHA:0:8} platform-frontend-test:latest
|
||||||
|
when:
|
||||||
|
branch:
|
||||||
|
- develop
|
||||||
|
|
||||||
|
# 构建前端镜像(生产环境)
|
||||||
|
- name: build-frontend-prod
|
||||||
|
image: docker:dind
|
||||||
|
volumes:
|
||||||
|
- name: docker-sock
|
||||||
|
path: /var/run/docker.sock
|
||||||
|
commands:
|
||||||
|
- docker build -t platform-frontend-prod:${DRONE_COMMIT_SHA:0:8} -f deploy/Dockerfile.frontend --build-arg BACKEND_HOST=platform-backend-prod .
|
||||||
|
- docker tag platform-frontend-prod:${DRONE_COMMIT_SHA:0:8} platform-frontend-prod:latest
|
||||||
|
when:
|
||||||
|
branch:
|
||||||
|
- main
|
||||||
|
|
||||||
# 部署测试环境
|
# 部署测试环境
|
||||||
- name: deploy-test
|
- name: deploy-test
|
||||||
@@ -50,7 +66,7 @@ steps:
|
|||||||
- docker stop platform-backend-test platform-frontend-test || true
|
- docker stop platform-backend-test platform-frontend-test || true
|
||||||
- docker rm platform-backend-test platform-frontend-test || true
|
- docker rm platform-backend-test platform-frontend-test || true
|
||||||
- docker run -d --name platform-backend-test --network platform-network -p 8001:8000 --restart unless-stopped -e DATABASE_URL=$DATABASE_URL -e API_KEY=$API_KEY -e JWT_SECRET=$JWT_SECRET -e CONFIG_ENCRYPT_KEY=$CONFIG_ENCRYPT_KEY platform-backend:latest
|
- docker run -d --name platform-backend-test --network platform-network -p 8001:8000 --restart unless-stopped -e DATABASE_URL=$DATABASE_URL -e API_KEY=$API_KEY -e JWT_SECRET=$JWT_SECRET -e CONFIG_ENCRYPT_KEY=$CONFIG_ENCRYPT_KEY platform-backend:latest
|
||||||
- docker run -d --name platform-frontend-test --network platform-network -p 3003:80 --restart unless-stopped platform-frontend:latest
|
- docker run -d --name platform-frontend-test --network platform-network -p 3003:80 --restart unless-stopped platform-frontend-test:latest
|
||||||
when:
|
when:
|
||||||
branch:
|
branch:
|
||||||
- develop
|
- develop
|
||||||
@@ -63,18 +79,19 @@ steps:
|
|||||||
path: /var/run/docker.sock
|
path: /var/run/docker.sock
|
||||||
environment:
|
environment:
|
||||||
DATABASE_URL:
|
DATABASE_URL:
|
||||||
from_secret: database_url
|
from_secret: database_url_prod
|
||||||
API_KEY:
|
API_KEY:
|
||||||
from_secret: api_key
|
from_secret: api_key_prod
|
||||||
JWT_SECRET:
|
JWT_SECRET:
|
||||||
from_secret: jwt_secret
|
from_secret: jwt_secret_prod
|
||||||
CONFIG_ENCRYPT_KEY:
|
CONFIG_ENCRYPT_KEY:
|
||||||
from_secret: config_encrypt_key
|
from_secret: config_encrypt_key_prod
|
||||||
commands:
|
commands:
|
||||||
|
- docker network create platform-network-prod 2>/dev/null || true
|
||||||
- docker stop platform-backend-prod platform-frontend-prod || true
|
- docker stop platform-backend-prod platform-frontend-prod || true
|
||||||
- docker rm platform-backend-prod platform-frontend-prod || true
|
- docker rm platform-backend-prod platform-frontend-prod || true
|
||||||
- docker run -d --name platform-backend-prod -p 9001:8000 --restart unless-stopped -e DATABASE_URL=$DATABASE_URL -e API_KEY=$API_KEY -e JWT_SECRET=$JWT_SECRET -e CONFIG_ENCRYPT_KEY=$CONFIG_ENCRYPT_KEY platform-backend:latest
|
- docker run -d --name platform-backend-prod --network platform-network-prod -p 9001:8000 --restart unless-stopped -e DATABASE_URL=$DATABASE_URL -e API_KEY=$API_KEY -e JWT_SECRET=$JWT_SECRET -e CONFIG_ENCRYPT_KEY=$CONFIG_ENCRYPT_KEY platform-backend:latest
|
||||||
- docker run -d --name platform-frontend-prod -p 4003:80 --restart unless-stopped platform-frontend:latest
|
- docker run -d --name platform-frontend-prod --network platform-network-prod -p 4003:80 --restart unless-stopped platform-frontend-prod:latest
|
||||||
when:
|
when:
|
||||||
branch:
|
branch:
|
||||||
- main
|
- main
|
||||||
|
|||||||
@@ -13,9 +13,15 @@ RUN npm run build
|
|||||||
# 生产镜像
|
# 生产镜像
|
||||||
FROM nginx:alpine
|
FROM nginx:alpine
|
||||||
|
|
||||||
|
# 后端服务地址(通过 build-arg 传入,构建时替换)
|
||||||
|
ARG BACKEND_HOST=platform-backend-test
|
||||||
|
|
||||||
COPY --from=builder /app/dist /usr/share/nginx/html
|
COPY --from=builder /app/dist /usr/share/nginx/html
|
||||||
COPY deploy/nginx/frontend.conf /etc/nginx/conf.d/default.conf
|
COPY deploy/nginx/frontend.conf /etc/nginx/conf.d/default.conf
|
||||||
|
|
||||||
|
# 在构建时替换后端地址(只替换 BACKEND_HOST 变量)
|
||||||
|
RUN sed -i "s/\${BACKEND_HOST}/${BACKEND_HOST}/g" /etc/nginx/conf.d/default.conf
|
||||||
|
|
||||||
EXPOSE 80
|
EXPOSE 80
|
||||||
|
|
||||||
CMD ["nginx", "-g", "daemon off;"]
|
CMD ["nginx", "-g", "daemon off;"]
|
||||||
|
|||||||
@@ -14,9 +14,9 @@ server {
|
|||||||
}
|
}
|
||||||
|
|
||||||
# API 代理到后端
|
# API 代理到后端
|
||||||
# 使用 Docker 容器名称,通过 Docker DNS 解析
|
# 使用环境变量 BACKEND_HOST,通过 Docker DNS 解析
|
||||||
location /api/ {
|
location /api/ {
|
||||||
set $backend platform-backend-test:8000;
|
set $backend ${BACKEND_HOST}:8000;
|
||||||
proxy_pass http://$backend/api/;
|
proxy_pass http://$backend/api/;
|
||||||
proxy_http_version 1.1;
|
proxy_http_version 1.1;
|
||||||
proxy_set_header Host $host;
|
proxy_set_header Host $host;
|
||||||
|
|||||||
Reference in New Issue
Block a user