From 58f40bbdfc635278ad6c7621dccd724376c49837 Mon Sep 17 00:00:00 2001 From: dxin Date: Tue, 2 Dec 2025 14:22:44 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Ejava=E6=B5=81=E6=B0=B4?= =?UTF-8?q?=E7=BA=BFv2=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...admin.groovy => DM_s1_flymoon_admin_v1.groovy} | 16 +- SCM/部署镜像/s1/DM_s1_flymoon_admin_v2.groovy | 274 ++++++++++++++++++ ...agent.groovy => DM_s1_flymoon_agent_v1.groovy} | 16 +- SCM/部署镜像/s1/DM_s1_flymoon_agent_v2.groovy | 274 ++++++++++++++++++ SCM/部署镜像/s1/DM_s1_flymoon_email_v2.groovy | 174 +++++++---- .../s1/DM_s1_flymoon_payment_v2.groovy | 274 ++++++++++++++++++ 6 files changed, 955 insertions(+), 73 deletions(-) rename SCM/部署镜像/s1/{DM_s1_flymoon_admin.groovy => DM_s1_flymoon_admin_v1.groovy} (94%) create mode 100644 SCM/部署镜像/s1/DM_s1_flymoon_admin_v2.groovy rename SCM/部署镜像/s1/{DM_s1_flymoon_agent.groovy => DM_s1_flymoon_agent_v1.groovy} (92%) create mode 100644 SCM/部署镜像/s1/DM_s1_flymoon_agent_v2.groovy create mode 100644 SCM/部署镜像/s1/DM_s1_flymoon_payment_v2.groovy diff --git a/SCM/部署镜像/s1/DM_s1_flymoon_admin.groovy b/SCM/部署镜像/s1/DM_s1_flymoon_admin_v1.groovy similarity index 94% rename from SCM/部署镜像/s1/DM_s1_flymoon_admin.groovy rename to SCM/部署镜像/s1/DM_s1_flymoon_admin_v1.groovy index da3f76e..e845ab7 100644 --- a/SCM/部署镜像/s1/DM_s1_flymoon_admin.groovy +++ b/SCM/部署镜像/s1/DM_s1_flymoon_admin_v1.groovy @@ -3,7 +3,7 @@ pipeline { parameters { booleanParam( - name: 'DEPLOY_AFTER_BUILD', + name: 'ROLLBACK_VERSION', defaultValue: false, description: '是否快速回滚上一个版本?' ) @@ -34,7 +34,7 @@ pipeline { stages { stage('快速回滚上一版本') { when { - expression { return params.DEPLOY_AFTER_BUILD == true } + expression { return params.ROLLBACK_VERSION == true } } steps { script { @@ -52,7 +52,7 @@ pipeline { } stage('拉取yaml') { when { - expression { return params.DEPLOY_AFTER_BUILD == false } + expression { return params.ROLLBACK_VERSION == false } } steps { git branch: "${params.BRANCH_NAME}", @@ -62,7 +62,7 @@ pipeline { } stage('修改YAML镜像') { when { - expression { return params.DEPLOY_AFTER_BUILD == false } + expression { return params.ROLLBACK_VERSION == false } } steps { script { @@ -81,7 +81,7 @@ pipeline { } stage('部署到k8s') { when { - expression { return params.DEPLOY_AFTER_BUILD == false } + expression { return params.ROLLBACK_VERSION == false } } steps { script { @@ -104,7 +104,7 @@ pipeline { } stage('检查部署情况') { when { - expression { return params.DEPLOY_AFTER_BUILD == false } + expression { return params.ROLLBACK_VERSION == false } } steps { script { @@ -157,7 +157,7 @@ pipeline { } stage('生成变更说明') { when { - expression { return params.DEPLOY_AFTER_BUILD == false } + expression { return params.ROLLBACK_VERSION == false } } steps { script { @@ -177,7 +177,7 @@ pipeline { } stage('提交变更记录到 GitLab') { when { - expression { return params.DEPLOY_AFTER_BUILD == false } + expression { return params.ROLLBACK_VERSION == false } } steps { script { diff --git a/SCM/部署镜像/s1/DM_s1_flymoon_admin_v2.groovy b/SCM/部署镜像/s1/DM_s1_flymoon_admin_v2.groovy new file mode 100644 index 0000000..52370ed --- /dev/null +++ b/SCM/部署镜像/s1/DM_s1_flymoon_admin_v2.groovy @@ -0,0 +1,274 @@ +pipeline { + agent any + + parameters { + imageTag( + name: 'IMAGE_NAME', + description: 'sit 仓库镜像 (除非输入 CUSTOM_IMAGE, 否则使用这里的)', + registry: 'https://uswccr.ccs.tencentyun.com', + image: 'lessiesit/flymoon-admin', + credentialId: 'dxin_img_hub_auth', + filter: '.*', + defaultTag: '', + verifySsl: true + ) + string( + name: 'CUSTOM_IMAGE', + defaultValue: '', + description: '手输完整镜像//:,例如: uswccr.ccs.tencentyun.com/lessiesit/flymoon-admin:v0.0.1 (填充后,则忽略镜像仓库选择)' + ) + booleanParam( + name: 'ROLLBACK_VERSION', + defaultValue: false, + description: '是否快速回滚上一个版本?' + ) + choice( + name: 'BRANCH_NAME', + choices: ['dxin', 'opt'], + description: '选择资源清单Yaml的分支' + ) + } + environment { + KUBECONFIG = credentials('k8s-test-config-admin') // k8s 凭证 ID, Jenkins 中配置的凭证名称 + Deployment_yaml = "${WORKSPACE}/lessie-ai/sit/s1/flymoon-admin.yaml" + Deployment_name = "s1-flymoon-admin" + K8s_namespace = "sit" + Pod_container_name = "flymoon-admin" + } + stages { + stage('回滚上版') { + when { expression { return params.ROLLBACK_VERSION == true } } + steps { + script { + sh """ + echo "=== 开始回滚到上一个版本 ===" + kubectl rollout undo deployment/${Deployment_name} -n ${K8s_namespace} + echo "=== 回滚中... ===" + kubectl rollout status deployment/${Deployment_name} -n ${K8s_namespace} --timeout=60s + echo "=== 查看所使用的镜像 ===" + kubectl get deployment ${Deployment_name} -n ${K8s_namespace} -o=jsonpath='{.spec.template.spec.containers[*].image}' + echo "=== 回滚完成 ===" + """ + } + } + } + stage('决定镜像') { + steps { + script { + def imageRegex = /^([a-zA-Z0-9.-]+)(:[0-9]+)?\/([a-zA-Z0-9._-]+)\/([a-zA-Z0-9._-]+):([a-zA-Z0-9._-]+)$/ + + if (params.CUSTOM_IMAGE?.trim()) { + echo "检测手输镜像格式: ${params.CUSTOM_IMAGE}" + def customImage = params.CUSTOM_IMAGE.trim() + // 验证镜像格式 + def matcher = (customImage =~ imageRegex) + if (!matcher.matches()) { + error "CUSTOM_IMAGE 格式不正确,必须是 registry/namespace/repository:tag 格式\n" + + "示例: uswccr.ccs.tencentyun.com/lessiesit/flymoon-payment:v1.0.0\n" + + "当前输入: ${customImage}" + } + + echo "使用手输镜像: ${customImage}" + env.IMAGE_FULL_NAME = customImage + + } else { + def img = "uswccr.ccs.tencentyun.com/${params.IMAGE_NAME}" + echo "使用 imageTag 插件镜像: ${img}" + env.IMAGE_FULL_NAME = img + } + } + } + } + stage('查询镜像') { + when { expression { return params.ROLLBACK_VERSION == false } } + steps { + script { + echo "查询镜像: ${IMAGE_FULL_NAME}" + + def result = sh(returnStdout: true, script: """ + /data/sh/get-image-labels-fast.sh \ + "${IMAGE_FULL_NAME}" \ + "100038894437" \ + 'h8H1o6Fd!HLXn' | awk '/^{/,/^}\$/' + """).trim() + + if (result == "" || result == null) { + error("查询镜像 label 失败,请检查镜像是否存在或凭证问题") + } + + echo "镜像 Label:\n${result}" + env.IMAGE_LABEL = result + } + } + } + stage('拉取yaml') { + when { expression { return params.ROLLBACK_VERSION == false } } + steps { + git branch: "${params.BRANCH_NAME}", + credentialsId: 'fly_gitlab_auth', + url: 'http://172.24.16.20/opt/opt-config.git' + } + } + stage('修改YAML') { + when { expression { return params.ROLLBACK_VERSION == false } } + steps { + script { + def oldImg = sh ( + script: "kubectl get deployment ${Deployment_name} -n ${K8s_namespace} -o=jsonpath='{.spec.template.spec.containers[*].image}'", + returnStdout: true + ).trim() + env.OLD_IMAGE_NAME = oldImg + echo "--- 目前正常运行的旧镜像: ${OLD_IMAGE_NAME} ---" + echo "--- 所选择新的镜像: ${params.IMAGE_NAME} ---" + echo "--- 修改 Deployment YAML 中的镜像为新镜像版本 ---" + sh """ + sed -i 's#image:.*#image: ${IMAGE_FULL_NAME}#' ${Deployment_yaml} + """ + } + } + } + stage('部署k8s') { + when { expression { return params.ROLLBACK_VERSION == false } } + steps { + script { + def ANNOTATION = "更新 image 为 ${params.IMAGE_NAME}" + sh """ + echo "===Apply Deployment YAML ===" + kubectl apply -f ${Deployment_yaml} -n ${K8s_namespace} + + echo "=== 查看当前新使用的镜像 ===" + kubectl get deployment ${Deployment_name} -n ${K8s_namespace} -o=jsonpath='{.spec.template.spec.containers[*].image}' + + echo "=== 添加注解 ===" + kubectl annotate deployment/${Deployment_name} kubernetes.io/change-cause="${ANNOTATION}" --overwrite -n ${K8s_namespace} + + echo "=== 查看历史版本 ===" + kubectl rollout history deployment/${Deployment_name} -n ${K8s_namespace} + """ + } + } + } + stage('部署情况') { + when { expression { return params.ROLLBACK_VERSION == false } } + steps { + script { + echo "=== 检测部署状态并验证新版本运行情况 ===" + echo "--- 检查 Deployment 更新状态 ---" + def rolloutStatus = sh( + script: "kubectl rollout status deployment/${Deployment_name} -n ${K8s_namespace} --timeout=60s", + returnStatus: true + ) + + if (rolloutStatus != 0){ + echo "Deployment ${Deployment_name} 发布 **超时或失败**,开始排查..." + sh """ + echo "--- Deployment 状态 ---" + kubectl describe deployment ${Deployment_name} -n ${K8s_namespace} || true + + echo '--- Pod 列表 ---' + kubectl get pods -n ${K8s_namespace} -l app=${Pod_container_name} -o wide || true + + echo "--- Pod 描述 (describe) ---" + for pod in \$(kubectl get pods -n ${K8s_namespace} -l app=${Pod_container_name} -o jsonpath='{.items[*].metadata.name}'); do + echo "-- Pod 描述 \$pod --" + kubectl describe pod \$pod -n ${K8s_namespace} || true + done + + echo "--- 最近 200 行 Pod 日志 ---" + for pod in \$(kubectl get pods -n ${K8s_namespace} -l app=${Pod_container_name} -o jsonpath='{.items[*].metadata.name}'); do + echo "-- logs \$pod --" + kubectl logs \$pod -n ${K8s_namespace} --tail=200 || true + done + """ + error("=== Deployment 发布失败,请检查以上输出定位问题 ===") + } + + echo "=== 检查 Pods 是否全部 Ready ===" + sh "kubectl get pods -l app=${Pod_container_name} -n ${K8s_namespace} -o wide" + + echo "=== 获取最新 Pod 名称 ===" + NEW_POD = sh ( + script: "kubectl get pods -l app=${Pod_container_name} -n ${K8s_namespace} --sort-by=.metadata.creationTimestamp -o jsonpath='{.items[-1].metadata.name}'", + returnStdout: true + ).trim() + + echo "=== 新 Pod 启动日志(最近20行) ===" + sh "kubectl logs ${NEW_POD} -n ${K8s_namespace} --tail=20 || true" + + echo "部署成功:${NEW_POD} 已正常运行" + } + } + } + stage('变更说明') { + when { expression { return params.ROLLBACK_VERSION == false } } + steps { + script { + def user = currentBuild.getBuildCauses()[0].userName ?: "SYSTEM" + def now = sh(script: "date '+%Y-%m-%d %H:%M:%S'", returnStdout: true).trim() + env.CHANGE_MSG = """ +jenkins执行人: ${user} +修改时间: ${now} +旧镜像: ${OLD_IMAGE_NAME} +新镜像: ${IMAGE_FULL_NAME} +部署对象: ${Deployment_name} +镜像标签: ${env.IMAGE_LABEL} + """.stripIndent().trim() + echo env.CHANGE_MSG + } + } + } + stage('提交变更') { + when { expression { return params.ROLLBACK_VERSION == false } } + steps { + script { + withCredentials([usernamePassword(credentialsId: 'fly_gitlab_auth', usernameVariable: 'GIT_USER', passwordVariable: 'GIT_PASS')]) { + sh """ + cd ${WORKSPACE} + git config user.name "jenkins" + git config user.email "jenkins@local" + + # 检查工作树是否有变化 + if ! git diff --exit-code ${Deployment_yaml} > /dev/null 2>&1; then + echo "检测到更改,正在提交..." + git add ${Deployment_yaml} + git commit -m "更新镜像 \${CHANGE_MSG}" + else + echo "${Deployment_yaml} 没有变化, 无需commit" + fi + + # 检查是否需要推送(是否有新的提交) + LOCAL=\$(git rev-parse @) + REMOTE=\$(git rev-parse @{u} 2>/dev/null || true) + BASE=\$(git merge-base @ @{u} 2>/dev/null || true) + + if [ "\$LOCAL" = "\$REMOTE" ]; then + echo "已与远程系统同步更新" + elif [ "\$LOCAL" = "\$BASE" ]; then + echo "需要从远程获取数据" + git pull http://172.24.16.20/opt/opt-config.git ${params.BRANCH_NAME} + elif [ "\$REMOTE" = "\$BASE" ]; then + echo "将更改推送到远程服务器..." + # 生成临时 .netrc 文件 + echo "machine 172.24.16.20 login \$GIT_USER password \$GIT_PASS" > ~/.netrc + chmod 600 ~/.netrc + git push http://172.24.16.20/opt/opt-config.git ${params.BRANCH_NAME} + else + echo "与远程模式不同,跳过推送操作" + fi + """ + } + } + } + } + } + + post { + success { + echo "成功!" + echo "=== 所选择镜像: ${params.IMAGE_NAME} ===" + } + failure { + echo "有步骤失败,请检查!" + } + } +} \ No newline at end of file diff --git a/SCM/部署镜像/s1/DM_s1_flymoon_agent.groovy b/SCM/部署镜像/s1/DM_s1_flymoon_agent_v1.groovy similarity index 92% rename from SCM/部署镜像/s1/DM_s1_flymoon_agent.groovy rename to SCM/部署镜像/s1/DM_s1_flymoon_agent_v1.groovy index 64b6d38..b914fc8 100644 --- a/SCM/部署镜像/s1/DM_s1_flymoon_agent.groovy +++ b/SCM/部署镜像/s1/DM_s1_flymoon_agent_v1.groovy @@ -3,7 +3,7 @@ pipeline { parameters { booleanParam( - name: 'DEPLOY_AFTER_BUILD', + name: 'ROLLBACK_VERSION', defaultValue: false, description: '是否快速回滚上一个版本?' ) @@ -33,7 +33,7 @@ pipeline { stages { stage('快速回滚上一版本') { when { - expression { return params.DEPLOY_AFTER_BUILD == true } + expression { return params.ROLLBACK_VERSION == true } } steps { script { @@ -51,7 +51,7 @@ pipeline { } stage('拉取yaml') { when { - expression { return params.DEPLOY_AFTER_BUILD == false } + expression { return params.ROLLBACK_VERSION == false } } steps { git branch: "${params.BRANCH_NAME}", @@ -61,7 +61,7 @@ pipeline { } stage('修改YAML镜像') { when { - expression { return params.DEPLOY_AFTER_BUILD == false } + expression { return params.ROLLBACK_VERSION == false } } steps { script { @@ -75,7 +75,7 @@ pipeline { } stage('部署到k8s') { when { - expression { return params.DEPLOY_AFTER_BUILD == false } + expression { return params.ROLLBACK_VERSION == false } } steps { script { @@ -98,7 +98,7 @@ pipeline { } stage('检查部署情况') { when { - expression { return params.DEPLOY_AFTER_BUILD == false } + expression { return params.ROLLBACK_VERSION == false } } steps { echo "检测部署状态并验证新版本运行情况" @@ -121,7 +121,7 @@ pipeline { } stage('生成变更说明') { when { - expression { return params.DEPLOY_AFTER_BUILD == false } + expression { return params.ROLLBACK_VERSION == false } } steps { script { @@ -140,7 +140,7 @@ pipeline { } stage('提交变更记录到 GitLab') { when { - expression { return params.DEPLOY_AFTER_BUILD == false } + expression { return params.ROLLBACK_VERSION == false } } steps { script { diff --git a/SCM/部署镜像/s1/DM_s1_flymoon_agent_v2.groovy b/SCM/部署镜像/s1/DM_s1_flymoon_agent_v2.groovy new file mode 100644 index 0000000..46662ce --- /dev/null +++ b/SCM/部署镜像/s1/DM_s1_flymoon_agent_v2.groovy @@ -0,0 +1,274 @@ +pipeline { + agent any + + parameters { + imageTag( + name: 'IMAGE_NAME', + description: 'sit 仓库镜像 (除非输入 CUSTOM_IMAGE, 否则使用这里的)', + registry: 'https://uswccr.ccs.tencentyun.com', + image: 'lessiesit/flymoon-agent', + credentialId: 'dxin_img_hub_auth', + filter: '.*', + defaultTag: '', + verifySsl: true + ) + string( + name: 'CUSTOM_IMAGE', + defaultValue: '', + description: '手输完整镜像//:,例如: uswccr.ccs.tencentyun.com/lessiesit/flymoon-agent:v0.0.1 (填充后,则忽略镜像仓库选择)' + ) + booleanParam( + name: 'ROLLBACK_VERSION', + defaultValue: false, + description: '是否快速回滚上一个版本?' + ) + choice( + name: 'BRANCH_NAME', + choices: ['dxin', 'opt'], + description: '选择资源清单Yaml的分支' + ) + } + environment { + KUBECONFIG = credentials('k8s-test-config-admin') // k8s 凭证 ID, Jenkins 中配置的凭证名称 + Deployment_yaml = "${WORKSPACE}/lessie-ai/sit/s1/flymoon-agent.yaml" + Deployment_name = "s1-flymoon-agent" + K8s_namespace = "sit" + Pod_container_name = "flymoon-agent" + } + stages { + stage('回滚上版') { + when { expression { return params.ROLLBACK_VERSION == true } } + steps { + script { + sh """ + echo "=== 开始回滚到上一个版本 ===" + kubectl rollout undo deployment/${Deployment_name} -n ${K8s_namespace} + echo "=== 回滚中... ===" + kubectl rollout status deployment/${Deployment_name} -n ${K8s_namespace} --timeout=60s + echo "=== 查看所使用的镜像 ===" + kubectl get deployment ${Deployment_name} -n ${K8s_namespace} -o=jsonpath='{.spec.template.spec.containers[*].image}' + echo "=== 回滚完成 ===" + """ + } + } + } + stage('决定镜像') { + steps { + script { + def imageRegex = /^([a-zA-Z0-9.-]+)(:[0-9]+)?\/([a-zA-Z0-9._-]+)\/([a-zA-Z0-9._-]+):([a-zA-Z0-9._-]+)$/ + + if (params.CUSTOM_IMAGE?.trim()) { + echo "检测手输镜像格式: ${params.CUSTOM_IMAGE}" + def customImage = params.CUSTOM_IMAGE.trim() + // 验证镜像格式 + def matcher = (customImage =~ imageRegex) + if (!matcher.matches()) { + error "CUSTOM_IMAGE 格式不正确,必须是 registry/namespace/repository:tag 格式\n" + + "示例: uswccr.ccs.tencentyun.com/lessiesit/flymoon-payment:v1.0.0\n" + + "当前输入: ${customImage}" + } + + echo "使用手输镜像: ${customImage}" + env.IMAGE_FULL_NAME = customImage + + } else { + def img = "uswccr.ccs.tencentyun.com/${params.IMAGE_NAME}" + echo "使用 imageTag 插件镜像: ${img}" + env.IMAGE_FULL_NAME = img + } + } + } + } + stage('查询镜像') { + when { expression { return params.ROLLBACK_VERSION == false } } + steps { + script { + echo "查询镜像: ${IMAGE_FULL_NAME}" + + def result = sh(returnStdout: true, script: """ + /data/sh/get-image-labels-fast.sh \ + "${IMAGE_FULL_NAME}" \ + "100038894437" \ + 'h8H1o6Fd!HLXn' | awk '/^{/,/^}\$/' + """).trim() + + if (result == "" || result == null) { + error("查询镜像 label 失败,请检查镜像是否存在或凭证问题") + } + + echo "镜像 Label:\n${result}" + env.IMAGE_LABEL = result + } + } + } + stage('拉取yaml') { + when { expression { return params.ROLLBACK_VERSION == false } } + steps { + git branch: "${params.BRANCH_NAME}", + credentialsId: 'fly_gitlab_auth', + url: 'http://172.24.16.20/opt/opt-config.git' + } + } + stage('修改YAML') { + when { expression { return params.ROLLBACK_VERSION == false } } + steps { + script { + def oldImg = sh ( + script: "kubectl get deployment ${Deployment_name} -n ${K8s_namespace} -o=jsonpath='{.spec.template.spec.containers[*].image}'", + returnStdout: true + ).trim() + env.OLD_IMAGE_NAME = oldImg + echo "--- 目前正常运行的旧镜像: ${OLD_IMAGE_NAME} ---" + echo "--- 所选择新的镜像: ${params.IMAGE_NAME} ---" + echo "--- 修改 Deployment YAML 中的镜像为新镜像版本 ---" + sh """ + sed -i 's#image:.*#image: ${IMAGE_FULL_NAME}#' ${Deployment_yaml} + """ + } + } + } + stage('部署k8s') { + when { expression { return params.ROLLBACK_VERSION == false } } + steps { + script { + def ANNOTATION = "更新 image 为 ${params.IMAGE_NAME}" + sh """ + echo "===Apply Deployment YAML ===" + kubectl apply -f ${Deployment_yaml} -n ${K8s_namespace} + + echo "=== 查看当前新使用的镜像 ===" + kubectl get deployment ${Deployment_name} -n ${K8s_namespace} -o=jsonpath='{.spec.template.spec.containers[*].image}' + + echo "=== 添加注解 ===" + kubectl annotate deployment/${Deployment_name} kubernetes.io/change-cause="${ANNOTATION}" --overwrite -n ${K8s_namespace} + + echo "=== 查看历史版本 ===" + kubectl rollout history deployment/${Deployment_name} -n ${K8s_namespace} + """ + } + } + } + stage('部署情况') { + when { expression { return params.ROLLBACK_VERSION == false } } + steps { + script { + echo "=== 检测部署状态并验证新版本运行情况 ===" + echo "--- 检查 Deployment 更新状态 ---" + def rolloutStatus = sh( + script: "kubectl rollout status deployment/${Deployment_name} -n ${K8s_namespace} --timeout=60s", + returnStatus: true + ) + + if (rolloutStatus != 0){ + echo "Deployment ${Deployment_name} 发布 **超时或失败**,开始排查..." + sh """ + echo "--- Deployment 状态 ---" + kubectl describe deployment ${Deployment_name} -n ${K8s_namespace} || true + + echo '--- Pod 列表 ---' + kubectl get pods -n ${K8s_namespace} -l app=${Pod_container_name} -o wide || true + + echo "--- Pod 描述 (describe) ---" + for pod in \$(kubectl get pods -n ${K8s_namespace} -l app=${Pod_container_name} -o jsonpath='{.items[*].metadata.name}'); do + echo "-- Pod 描述 \$pod --" + kubectl describe pod \$pod -n ${K8s_namespace} || true + done + + echo "--- 最近 200 行 Pod 日志 ---" + for pod in \$(kubectl get pods -n ${K8s_namespace} -l app=${Pod_container_name} -o jsonpath='{.items[*].metadata.name}'); do + echo "-- logs \$pod --" + kubectl logs \$pod -n ${K8s_namespace} --tail=200 || true + done + """ + error("=== Deployment 发布失败,请检查以上输出定位问题 ===") + } + + echo "=== 检查 Pods 是否全部 Ready ===" + sh "kubectl get pods -l app=${Pod_container_name} -n ${K8s_namespace} -o wide" + + echo "=== 获取最新 Pod 名称 ===" + NEW_POD = sh ( + script: "kubectl get pods -l app=${Pod_container_name} -n ${K8s_namespace} --sort-by=.metadata.creationTimestamp -o jsonpath='{.items[-1].metadata.name}'", + returnStdout: true + ).trim() + + echo "=== 新 Pod 启动日志(最近20行) ===" + sh "kubectl logs ${NEW_POD} -n ${K8s_namespace} --tail=20 || true" + + echo "部署成功:${NEW_POD} 已正常运行" + } + } + } + stage('变更说明') { + when { expression { return params.ROLLBACK_VERSION == false } } + steps { + script { + def user = currentBuild.getBuildCauses()[0].userName ?: "SYSTEM" + def now = sh(script: "date '+%Y-%m-%d %H:%M:%S'", returnStdout: true).trim() + env.CHANGE_MSG = """ +jenkins执行人: ${user} +修改时间: ${now} +旧镜像: ${OLD_IMAGE_NAME} +新镜像: ${IMAGE_FULL_NAME} +部署对象: ${Deployment_name} +镜像标签: ${env.IMAGE_LABEL} + """.stripIndent().trim() + echo env.CHANGE_MSG + } + } + } + stage('提交变更') { + when { expression { return params.ROLLBACK_VERSION == false } } + steps { + script { + withCredentials([usernamePassword(credentialsId: 'fly_gitlab_auth', usernameVariable: 'GIT_USER', passwordVariable: 'GIT_PASS')]) { + sh """ + cd ${WORKSPACE} + git config user.name "jenkins" + git config user.email "jenkins@local" + + # 检查工作树是否有变化 + if ! git diff --exit-code ${Deployment_yaml} > /dev/null 2>&1; then + echo "检测到更改,正在提交..." + git add ${Deployment_yaml} + git commit -m "更新镜像 \${CHANGE_MSG}" + else + echo "${Deployment_yaml} 没有变化, 无需commit" + fi + + # 检查是否需要推送(是否有新的提交) + LOCAL=\$(git rev-parse @) + REMOTE=\$(git rev-parse @{u} 2>/dev/null || true) + BASE=\$(git merge-base @ @{u} 2>/dev/null || true) + + if [ "\$LOCAL" = "\$REMOTE" ]; then + echo "已与远程系统同步更新" + elif [ "\$LOCAL" = "\$BASE" ]; then + echo "需要从远程获取数据" + git pull http://172.24.16.20/opt/opt-config.git ${params.BRANCH_NAME} + elif [ "\$REMOTE" = "\$BASE" ]; then + echo "将更改推送到远程服务器..." + # 生成临时 .netrc 文件 + echo "machine 172.24.16.20 login \$GIT_USER password \$GIT_PASS" > ~/.netrc + chmod 600 ~/.netrc + git push http://172.24.16.20/opt/opt-config.git ${params.BRANCH_NAME} + else + echo "与远程模式不同,跳过推送操作" + fi + """ + } + } + } + } + } + + post { + success { + echo "成功!" + echo "=== 所选择镜像: ${params.IMAGE_NAME} ===" + } + failure { + echo "有步骤失败,请检查!" + } + } +} \ No newline at end of file diff --git a/SCM/部署镜像/s1/DM_s1_flymoon_email_v2.groovy b/SCM/部署镜像/s1/DM_s1_flymoon_email_v2.groovy index 1f232ea..f78f0ea 100644 --- a/SCM/部署镜像/s1/DM_s1_flymoon_email_v2.groovy +++ b/SCM/部署镜像/s1/DM_s1_flymoon_email_v2.groovy @@ -2,8 +2,23 @@ pipeline { agent any parameters { + imageTag( + name: 'IMAGE_NAME', + description: 'sit 仓库镜像 (除非输入 CUSTOM_IMAGE, 否则使用这里的)', + registry: 'https://uswccr.ccs.tencentyun.com', + image: 'lessiesit/flymoon-email', + credentialId: 'dxin_img_hub_auth', + filter: '.*', + defaultTag: '', + verifySsl: true + ) + string( + name: 'CUSTOM_IMAGE', + defaultValue: '', + description: '手输完整镜像//:,例如: uswccr.ccs.tencentyun.com/lessiesit/flymoon-email:v0.0.1 (填充后,则忽略镜像仓库选择)' + ) booleanParam( - name: 'DEPLOY_AFTER_BUILD', + name: 'ROLLBACK_VERSION', defaultValue: false, description: '是否快速回滚上一个版本?' ) @@ -12,37 +27,24 @@ pipeline { choices: ['dxin', 'opt'], description: '选择资源清单Yaml的分支' ) - imageTag( - name: 'IMAGE_NAME', - description: '此处是 sit 镜像仓库内的 flymoon-email 镜像', - registry: 'https://uswccr.ccs.tencentyun.com', - image: 'lessiesit/flymoon-email', - credentialId: 'dxin_img_hub_auth', - filter: '.*', - defaultTag: '', - verifySsl: true - ) } environment { KUBECONFIG = credentials('k8s-test-config-admin') // k8s 凭证 ID, Jenkins 中配置的凭证名称 - IMAGE_FULL_NAME = "uswccr.ccs.tencentyun.com/${IMAGE_NAME}" Deployment_yaml = "${WORKSPACE}/lessie-ai/sit/s1/flymoon-email.yaml" Deployment_name = "s1-flymoon-email" K8s_namespace = "sit" Pod_container_name = "flymoon-email" } stages { - stage('快速回滚上一版本') { - when { - expression { return params.DEPLOY_AFTER_BUILD == true } - } + stage('回滚上版') { + when { expression { return params.ROLLBACK_VERSION == true } } steps { script { sh """ echo "=== 开始回滚到上一个版本 ===" kubectl rollout undo deployment/${Deployment_name} -n ${K8s_namespace} echo "=== 回滚中... ===" - kubectl rollout status deployment/${Deployment_name} -n ${K8s_namespace} --timeout=180s + kubectl rollout status deployment/${Deployment_name} -n ${K8s_namespace} --timeout=60s echo "=== 查看所使用的镜像 ===" kubectl get deployment ${Deployment_name} -n ${K8s_namespace} -o=jsonpath='{.spec.template.spec.containers[*].image}' echo "=== 回滚完成 ===" @@ -50,39 +52,83 @@ pipeline { } } } - stage('拉取yaml') { - when { - expression { return params.DEPLOY_AFTER_BUILD == false } + stage('决定镜像') { + steps { + script { + def imageRegex = /^([a-zA-Z0-9.-]+)(:[0-9]+)?\/([a-zA-Z0-9._-]+)\/([a-zA-Z0-9._-]+):([a-zA-Z0-9._-]+)$/ + + if (params.CUSTOM_IMAGE?.trim()) { + echo "检测手输镜像格式: ${params.CUSTOM_IMAGE}" + def customImage = params.CUSTOM_IMAGE.trim() + // 验证镜像格式 + def matcher = (customImage =~ imageRegex) + if (!matcher.matches()) { + error "CUSTOM_IMAGE 格式不正确,必须是 registry/namespace/repository:tag 格式\n" + + "示例: uswccr.ccs.tencentyun.com/lessiesit/flymoon-payment:v1.0.0\n" + + "当前输入: ${customImage}" + } + + echo "使用手输镜像: ${customImage}" + env.IMAGE_FULL_NAME = customImage + + } else { + def img = "uswccr.ccs.tencentyun.com/${params.IMAGE_NAME}" + echo "使用 imageTag 插件镜像: ${img}" + env.IMAGE_FULL_NAME = img + } + } } + } + stage('查询镜像') { + when { expression { return params.ROLLBACK_VERSION == false } } + steps { + script { + echo "查询镜像: ${IMAGE_FULL_NAME}" + + def result = sh(returnStdout: true, script: """ + /data/sh/get-image-labels-fast.sh \ + "${IMAGE_FULL_NAME}" \ + "100038894437" \ + 'h8H1o6Fd!HLXn' | awk '/^{/,/^}\$/' + """).trim() + + if (result == "" || result == null) { + error("查询镜像 label 失败,请检查镜像是否存在或凭证问题") + } + + echo "镜像 Label:\n${result}" + env.IMAGE_LABEL = result + } + } + } + stage('拉取yaml') { + when { expression { return params.ROLLBACK_VERSION == false } } steps { git branch: "${params.BRANCH_NAME}", credentialsId: 'fly_gitlab_auth', url: 'http://172.24.16.20/opt/opt-config.git' } } - stage('修改YAML镜像') { - when { - expression { return params.DEPLOY_AFTER_BUILD == false } - } + stage('修改YAML') { + when { expression { return params.ROLLBACK_VERSION == false } } steps { script { - echo "=== 所选择新的镜像: ${params.IMAGE_NAME} ===" - OLD_IMAGE_NAME = sh ( + def oldImg = sh ( script: "kubectl get deployment ${Deployment_name} -n ${K8s_namespace} -o=jsonpath='{.spec.template.spec.containers[*].image}'", returnStdout: true ).trim() - echo "--- 旧镜像: ${OLD_IMAGE_NAME} ---" + env.OLD_IMAGE_NAME = oldImg + echo "--- 目前正常运行的旧镜像: ${OLD_IMAGE_NAME} ---" + echo "--- 所选择新的镜像: ${params.IMAGE_NAME} ---" + echo "--- 修改 Deployment YAML 中的镜像为新镜像版本 ---" sh """ - echo "=== 修改 Deployment YAML 中的镜像版本 ===" sed -i 's#image:.*#image: ${IMAGE_FULL_NAME}#' ${Deployment_yaml} """ } } } - stage('部署到k8s') { - when { - expression { return params.DEPLOY_AFTER_BUILD == false } - } + stage('部署k8s') { + when { expression { return params.ROLLBACK_VERSION == false } } steps { script { def ANNOTATION = "更新 image 为 ${params.IMAGE_NAME}" @@ -102,10 +148,8 @@ pipeline { } } } - stage('检查部署情况') { - when { - expression { return params.DEPLOY_AFTER_BUILD == false } - } + stage('部署情况') { + when { expression { return params.ROLLBACK_VERSION == false } } steps { script { echo "=== 检测部署状态并验证新版本运行情况 ===" @@ -155,30 +199,26 @@ pipeline { } } } - stage('生成变更说明') { - when { - expression { return params.DEPLOY_AFTER_BUILD == false } - } + stage('变更说明') { + when { expression { return params.ROLLBACK_VERSION == false } } steps { script { def user = currentBuild.getBuildCauses()[0].userName ?: "SYSTEM" def now = sh(script: "date '+%Y-%m-%d %H:%M:%S'", returnStdout: true).trim() env.CHANGE_MSG = """ - jenkins执行人: ${user} - 修改时间: ${now} - 旧镜像: ${OLD_IMAGE_NAME} - 新镜像: ${IMAGE_FULL_NAME} - 部署对象: ${Deployment_name} - """.stripIndent() - echo "=== 本次部署变更说明 ===" +jenkins执行人: ${user} +修改时间: ${now} +旧镜像: ${OLD_IMAGE_NAME} +新镜像: ${IMAGE_FULL_NAME} +部署对象: ${Deployment_name} +镜像标签: ${env.IMAGE_LABEL} + """.stripIndent().trim() echo env.CHANGE_MSG } } } - stage('提交变更记录到 GitLab') { - when { - expression { return params.DEPLOY_AFTER_BUILD == false } - } + stage('提交变更') { + when { expression { return params.ROLLBACK_VERSION == false } } steps { script { withCredentials([usernamePassword(credentialsId: 'fly_gitlab_auth', usernameVariable: 'GIT_USER', passwordVariable: 'GIT_PASS')]) { @@ -187,14 +227,34 @@ pipeline { git config user.name "jenkins" git config user.email "jenkins@local" - git add ${Deployment_yaml} - git commit -m "更新镜像 ${CHANGE_MSG}" + # 检查工作树是否有变化 + if ! git diff --exit-code ${Deployment_yaml} > /dev/null 2>&1; then + echo "检测到更改,正在提交..." + git add ${Deployment_yaml} + git commit -m "更新镜像 \${CHANGE_MSG}" + else + echo "${Deployment_yaml} 没有变化, 无需commit" + fi - # 生成临时 .netrc 文件 - echo "machine 172.24.16.20 login \$GIT_USER password \$GIT_PASS" > ~/.netrc - chmod 600 ~/.netrc + # 检查是否需要推送(是否有新的提交) + LOCAL=\$(git rev-parse @) + REMOTE=\$(git rev-parse @{u} 2>/dev/null || true) + BASE=\$(git merge-base @ @{u} 2>/dev/null || true) - git push http://172.24.16.20/opt/opt-config.git ${params.BRANCH_NAME} + if [ "\$LOCAL" = "\$REMOTE" ]; then + echo "已与远程系统同步更新" + elif [ "\$LOCAL" = "\$BASE" ]; then + echo "需要从远程获取数据" + git pull http://172.24.16.20/opt/opt-config.git ${params.BRANCH_NAME} + elif [ "\$REMOTE" = "\$BASE" ]; then + echo "将更改推送到远程服务器..." + # 生成临时 .netrc 文件 + echo "machine 172.24.16.20 login \$GIT_USER password \$GIT_PASS" > ~/.netrc + chmod 600 ~/.netrc + git push http://172.24.16.20/opt/opt-config.git ${params.BRANCH_NAME} + else + echo "与远程模式不同,跳过推送操作" + fi """ } } diff --git a/SCM/部署镜像/s1/DM_s1_flymoon_payment_v2.groovy b/SCM/部署镜像/s1/DM_s1_flymoon_payment_v2.groovy new file mode 100644 index 0000000..b0e3376 --- /dev/null +++ b/SCM/部署镜像/s1/DM_s1_flymoon_payment_v2.groovy @@ -0,0 +1,274 @@ +pipeline { + agent any + + parameters { + imageTag( + name: 'IMAGE_NAME', + description: 'sit 仓库镜像 (除非输入 CUSTOM_IMAGE, 否则使用这里的)', + registry: 'https://uswccr.ccs.tencentyun.com', + image: 'lessiesit/flymoon-payment', + credentialId: 'dxin_img_hub_auth', + filter: '.*', + defaultTag: '', + verifySsl: true + ) + string( + name: 'CUSTOM_IMAGE', + defaultValue: '', + description: '手输完整镜像//:,例如: uswccr.ccs.tencentyun.com/lessiesit/flymoon-payment:v0.0.1 (填充后,则忽略镜像仓库选择)' + ) + booleanParam( + name: 'ROLLBACK_VERSION', + defaultValue: false, + description: '是否快速回滚上一个版本?' + ) + choice( + name: 'BRANCH_NAME', + choices: ['dxin', 'opt'], + description: '选择资源清单Yaml的分支' + ) + } + environment { + KUBECONFIG = credentials('k8s-test-config-admin') // k8s 凭证 ID, Jenkins 中配置的凭证名称 + Deployment_yaml = "${WORKSPACE}/lessie-ai/sit/s1/flymoon-payment.yaml" + Deployment_name = "s1-flymoon-payment" + K8s_namespace = "sit" + Pod_container_name = "flymoon-payment" + } + stages { + stage('回滚上版') { + when { expression { return params.ROLLBACK_VERSION == true } } + steps { + script { + sh """ + echo "=== 开始回滚到上一个版本 ===" + kubectl rollout undo deployment/${Deployment_name} -n ${K8s_namespace} + echo "=== 回滚中... ===" + kubectl rollout status deployment/${Deployment_name} -n ${K8s_namespace} --timeout=60s + echo "=== 查看所使用的镜像 ===" + kubectl get deployment ${Deployment_name} -n ${K8s_namespace} -o=jsonpath='{.spec.template.spec.containers[*].image}' + echo "=== 回滚完成 ===" + """ + } + } + } + stage('决定镜像') { + steps { + script { + def imageRegex = /^([a-zA-Z0-9.-]+)(:[0-9]+)?\/([a-zA-Z0-9._-]+)\/([a-zA-Z0-9._-]+):([a-zA-Z0-9._-]+)$/ + + if (params.CUSTOM_IMAGE?.trim()) { + echo "检测手输镜像格式: ${params.CUSTOM_IMAGE}" + def customImage = params.CUSTOM_IMAGE.trim() + // 验证镜像格式 + def matcher = (customImage =~ imageRegex) + if (!matcher.matches()) { + error "CUSTOM_IMAGE 格式不正确,必须是 registry/namespace/repository:tag 格式\n" + + "示例: uswccr.ccs.tencentyun.com/lessiesit/flymoon-payment:v1.0.0\n" + + "当前输入: ${customImage}" + } + + echo "使用手输镜像: ${customImage}" + env.IMAGE_FULL_NAME = customImage + + } else { + def img = "uswccr.ccs.tencentyun.com/${params.IMAGE_NAME}" + echo "使用 imageTag 插件镜像: ${img}" + env.IMAGE_FULL_NAME = img + } + } + } + } + stage('查询镜像') { + when { expression { return params.ROLLBACK_VERSION == false } } + steps { + script { + echo "查询镜像: ${IMAGE_FULL_NAME}" + + def result = sh(returnStdout: true, script: """ + /data/sh/get-image-labels-fast.sh \ + "${IMAGE_FULL_NAME}" \ + "100038894437" \ + 'h8H1o6Fd!HLXn' | awk '/^{/,/^}\$/' + """).trim() + + if (result == "" || result == null) { + error("查询镜像 label 失败,请检查镜像是否存在或凭证问题") + } + + echo "镜像 Label:\n${result}" + env.IMAGE_LABEL = result + } + } + } + stage('拉取yaml') { + when { expression { return params.ROLLBACK_VERSION == false } } + steps { + git branch: "${params.BRANCH_NAME}", + credentialsId: 'fly_gitlab_auth', + url: 'http://172.24.16.20/opt/opt-config.git' + } + } + stage('修改YAML') { + when { expression { return params.ROLLBACK_VERSION == false } } + steps { + script { + def oldImg = sh ( + script: "kubectl get deployment ${Deployment_name} -n ${K8s_namespace} -o=jsonpath='{.spec.template.spec.containers[*].image}'", + returnStdout: true + ).trim() + env.OLD_IMAGE_NAME = oldImg + echo "--- 目前正常运行的旧镜像: ${OLD_IMAGE_NAME} ---" + echo "--- 所选择新的镜像: ${params.IMAGE_NAME} ---" + echo "--- 修改 Deployment YAML 中的镜像为新镜像版本 ---" + sh """ + sed -i 's#image:.*#image: ${IMAGE_FULL_NAME}#' ${Deployment_yaml} + """ + } + } + } + stage('部署k8s') { + when { expression { return params.ROLLBACK_VERSION == false } } + steps { + script { + def ANNOTATION = "更新 image 为 ${params.IMAGE_NAME}" + sh """ + echo "===Apply Deployment YAML ===" + kubectl apply -f ${Deployment_yaml} -n ${K8s_namespace} + + echo "=== 查看当前新使用的镜像 ===" + kubectl get deployment ${Deployment_name} -n ${K8s_namespace} -o=jsonpath='{.spec.template.spec.containers[*].image}' + + echo "=== 添加注解 ===" + kubectl annotate deployment/${Deployment_name} kubernetes.io/change-cause="${ANNOTATION}" --overwrite -n ${K8s_namespace} + + echo "=== 查看历史版本 ===" + kubectl rollout history deployment/${Deployment_name} -n ${K8s_namespace} + """ + } + } + } + stage('部署情况') { + when { expression { return params.ROLLBACK_VERSION == false } } + steps { + script { + echo "=== 检测部署状态并验证新版本运行情况 ===" + echo "--- 检查 Deployment 更新状态 ---" + def rolloutStatus = sh( + script: "kubectl rollout status deployment/${Deployment_name} -n ${K8s_namespace} --timeout=60s", + returnStatus: true + ) + + if (rolloutStatus != 0){ + echo "Deployment ${Deployment_name} 发布 **超时或失败**,开始排查..." + sh """ + echo "--- Deployment 状态 ---" + kubectl describe deployment ${Deployment_name} -n ${K8s_namespace} || true + + echo '--- Pod 列表 ---' + kubectl get pods -n ${K8s_namespace} -l app=${Pod_container_name} -o wide || true + + echo "--- Pod 描述 (describe) ---" + for pod in \$(kubectl get pods -n ${K8s_namespace} -l app=${Pod_container_name} -o jsonpath='{.items[*].metadata.name}'); do + echo "-- Pod 描述 \$pod --" + kubectl describe pod \$pod -n ${K8s_namespace} || true + done + + echo "--- 最近 200 行 Pod 日志 ---" + for pod in \$(kubectl get pods -n ${K8s_namespace} -l app=${Pod_container_name} -o jsonpath='{.items[*].metadata.name}'); do + echo "-- logs \$pod --" + kubectl logs \$pod -n ${K8s_namespace} --tail=200 || true + done + """ + error("=== Deployment 发布失败,请检查以上输出定位问题 ===") + } + + echo "=== 检查 Pods 是否全部 Ready ===" + sh "kubectl get pods -l app=${Pod_container_name} -n ${K8s_namespace} -o wide" + + echo "=== 获取最新 Pod 名称 ===" + NEW_POD = sh ( + script: "kubectl get pods -l app=${Pod_container_name} -n ${K8s_namespace} --sort-by=.metadata.creationTimestamp -o jsonpath='{.items[-1].metadata.name}'", + returnStdout: true + ).trim() + + echo "=== 新 Pod 启动日志(最近20行) ===" + sh "kubectl logs ${NEW_POD} -n ${K8s_namespace} --tail=20 || true" + + echo "部署成功:${NEW_POD} 已正常运行" + } + } + } + stage('变更说明') { + when { expression { return params.ROLLBACK_VERSION == false } } + steps { + script { + def user = currentBuild.getBuildCauses()[0].userName ?: "SYSTEM" + def now = sh(script: "date '+%Y-%m-%d %H:%M:%S'", returnStdout: true).trim() + env.CHANGE_MSG = """ +jenkins执行人: ${user} +修改时间: ${now} +旧镜像: ${OLD_IMAGE_NAME} +新镜像: ${IMAGE_FULL_NAME} +部署对象: ${Deployment_name} +镜像标签: ${env.IMAGE_LABEL} + """.stripIndent().trim() + echo env.CHANGE_MSG + } + } + } + stage('提交变更') { + when { expression { return params.ROLLBACK_VERSION == false } } + steps { + script { + withCredentials([usernamePassword(credentialsId: 'fly_gitlab_auth', usernameVariable: 'GIT_USER', passwordVariable: 'GIT_PASS')]) { + sh """ + cd ${WORKSPACE} + git config user.name "jenkins" + git config user.email "jenkins@local" + + # 检查工作树是否有变化 + if ! git diff --exit-code ${Deployment_yaml} > /dev/null 2>&1; then + echo "检测到更改,正在提交..." + git add ${Deployment_yaml} + git commit -m "更新镜像 \${CHANGE_MSG}" + else + echo "${Deployment_yaml} 没有变化, 无需commit" + fi + + # 检查是否需要推送(是否有新的提交) + LOCAL=\$(git rev-parse @) + REMOTE=\$(git rev-parse @{u} 2>/dev/null || true) + BASE=\$(git merge-base @ @{u} 2>/dev/null || true) + + if [ "\$LOCAL" = "\$REMOTE" ]; then + echo "已与远程系统同步更新" + elif [ "\$LOCAL" = "\$BASE" ]; then + echo "需要从远程获取数据" + git pull http://172.24.16.20/opt/opt-config.git ${params.BRANCH_NAME} + elif [ "\$REMOTE" = "\$BASE" ]; then + echo "将更改推送到远程服务器..." + # 生成临时 .netrc 文件 + echo "machine 172.24.16.20 login \$GIT_USER password \$GIT_PASS" > ~/.netrc + chmod 600 ~/.netrc + git push http://172.24.16.20/opt/opt-config.git ${params.BRANCH_NAME} + else + echo "与远程模式不同,跳过推送操作" + fi + """ + } + } + } + } + } + + post { + success { + echo "成功!" + echo "=== 所选择镜像: ${params.IMAGE_NAME} ===" + } + failure { + echo "有步骤失败,请检查!" + } + } +} \ No newline at end of file