跟新 nginx 流量切换方式
This commit is contained in:
Vendored
+6
-11
@@ -3,11 +3,6 @@
|
|||||||
*
|
*
|
||||||
* 架构:Jenkins 本地编译 → scp 镜像到目标机 → SSH 远程蓝绿切换
|
* 架构:Jenkins 本地编译 → scp 镜像到目标机 → SSH 远程蓝绿切换
|
||||||
* 目标机目录:/opt/offerpie/web/
|
* 目标机目录:/opt/offerpie/web/
|
||||||
*
|
|
||||||
* 内层 Nginx 职责:
|
|
||||||
* - 蓝绿切换(前端静态文件 serve)
|
|
||||||
* - /api/ 代理到 Java 后端(10202)
|
|
||||||
* - /ai-api/ 代理到 Python AI(10502),rewrite 去掉前缀
|
|
||||||
*/
|
*/
|
||||||
pipeline {
|
pipeline {
|
||||||
agent any
|
agent any
|
||||||
@@ -18,10 +13,10 @@ pipeline {
|
|||||||
|
|
||||||
environment {
|
environment {
|
||||||
// 目标服务器配置
|
// 目标服务器配置
|
||||||
DEPLOY_HOST = '8.138.180.255'
|
DEPLOY_HOST = '8.148.237.97'
|
||||||
DEPLOY_PORT = '22'
|
DEPLOY_PORT = '22'
|
||||||
DEPLOY_USER = 'root'
|
DEPLOY_USER = 'root'
|
||||||
DEPLOY_PASS = 'sh.0807.'
|
DEPLOY_PASS = 'Mzpy520@126.com'
|
||||||
|
|
||||||
// 项目配置
|
// 项目配置
|
||||||
IMAGE_NAME = 'offerpie-web'
|
IMAGE_NAME = 'offerpie-web'
|
||||||
@@ -65,9 +60,7 @@ pipeline {
|
|||||||
echo "传输文件到目标服务器"
|
echo "传输文件到目标服务器"
|
||||||
sh "${SSH_CMD} 'mkdir -p ${REMOTE_DIR}'"
|
sh "${SSH_CMD} 'mkdir -p ${REMOTE_DIR}'"
|
||||||
sh "${SCP_CMD} ${IMAGE_NAME}.tar ${DEPLOY_USER}@${DEPLOY_HOST}:${REMOTE_DIR}/"
|
sh "${SCP_CMD} ${IMAGE_NAME}.tar ${DEPLOY_USER}@${DEPLOY_HOST}:${REMOTE_DIR}/"
|
||||||
// 传输 proxy_nginx.conf(用 cat 写入保持 inode,避免 bind mount 失效)
|
sh "${SCP_CMD} proxy_nginx.conf ${DEPLOY_USER}@${DEPLOY_HOST}:${REMOTE_DIR}/proxy_nginx.conf"
|
||||||
sh "${SCP_CMD} proxy_nginx.conf ${DEPLOY_USER}@${DEPLOY_HOST}:${REMOTE_DIR}/proxy_nginx.conf.tmp"
|
|
||||||
sh "${SSH_CMD} 'cat ${REMOTE_DIR}/proxy_nginx.conf.tmp > ${REMOTE_DIR}/proxy_nginx.conf && rm -f ${REMOTE_DIR}/proxy_nginx.conf.tmp'"
|
|
||||||
sh "${SCP_CMD} docker-compose.yml ${DEPLOY_USER}@${DEPLOY_HOST}:${REMOTE_DIR}/docker-compose.yml"
|
sh "${SCP_CMD} docker-compose.yml ${DEPLOY_USER}@${DEPLOY_HOST}:${REMOTE_DIR}/docker-compose.yml"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -133,6 +126,8 @@ pipeline {
|
|||||||
}
|
}
|
||||||
echo "Nginx 容器已存在且运行中"
|
echo "Nginx 容器已存在且运行中"
|
||||||
}
|
}
|
||||||
|
// docker cp proxy_nginx.conf 进容器
|
||||||
|
sh "${SSH_CMD} 'docker cp ${REMOTE_DIR}/proxy_nginx.conf ${CONTAINER_PREFIX}-nginx:/etc/nginx/nginx.conf'"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -168,7 +163,7 @@ pipeline {
|
|||||||
stage('切换流量') {
|
stage('切换流量') {
|
||||||
steps {
|
steps {
|
||||||
script {
|
script {
|
||||||
sh "${SSH_CMD} \"sed -i 's/proxy_pass http:\\/\\/\\(blue\\|green\\):80;/proxy_pass http:\\/\\/${env.DEPLOY_TARGET}:80;/' ${REMOTE_DIR}/proxy_nginx.conf\""
|
sh "${SSH_CMD} 'docker exec ${CONTAINER_PREFIX}-nginx sed -i \"s/proxy_pass http:\\/\\/\\(blue\\|green\\):80;/proxy_pass http:\\/\\/${env.DEPLOY_TARGET}:80;/\" /etc/nginx/nginx.conf'"
|
||||||
sh "${SSH_CMD} 'docker exec ${CONTAINER_PREFIX}-nginx nginx -s reload'"
|
sh "${SSH_CMD} 'docker exec ${CONTAINER_PREFIX}-nginx nginx -s reload'"
|
||||||
echo "✅ 流量已切换到 ${env.DEPLOY_TARGET}"
|
echo "✅ 流量已切换到 ${env.DEPLOY_TARGET}"
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-4
@@ -5,8 +5,6 @@ services:
|
|||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
ports:
|
ports:
|
||||||
- "10302:80"
|
- "10302:80"
|
||||||
volumes:
|
|
||||||
- ./proxy_nginx.conf:/etc/nginx/nginx.conf:ro
|
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: ["CMD", "wget", "--spider", "-q", "http://localhost/health"]
|
test: ["CMD", "wget", "--spider", "-q", "http://localhost/health"]
|
||||||
interval: 30s
|
interval: 30s
|
||||||
@@ -15,8 +13,8 @@ services:
|
|||||||
deploy:
|
deploy:
|
||||||
resources:
|
resources:
|
||||||
limits:
|
limits:
|
||||||
memory: 128M
|
memory: 256M
|
||||||
cpus: '0.25'
|
cpus: '0.5'
|
||||||
|
|
||||||
blue:
|
blue:
|
||||||
image: offerpie-web:latest
|
image: offerpie-web:latest
|
||||||
|
|||||||
Reference in New Issue
Block a user