跟新 nginx 流量切换方式
This commit is contained in:
Vendored
+4
-11
@@ -58,14 +58,9 @@ pipeline {
|
||||
stage('文件传输') {
|
||||
steps {
|
||||
echo "传输文件到目标服务器"
|
||||
// 确保远程目录存在
|
||||
sh "${SSH_CMD} 'mkdir -p ${REMOTE_DIR}'"
|
||||
// 传输镜像
|
||||
sh "${SCP_CMD} ${IMAGE_NAME}.tar ${DEPLOY_USER}@${DEPLOY_HOST}:${REMOTE_DIR}/"
|
||||
// 传输 nginx.conf(用 cat 写入保持 inode,避免 bind mount 失效)
|
||||
sh "${SCP_CMD} client-api/nginx.conf ${DEPLOY_USER}@${DEPLOY_HOST}:${REMOTE_DIR}/nginx.conf.tmp"
|
||||
sh "${SSH_CMD} 'cat ${REMOTE_DIR}/nginx.conf.tmp > ${REMOTE_DIR}/nginx.conf && rm -f ${REMOTE_DIR}/nginx.conf.tmp'"
|
||||
// 传输 docker-compose.yml
|
||||
sh "${SCP_CMD} client-api/nginx.conf ${DEPLOY_USER}@${DEPLOY_HOST}:${REMOTE_DIR}/nginx.conf"
|
||||
sh "${SCP_CMD} docker-compose.client-api.yml ${DEPLOY_USER}@${DEPLOY_HOST}:${REMOTE_DIR}/docker-compose.yml"
|
||||
}
|
||||
}
|
||||
@@ -104,7 +99,6 @@ pipeline {
|
||||
stage('启动新版本') {
|
||||
steps {
|
||||
script {
|
||||
// 清理目标色残留容器
|
||||
def existingContainer = sh(script: "${SSH_CMD} 'docker ps -aq -f name=${CONTAINER_PREFIX}-${env.DEPLOY_TARGET}'", returnStdout: true).trim()
|
||||
if (existingContainer) {
|
||||
sh "${SSH_CMD} 'docker rm -f ${CONTAINER_PREFIX}-${env.DEPLOY_TARGET}'"
|
||||
@@ -132,6 +126,8 @@ pipeline {
|
||||
}
|
||||
echo "Nginx 容器已存在且运行中"
|
||||
}
|
||||
// docker cp nginx.conf 进容器(覆盖更新配置模板)
|
||||
sh "${SSH_CMD} 'docker cp ${REMOTE_DIR}/nginx.conf ${CONTAINER_PREFIX}-nginx:/etc/nginx/nginx.conf'"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -167,7 +163,7 @@ pipeline {
|
||||
stage('切换流量') {
|
||||
steps {
|
||||
script {
|
||||
sh "${SSH_CMD} \"sed -i 's/proxy_pass http:\\/\\/\\(blue\\|green\\):8080;/proxy_pass http:\\/\\/${env.DEPLOY_TARGET}:8080;/' ${REMOTE_DIR}/nginx.conf\""
|
||||
sh "${SSH_CMD} 'docker exec ${CONTAINER_PREFIX}-nginx sed -i \"s/proxy_pass http:\\/\\/\\(blue\\|green\\):8080;/proxy_pass http:\\/\\/${env.DEPLOY_TARGET}:8080;/\" /etc/nginx/nginx.conf'"
|
||||
sh "${SSH_CMD} 'docker exec ${CONTAINER_PREFIX}-nginx nginx -s reload'"
|
||||
echo "✅ 流量已切换到 ${env.DEPLOY_TARGET}"
|
||||
}
|
||||
@@ -188,9 +184,7 @@ pipeline {
|
||||
|
||||
stage('清理') {
|
||||
steps {
|
||||
// 清理远程镜像文件
|
||||
sh "${SSH_CMD} 'rm -f ${REMOTE_DIR}/${IMAGE_NAME}.tar'"
|
||||
// 清理本地镜像文件
|
||||
sh "rm -f ${IMAGE_NAME}.tar"
|
||||
}
|
||||
}
|
||||
@@ -202,7 +196,6 @@ pipeline {
|
||||
}
|
||||
failure {
|
||||
echo '❌ 部署失败,请检查日志'
|
||||
// 失败时清理本地临时文件
|
||||
sh "rm -f ${IMAGE_NAME}.tar || true"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,8 +5,6 @@ services:
|
||||
restart: unless-stopped
|
||||
ports:
|
||||
- "10202:80"
|
||||
volumes:
|
||||
- ./nginx.conf:/etc/nginx/nginx.conf:ro
|
||||
healthcheck:
|
||||
test: ["CMD", "wget", "--spider", "-q", "http://localhost/health"]
|
||||
interval: 30s
|
||||
|
||||
Reference in New Issue
Block a user