Jenkins file edit
Some checks failed
test/timeline-frontend/pipeline/head There was a failure building this commit

This commit is contained in:
2025-12-30 12:41:12 +08:00
parent 251e440108
commit 0d103a081f

68
Jenkinsfile vendored
View File

@@ -226,30 +226,72 @@ pipeline {
} }
// 公共函数定义 // 公共函数定义
def deployToEnvironment(String env) { def deployToEnvironmentWithCompose(String env) {
script { script {
// 根据环境设置部署参数 // 根据环境设置部署参数
def containerName = "${PROJECT_NAME}-${env}" def containerName = "${PROJECT_NAME}-${env}"
def imageToDeploy = "${DOCKER_IMAGE}:latest" def imageToDeploy = "${DOCKER_IMAGE}:latest"
// 停止现有容器 // 读取原始 nginx 配置
sh """ def nginxConfContent = readFile('nginx.conf')
docker stop ${containerName} || true
docker rm ${containerName} || true // 根据部署环境替换后端服务地址
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 """ sh """
docker run -d \\ docker-compose -f docker-compose.yml down || true
--name ${containerName} \\ """
--restart unless-stopped \\
-p ${getPortForEnvironment(env)}:80 \\ // 启动新服务
${imageToDeploy} sh """
docker-compose -f docker-compose.yml up -d
""" """
// 验证部署 // 验证部署
sh """ sh """
echo "等待容器启动..." echo "等待服务启动..."
sleep 10 sleep 10
docker ps | grep ${containerName} docker ps | grep ${containerName}
docker logs ${containerName} docker logs ${containerName}
@@ -272,7 +314,7 @@ def getPortForEnvironment(String env) {
} }
// 生成docker-compose文件内容的函数 // 生成docker-compose文件内容的函数
def getComposeFileContent(buildNumber) { def getComposeFileContent() {
def imageToDeploy = "${DOCKER_IMAGE}:latest" def imageToDeploy = "${DOCKER_IMAGE}:latest"
def containerName = "${PROJECT_NAME}-${env}" def containerName = "${PROJECT_NAME}-${env}"
return """ return """