From 0d103a081f1b151e4d5d3232088f2cdd76d816aa Mon Sep 17 00:00:00 2001 From: jianghao <332515344@qq.com> Date: Tue, 30 Dec 2025 12:41:12 +0800 Subject: [PATCH] Jenkins file edit --- Jenkinsfile | 68 +++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 55 insertions(+), 13 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 75d2d7e..9972361 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -226,30 +226,72 @@ pipeline { } // 公共函数定义 -def deployToEnvironment(String env) { +def deployToEnvironmentWithCompose(String env) { script { // 根据环境设置部署参数 def containerName = "${PROJECT_NAME}-${env}" def imageToDeploy = "${DOCKER_IMAGE}:latest" - // 停止现有容器 - sh """ - docker stop ${containerName} || true - docker rm ${containerName} || true + // 读取原始 nginx 配置 + def nginxConfContent = readFile('nginx.conf') + + // 根据部署环境替换后端服务地址 + switch(env) { + case 'dev': + // 开发环境可能使用特定的后端服务地址 + nginxConfContent = nginxConfContent.replaceAll('host.docker.internal:33333', 'dev-backend-service:33333') + break + case 'staging': + // 预发布环境可能使用特定的后端服务地址 + nginxConfContent = nginxConfContent.replaceAll('host.docker.internal:33333', 'staging-backend-service:33333') + break + case 'prod': + // 生产环境可能使用特定的后端服务地址 + nginxConfContent = nginxConfContent.replaceAll('host.docker.internal:33333', 'prod-backend-service:33333') + break + default: + // 默认使用生产环境后端服务 + nginxConfContent = nginxConfContent.replaceAll('host.docker.internal:33333', 'prod-backend-service:33333') + } + + // 写入环境特定的 nginx 配置 + writeFile file: "nginx-${env}.conf", text: nginxConfContent + + // 创建 docker-compose.yml 文件,前端服务连接到已存在的后端 + def composeFileContent = """version: '3.8' + services: + frontend: + image: ${imageToDeploy} + container_name: ${containerName} + restart: unless-stopped + ports: + - "${getPortForEnvironment(env)}:80" + extra_hosts: + - "host.docker.internal:host-gateway" # 兼容 Docker Desktop + networks: + - app-network + + networks: + app-network: + driver: bridge """ - // 运行新容器 - 使用标准端口映射 + // 写入 docker-compose.yml 文件 + writeFile file: 'docker-compose.yml', text: composeFileContent + + // 停止现有服务 sh """ - docker run -d \\ - --name ${containerName} \\ - --restart unless-stopped \\ - -p ${getPortForEnvironment(env)}:80 \\ - ${imageToDeploy} + docker-compose -f docker-compose.yml down || true + """ + + // 启动新服务 + sh """ + docker-compose -f docker-compose.yml up -d """ // 验证部署 sh """ - echo "等待容器启动..." + echo "等待服务启动..." sleep 10 docker ps | grep ${containerName} docker logs ${containerName} @@ -272,7 +314,7 @@ def getPortForEnvironment(String env) { } // 生成docker-compose文件内容的函数 -def getComposeFileContent(buildNumber) { +def getComposeFileContent() { def imageToDeploy = "${DOCKER_IMAGE}:latest" def containerName = "${PROJECT_NAME}-${env}" return """