node { properties([ parameters([ // 分支选择参数 gitParameter( branchFilter: 'origin/(.*)', defaultValue: 'master', description: 's4环境,默认master分支', name: 'Code_branch', quickFilterEnabled: true, selectedValue: 'DEFAULT', sortMode: 'NONE', type: 'PT_BRANCH', size: 1 ), // 部署实例选择 choice( name: 'DEPLOY_TARGETS', choices: ['A'], description: '选择需要部署的实例' ), // 部署顺序 string( name: 'DEPLOY_ORDER', defaultValue: 'A', description: '指定部署顺序' ) ]) ]) // 环境配置(集中管理实例参数) def config = [ A: [ remoteHost: "49.51.33.153", projectPath: "/data/webapps/lessie_sourcing_agents_s6", venvDir: "/data/webapps/lessie_sourcing_agents_s6/.venv", port: "8001", killScript: "/data/sh/kill_lessie_sourcing_agents.sh", checkScript: "/data/sh/check_lessie_agents_8001.sh", gunicornWorkers: 4 ], ] stage('拉代码') { checkout scm: [ $class: 'GitSCM', branches: [[name: params.Code_branch]], userRemoteConfigs: [[ url: 'http://172.24.16.20/python/lessie-sourcing-agents.git', credentialsId: 'fly_gitlab_auth' ]] ] } stage('验证参数') { def selectedTargets = params.DEPLOY_TARGETS.split(',').toList() def orderChars = params.DEPLOY_ORDER.split('').toList() if (orderChars.isEmpty()) { error("部署顺序不能为空,请输入如AB、BA的顺序") } orderChars.each { instance -> if (!selectedTargets.contains(instance)) { error("部署顺序包含未选择的实例: ${instance},已选实例:${params.DEPLOY_TARGETS}") } } def uniqueOrder = orderChars.unique() if (uniqueOrder.size() != orderChars.size()) { error("部署顺序包含重复实例: ${params.DEPLOY_ORDER}") } echo "✅ 参数验证通过:已选实例=${selectedTargets},部署顺序=${orderChars}" env.VALID_ORDER = params.DEPLOY_ORDER } stage('动态部署') { def orderChars = env.VALID_ORDER.split('').toList() orderChars.each { instance -> echo "===== 开始部署实例 ${instance} =====" stage("${instance}下线&同步") { def cfg = config[instance] sh "ssh ${cfg.remoteHost} 'sh ${cfg.killScript} ${cfg.port}'" sh """ ssh ${cfg.remoteHost} 'mkdir -p ${cfg.projectPath}' rsync -avz --exclude '.venv' --exclude '.git' ${WORKSPACE}/ ${cfg.remoteHost}:${cfg.projectPath}/ """ } stage("${instance}依赖&启动") { def cfg = config[instance] sh """ ssh ${cfg.remoteHost} ' cd ${cfg.projectPath} uv sync source ${cfg.venvDir}/bin/activate TIMESTAMP=\$(date +"%Y%m%d_%H%M%S") LOGFILE="${cfg.projectPath}/logs/lessie_sourcing_agents_\${TIMESTAMP}.log" nohup env APP_ENV=s6 gunicorn -w ${cfg.gunicornWorkers} -k uvicorn.workers.UvicornWorker \ -b 0.0.0.0:${cfg.port} --timeout 300 dialogue.app:app \ --max-requests 200 --max-requests-jitter 50 \ > "\$LOGFILE" 2>&1 & ln -sf "\$LOGFILE" ${cfg.projectPath}/logs/lessie_sourcing_agents_latest.log ' """ } stage("探测${instance}探测服务") { def cfg = config[instance] sh "sleep 5" sh "ssh ${cfg.remoteHost} 'head -n 300 ${cfg.projectPath}/logs/lessie_sourcing_agents_latest.log | grep -i error || echo 未发现错误日志'" sh "ssh ${cfg.remoteHost} 'sh ${cfg.checkScript}'" } echo "===== 实例 ${instance} 部署完成 =====" } } echo '✅ 所有选中的实例部署成功!' }