From 1dd139c1c6a189bbe2bef301dd891c5d1bad2d71 Mon Sep 17 00:00:00 2001 From: ouyangxq <775153912@qq.com> Date: Fri, 18 Sep 2026 18:02:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Jenkinsfile | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/Jenkinsfile b/Jenkinsfile index 0dec6ca3..6f0ae53c 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,3 +1,37 @@ +def sendProductionWeComNotification(String status, String color, String detail) { + withCredentials([string(credentialsId: 'wecom-prod-webhook', variable: 'WECOM_WEBHOOK')]) { + String sourceVersion = env.SOURCE_TAG ?: env.GIT_COMMIT ?: '—' + String content = """## 生产部署通知 +> 项目:${env.JOB_NAME} +> 构建:[#${env.BUILD_NUMBER}](${env.BUILD_URL}) +> 提交:${sourceVersion} +> 状态:${status} +> 详情:${detail} +> 时间:${new Date().format('yyyy-MM-dd HH:mm:ss', TimeZone.getTimeZone('Asia/Shanghai'))} +""" + String payloadFile = 'wecom-production-notification.json' + writeFile( + file: payloadFile, + text: groovy.json.JsonOutput.toJson([ + msgtype: 'markdown', + markdown: [content: content] + ]), + encoding: 'UTF-8' + ) + sh ''' + set -eu + payload_file='wecom-production-notification.json' + trap 'rm -f "$payload_file"' EXIT HUP INT TERM + response="$(curl --fail --silent --show-error \ + -H 'Content-Type: application/json' \ + --data-binary @"$payload_file" \ + "$WECOM_WEBHOOK")" + printf '%s\n' "$response" + printf '%s' "$response" | grep -Eq '"errcode"[[:space:]]*:[[:space:]]*0([[:space:]]*[,}])' + ''' + } +} + pipeline { agent { label "${env.JOB_BASE_NAME?.endsWith('-prod') ? 'java-builder' : (env.JOB_BASE_NAME?.endsWith('-dev') ? 'java-dev' : 'java-test')}" @@ -161,6 +195,17 @@ pipeline { } post { + success { + script { + if (env.JOB_BASE_NAME?.endsWith('-prod')) { + try { + sendProductionWeComNotification('部署成功', 'info', 'Admin 生产前端已完成发布和部署检查') + } catch (Exception error) { + echo "企业微信通知失败,不影响原构建结果:${error.message}" + } + } + } + } failure { sh ''' if [ "${IS_PRODUCTION:-false}" = "true" ]; then @@ -171,6 +216,26 @@ pipeline { docker compose -f docker-compose.test.yml logs --tail=100 || true fi ''' + script { + if (env.JOB_BASE_NAME?.endsWith('-prod')) { + try { + sendProductionWeComNotification('部署失败', 'warning', '请打开 Jenkins 构建日志查看失败阶段') + } catch (Exception error) { + echo "企业微信通知失败,不影响原构建结果:${error.message}" + } + } + } + } + aborted { + script { + if (env.JOB_BASE_NAME?.endsWith('-prod')) { + try { + sendProductionWeComNotification('部署已取消', 'comment', '生产部署流程被人工取消或中止') + } catch (Exception error) { + echo "企业微信通知失败,不影响原构建结果:${error.message}" + } + } + } } } }