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 11:47:21 +08:00
parent dd289d6f4e
commit 251e440108

69
Jenkinsfile vendored
View File

@@ -156,46 +156,24 @@ pipeline {
} }
stage('Deploy to Environment') { stage('Deploy to Environment') {
parallel { steps {
stage('Deploy to Dev') { script {
when { // 创建或更新docker-compose文件
expression { params.DEPLOY_TARGET == 'dev' } def composeContent = getComposeFileContent()
} writeFile file: 'docker-compose.yml', text: composeContent
steps {
deployToEnvironment('dev')
}
}
stage('Deploy to Staging') { // 拉取最新镜像
when { sh 'docker compose pull'
anyOf {
expression { params.DEPLOY_TARGET == 'staging' }
expression { params.DEPLOY_TARGET == 'prod' }
}
}
steps {
deployToEnvironment('staging')
}
}
stage('Deploy to Production') { // 停止旧容器
when { sh 'docker compose down || true'
expression { params.DEPLOY_TARGET == 'prod' }
} // 启动新容器
steps { sh 'docker compose up -d'
script {
input message: "确定要部署到生产环境吗?", ok: "是", parameters: [ echo "所有服务已部署完成"
choice(name: 'CONFIRM_DEPLOY', choices: ['yes', 'no'], description: '确认部署')
]
if (params.CONFIRM_DEPLOY == 'yes') {
deployToEnvironment('prod')
} else {
error "部署被取消"
}
}
}
} }
} }
} }
} }
@@ -292,3 +270,20 @@ def getPortForEnvironment(String env) {
return '80' return '80'
} }
} }
// 生成docker-compose文件内容的函数
def getComposeFileContent(buildNumber) {
def imageToDeploy = "${DOCKER_IMAGE}:latest"
def containerName = "${PROJECT_NAME}-${env}"
return """
version: '3.8'
services:
timeline-story-service:
image: ${imageToDeploy}
container_name: ${containerName}
ports:
- "3000:80"
extra_hosts:
- "host.docker.internal:host-gateway"
"""
}