部署成功或失败发送企业微信通知
This commit is contained in:
1 parent
72642b981e
commit
e0d91e53e7
3 files changed
+193
-14
No files matched your search
Vendored
+141
@@ -1,3 +1,84 @@
|
||||
import groovy.json.JsonOutput
|
||||
|
||||
// 企业微信群机器人部署通知(成功/失败/中止)。Webhook 完整 URL 存 Jenkins 凭据 WECOM_WEBHOOK_DEPLOY,勿写入 Git。
|
||||
// WeCom group-bot deploy notifications. Store webhook URL in Jenkins Secret text credential only.
|
||||
def notifyWecomDeploy(Map opts) {
|
||||
if ((env.DISABLE_DEPLOY_WECOM_NOTIFY ?: '').equalsIgnoreCase('true')) {
|
||||
echo '[WeCom] DISABLE_DEPLOY_WECOM_NOTIFY=true,跳过通知'
|
||||
return
|
||||
}
|
||||
|
||||
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()
|
||||
|
||||
try {
|
||||
withCredentials([string(credentialsId: credId, variable: 'WECOM_WEBHOOK_URL')]) {
|
||||
def webhookUrl = WECOM_WEBHOOK_URL?.trim()
|
||||
if (!webhookUrl) {
|
||||
echo "[WeCom] 凭据 ${credId} 为空,跳过通知"
|
||||
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')],
|
||||
]
|
||||
writeFile(file: 'wecom-notify-payload.json', encoding: 'UTF-8', text: JsonOutput.toJson(payload))
|
||||
|
||||
def httpCode = sh(
|
||||
returnStdout: true,
|
||||
script: '''
|
||||
set +e
|
||||
code=$(curl -sS -o wecom-notify-response.txt -w "%{http_code}" \
|
||||
-X POST "$WECOM_WEBHOOK_URL" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d @wecom-notify-payload.json)
|
||||
echo "[WeCom] HTTP ${code}" >&2
|
||||
cat wecom-notify-response.txt >&2 || true
|
||||
echo "${code}"
|
||||
'''
|
||||
).trim()
|
||||
|
||||
def resp = readFile('wecom-notify-response.txt').trim()
|
||||
if (httpCode == '200' && resp.contains('"errcode":0')) {
|
||||
echo '[WeCom] 通知已发送'
|
||||
} else {
|
||||
echo "[WeCom] 发送可能失败,HTTP=${httpCode},响应=${resp}"
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
echo "[WeCom] 通知发送异常(不影响构建结果): ${e.message}"
|
||||
}
|
||||
}
|
||||
|
||||
pipeline {
|
||||
agent any
|
||||
|
||||
@@ -242,6 +323,36 @@ pipeline {
|
||||
}
|
||||
echo "[Deploy Test] done"
|
||||
}
|
||||
post {
|
||||
success {
|
||||
script {
|
||||
notifyWecomDeploy(
|
||||
envLabel: '测试环境',
|
||||
host: env.TEST_DEPLOY_HOST,
|
||||
status: 'success',
|
||||
)
|
||||
}
|
||||
}
|
||||
failure {
|
||||
script {
|
||||
notifyWecomDeploy(
|
||||
envLabel: '测试环境',
|
||||
host: env.TEST_DEPLOY_HOST,
|
||||
status: 'failure',
|
||||
)
|
||||
}
|
||||
}
|
||||
aborted {
|
||||
script {
|
||||
notifyWecomDeploy(
|
||||
envLabel: '测试环境',
|
||||
host: env.TEST_DEPLOY_HOST,
|
||||
status: 'failure',
|
||||
extraNote: '阶段已中止(可能为人工取消)',
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stage('Deploy Prod') {
|
||||
@@ -301,6 +412,36 @@ pipeline {
|
||||
}
|
||||
echo "[Deploy Prod] done"
|
||||
}
|
||||
post {
|
||||
success {
|
||||
script {
|
||||
notifyWecomDeploy(
|
||||
envLabel: '生产环境',
|
||||
host: env.PROD_DEPLOY_HOST,
|
||||
status: 'success',
|
||||
)
|
||||
}
|
||||
}
|
||||
failure {
|
||||
script {
|
||||
notifyWecomDeploy(
|
||||
envLabel: '生产环境',
|
||||
host: env.PROD_DEPLOY_HOST,
|
||||
status: 'failure',
|
||||
)
|
||||
}
|
||||
}
|
||||
aborted {
|
||||
script {
|
||||
notifyWecomDeploy(
|
||||
envLabel: '生产环境',
|
||||
host: env.PROD_DEPLOY_HOST,
|
||||
status: 'failure',
|
||||
extraNote: '阶段已中止(可能为未确认生产部署或人工取消)',
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user