onBuildEvent.groovy 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #!/usr/bin/env groovy
  2. def call(String phase) {
  3. // ===== 日志:确认加载的是新代码 =====
  4. println "[feishu-card] onBuildEvent phase=${phase} , loaded from ${getClass().protectionDomain.codeSource.location}"
  5. // =====================================
  6. // 1. 先把凭据解析成真正的 URL 字符串
  7. String webhook = credentials('feishu-webhook')
  8. println "[feishu-card] resolved webhook=${webhook}"
  9. // 2. 取环境变量
  10. String jobName = env.JOB_NAME
  11. String buildNum = env.BUILD_NUMBER
  12. String branch = env.GIT_BRANCH ?: 'unknown'
  13. String result = currentBuild.currentResult ?: 'UNKNOWN'
  14. String buildUrl = env.BUILD_URL
  15. String duration = phase == 'end' ? "${currentBuild.durationString}" : '-'
  16. String headerColor = phase == 'start' ? 'blue' : (result == 'SUCCESS' ? 'green' : 'red')
  17. def card = [
  18. header: [
  19. title: [tag: "plain_text", content: "Jenkins ${phase == 'start' ? '开始' : '结束'}通知"],
  20. template: headerColor
  21. ],
  22. elements: [
  23. [
  24. tag: "div",
  25. text: [
  26. tag: "lark_md",
  27. content: "**项目:** ${jobName}\n" +
  28. "**分支:** ${branch}\n" +
  29. "**构建:** #${buildNum}\n" +
  30. "**结果:** ${result}\n" +
  31. "**耗时:** ${duration}\n" +
  32. "**链接:** [查看详情](${buildUrl})"
  33. ]
  34. ]
  35. ]
  36. ]
  37. // 3. 发送
  38. feishu.send(webhook, card)
  39. }