From 251e4401087e877fec55f323785163ea0efa2312 Mon Sep 17 00:00:00 2001 From: jianghao <332515344@qq.com> Date: Tue, 30 Dec 2025 11:47:21 +0800 Subject: [PATCH] Jenkins file edit --- Jenkinsfile | 69 +++++++++++++++++++++++++---------------------------- 1 file changed, 32 insertions(+), 37 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 8cd5bd9..75d2d7e 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -156,46 +156,24 @@ pipeline { } stage('Deploy to Environment') { - parallel { - stage('Deploy to Dev') { - when { - expression { params.DEPLOY_TARGET == 'dev' } - } - steps { - deployToEnvironment('dev') - } - } + steps { + script { + // 创建或更新docker-compose文件 + def composeContent = getComposeFileContent() + writeFile file: 'docker-compose.yml', text: composeContent - stage('Deploy to Staging') { - when { - anyOf { - expression { params.DEPLOY_TARGET == 'staging' } - expression { params.DEPLOY_TARGET == 'prod' } - } - } - steps { - deployToEnvironment('staging') - } - } + // 拉取最新镜像 + sh 'docker compose pull' - stage('Deploy to Production') { - when { - expression { params.DEPLOY_TARGET == 'prod' } - } - steps { - script { - input message: "确定要部署到生产环境吗?", ok: "是", parameters: [ - choice(name: 'CONFIRM_DEPLOY', choices: ['yes', 'no'], description: '确认部署') - ] - if (params.CONFIRM_DEPLOY == 'yes') { - deployToEnvironment('prod') - } else { - error "部署被取消" - } - } - } + // 停止旧容器 + sh 'docker compose down || true' + + // 启动新容器 + sh 'docker compose up -d' + + echo "所有服务已部署完成" } - } + } } } @@ -292,3 +270,20 @@ def getPortForEnvironment(String env) { 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" + """ +}