diff --git a/Jenkinsfile b/Jenkinsfile index 00b3a8b..2bfe592 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -228,36 +228,12 @@ pipeline { // 公共函数定义 def deployToEnvironmentWithCompose(String env) { script { - // 根据环境设置部署参数 - def containerName = "${PROJECT_NAME}-${env}" + // 根据环境设置部署参数,确保容器名称合法 + def cleanEnv = env.replaceAll('[^a-zA-Z0-9]', '-') + def containerName = "${PROJECT_NAME}-${cleanEnv}".replaceAll('[^a-zA-Z0-9_.-]', '') def imageToDeploy = "${DOCKER_IMAGE}:latest" - // 读取原始 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 文件,使用卷挂载覆盖容器内的 nginx 配置 + // 创建 docker-compose.yml 文件,使用 host 网络模式以便访问宿主机上的后端服务 def composeFileContent = """services: frontend: image: ${imageToDeploy} @@ -265,14 +241,7 @@ def deployToEnvironmentWithCompose(String env) { restart: unless-stopped ports: - "${getPortForEnvironment(env)}:80" - extra_hosts: - - "host.docker.internal:host-gateway" # 允许访问宿主机 - networks: - - app-network - -networks: - app-network: - driver: bridge + network_mode: "host" """ // 写入 docker-compose.yml 文件