diff --git a/Jenkinsfile b/Jenkinsfile index 8acec369..88ff599b 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -9,11 +9,7 @@ def notifyWecomDeploy(Map opts) { } def credId = (opts.webhookCredId ?: env.WECOM_WEBHOOK_CRED ?: 'WECOM_WEBHOOK_DEPLOY').trim() - def envLabel = opts.envLabel ?: '部署环境' - def host = opts.host ?: '-' - def status = (opts.status ?: 'success').toString().toLowerCase() - def isSuccess = status == 'success' - def extraNote = (opts.extraNote ?: '').trim() + def content = buildDeployNotifyMarkdown(opts) try { withCredentials([string(credentialsId: credId, variable: 'WECOM_WEBHOOK_URL')]) { @@ -23,33 +19,9 @@ def notifyWecomDeploy(Map opts) { return } - def shortCommit = env.GIT_COMMIT ? env.GIT_COMMIT.take(8) : '-' - def buildLink = env.BUILD_URL ?: '-' - def title = isSuccess ? "✅ ${envLabel} 部署成功" : "❌ ${envLabel} 部署失败" - def lines = [ - "## ${title}", - "> **分支**: ${env.BRANCH_NAME ?: '-'}", - "> **提交**: `${shortCommit}`", - "> **目标主机**: ${host}", - "> **构建**: [#${env.BUILD_NUMBER}](${buildLink})", - "> **变更**: 前端=${env.FRONTEND_CHANGED ?: '-'} 后端=${env.BACKEND_CHANGED ?: '-'} 配置=${env.DEPLOY_CONFIG_CHANGED ?: '-'}", - ] - if (env.FORCE_FULL_DEPLOY_USED == 'true') { - lines << '> **备注**: 强制全量部署' - } - if (env.REMOTE_DRIFT_DETECTED == 'true') { - lines << '> **备注**: 远端版本漂移触发部署' - } - if (extraNote) { - lines << "> **说明**: ${extraNote}" - } - if (!isSuccess) { - lines << '> **处理**: 请打开 Jenkins 构建日志排查失败步骤' - } - def payload = [ msgtype: 'markdown', - markdown: [content: lines.join('\n')], + markdown: [content: content.markdown], ] writeFile(file: 'wecom-notify-payload.json', encoding: 'UTF-8', text: JsonOutput.toJson(payload)) @@ -79,6 +51,102 @@ def notifyWecomDeploy(Map opts) { } } +// PushPlus → 个人微信(方案3)。Token 存 Jenkins 凭据 PUSHPLUS_DEPLOY_TOKEN,见 https://www.pushplus.plus/ +def buildDeployNotifyMarkdown(Map opts) { + def envLabel = opts.envLabel ?: '部署环境' + def host = opts.host ?: '-' + def status = (opts.status ?: 'success').toString().toLowerCase() + def isSuccess = status == 'success' + def extraNote = (opts.extraNote ?: '').trim() + def shortCommit = env.GIT_COMMIT ? env.GIT_COMMIT.take(8) : '-' + def buildLink = env.BUILD_URL ?: '-' + def title = isSuccess ? "✅ ${envLabel} 部署成功" : "❌ ${envLabel} 部署失败" + def lines = [ + "## ${title}", + "> **分支**: ${env.BRANCH_NAME ?: '-'}", + "> **提交**: `${shortCommit}`", + "> **目标主机**: ${host}", + "> **构建**: [#${env.BUILD_NUMBER}](${buildLink})", + "> **变更**: 前端=${env.FRONTEND_CHANGED ?: '-'} 后端=${env.BACKEND_CHANGED ?: '-'} 配置=${env.DEPLOY_CONFIG_CHANGED ?: '-'}", + ] + if (env.FORCE_FULL_DEPLOY_USED == 'true') { + lines << '> **备注**: 强制全量部署' + } + if (env.REMOTE_DRIFT_DETECTED == 'true') { + lines << '> **备注**: 远端版本漂移触发部署' + } + if (extraNote) { + lines << "> **说明**: ${extraNote}" + } + if (!isSuccess) { + lines << '> **处理**: 请打开 Jenkins 构建日志排查失败步骤' + } + return [title: title, markdown: lines.join('\n')] +} + +def notifyPushPlusDeploy(Map opts) { + if ((env.DISABLE_DEPLOY_PUSHPLUS_NOTIFY ?: '').equalsIgnoreCase('true')) { + echo '[PushPlus] DISABLE_DEPLOY_PUSHPLUS_NOTIFY=true,跳过通知' + return + } + + def credId = (opts.tokenCredId ?: env.PUSHPLUS_TOKEN_CRED ?: 'PUSHPLUS_DEPLOY_TOKEN').trim() + def content = buildDeployNotifyMarkdown(opts) + + try { + withCredentials([string(credentialsId: credId, variable: 'PUSHPLUS_TOKEN')]) { + def token = PUSHPLUS_TOKEN?.trim() + if (!token) { + echo "[PushPlus] 凭据 ${credId} 为空,跳过通知" + return + } + + def payload = [ + token: token, + title: "[HxOps] ${content.title}", + content: content.markdown, + template: 'markdown', + channel: 'wechat', + ] + def topic = (env.PUSHPLUS_TOPIC ?: '').trim() + if (topic) { + payload.topic = topic + } + + writeFile(file: 'pushplus-notify-payload.json', encoding: 'UTF-8', text: JsonOutput.toJson(payload)) + + def httpCode = sh( + returnStdout: true, + script: ''' + set +e + code=$(curl -sS -o pushplus-notify-response.txt -w "%{http_code}" \ + -X POST "https://www.pushplus.plus/send" \ + -H "Content-Type: application/json" \ + -d @pushplus-notify-payload.json) + echo "[PushPlus] HTTP ${code}" >&2 + cat pushplus-notify-response.txt >&2 || true + echo "${code}" + ''' + ).trim() + + def resp = readFile('pushplus-notify-response.txt').trim() + if (httpCode == '200' && (resp.contains('"code":200') || resp.contains('"code": 200'))) { + echo '[PushPlus] 微信通知已发送' + } else { + echo "[PushPlus] 发送可能失败,HTTP=${httpCode},响应=${resp}" + } + } + } catch (Exception e) { + echo "[PushPlus] 通知发送异常(不影响构建结果): ${e.message}" + } +} + +// 企业微信 + PushPlus 微信双通道 +def notifyDeployAll(Map opts) { + notifyWecomDeploy(opts) + notifyPushPlusDeploy(opts) +} + pipeline { agent any @@ -121,6 +189,9 @@ pipeline { DEV_DEPLOY_ROOT = '/opt/hxops' TEST_DEPLOY_ROOT = '/home/lisq/ai/platform/hxops' PROD_DEPLOY_ROOT = '/data/ai-platform/hxops/' + + // PushPlus 群组编码(一对多推送到个人微信);Token 仍用 Jenkins 凭据 PUSHPLUS_DEPLOY_TOKEN + PUSHPLUS_TOPIC = 'hxops666' } stages { @@ -326,7 +397,7 @@ pipeline { post { success { script { - notifyWecomDeploy( + notifyDeployAll( envLabel: '测试环境', host: env.TEST_DEPLOY_HOST, status: 'success', @@ -335,7 +406,7 @@ pipeline { } failure { script { - notifyWecomDeploy( + notifyDeployAll( envLabel: '测试环境', host: env.TEST_DEPLOY_HOST, status: 'failure', @@ -344,7 +415,7 @@ pipeline { } aborted { script { - notifyWecomDeploy( + notifyDeployAll( envLabel: '测试环境', host: env.TEST_DEPLOY_HOST, status: 'failure', @@ -415,7 +486,7 @@ pipeline { post { success { script { - notifyWecomDeploy( + notifyDeployAll( envLabel: '生产环境', host: env.PROD_DEPLOY_HOST, status: 'success', @@ -424,7 +495,7 @@ pipeline { } failure { script { - notifyWecomDeploy( + notifyDeployAll( envLabel: '生产环境', host: env.PROD_DEPLOY_HOST, status: 'failure', @@ -433,7 +504,7 @@ pipeline { } aborted { script { - notifyWecomDeploy( + notifyDeployAll( envLabel: '生产环境', host: env.PROD_DEPLOY_HOST, status: 'failure',