Files
Work-configuration-file/jenkins/流水线配置/app_lessie_ai/s1新的可选部署顺序py.conf
2025-10-07 15:58:15 +08:00

156 lines
6.1 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

node {
properties([
parameters([
// 分支选择参数
gitParameter(
branchFilter: 'origin/(.*)',
defaultValue: 'master',
description: 's1环境默认master分支',
name: 'Code_branch',
quickFilterEnabled: true,
selectedValue: 'DEFAULT',
sortMode: 'NONE',
type: 'PT_BRANCH',
size: 1
),
// 部署实例选择
choice(
name: 'DEPLOY_TARGETS',
choices: ['A', 'B', 'A,B'],
description: '选择需要部署的实例(多选格式为"A,B"'
),
// 部署顺序
string(
name: 'DEPLOY_ORDER',
defaultValue: 'A',
description: '指定部署顺序如AB、BA必须是已选实例的组合'
)
])
])
// 环境配置(集中管理实例参数)
def config = [
A: [
remoteHost: "43.130.56.138",
projectPath: "/data/webapps/lessie_sourcing_agents",
venvDir: "/data/webapps/lessie_sourcing_agents/venv",
nginxBackend: "10.0.0.5",
port: "8000",
killScript: "/data/sh/kill_lessie_sourcing_agents.sh",
checkScript: "/data/sh/check_lessie_agents_8000.sh",
gunicornWorkers: 8
],
B: [
remoteHost: "43.130.53.202",
projectPath: "/data/webapps/lessie_sourcing_agents",
venvDir: "/data/webapps/lessie_sourcing_agents/venv",
nginxBackend: "10.0.0.13",
port: "8002",
killScript: "/data/sh/kill_lessie_sourcing_agents.sh",
checkScript: "/data/sh/check_lessie_agents_8002.sh",
gunicornWorkers: 4
]
]
def commonConfig = [
nginxHost: "49.51.46.148",
connectionTimeout: "600",
nginxReloadScript: "/data/sh/set_s1_backend_weight_new.sh",
waitConnectionsScript: "/data/sh/wait_s1_for_connections.sh"
]
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} ====="
// 实例部署的5个步骤直接在循环中定义stage无嵌套
stage("${instance}:脱离后端组") {
def cfg = config[instance]
sh "ssh ${commonConfig.nginxHost} 'sh ${commonConfig.nginxReloadScript} ${cfg.nginxBackend} ${cfg.port} down'"
sh "ssh ${cfg.remoteHost} 'sh ${commonConfig.waitConnectionsScript} ${cfg.port} ${commonConfig.connectionTimeout}'"
}
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}
source ~/.bashrc
conda activate search
source ${cfg.venvDir}/bin/activate
pip install --upgrade pip
pip install -r requirements.txt
TIMESTAMP=\$(date +"%Y%m%d_%H%M%S")
LOGFILE="${cfg.projectPath}/logs/lessie_sourcing_agents_\${TIMESTAMP}.log"
nohup env APP_ENV=s1 gunicorn -w ${cfg.gunicornWorkers} -k uvicorn.workers.UvicornWorker \
-b 0.0.0.0:${cfg.port} --timeout 300 dialogue.app:app \
--max-requests 500 --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}'"
}
stage("恢复${instance}流量") {
def cfg = config[instance]
sh "ssh ${commonConfig.nginxHost} 'sh ${commonConfig.nginxReloadScript} ${cfg.nginxBackend} ${cfg.port} up'"
}
echo "===== 实例 ${instance} 部署完成 ====="
}
}
echo '✅ 所有选中的实例部署成功!'
}