onBuildEvent.groovy 1.5 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. // 2. 取环境变量
  9. String jobName = env.JOB_NAME
  10. String buildNum = env.BUILD_NUMBER
  11. String branch = env.GIT_BRANCH ?: 'unknown'
  12. String result = currentBuild.currentResult ?: 'UNKNOWN'
  13. String buildUrl = env.BUILD_URL
  14. String duration = phase == 'end' ? "${currentBuild.durationString}" : '-'
  15. String headerColor = phase == 'start' ? 'blue' : (result == 'SUCCESS' ? 'green' : 'red')
  16. def card = [
  17. header: [
  18. title: [tag: "plain_text", content: "Jenkins ${phase == 'start' ? '开始' : '结束'}通知"],
  19. template: headerColor
  20. ],
  21. elements: [
  22. [
  23. tag: "div",
  24. text: [
  25. tag: "lark_md",
  26. content: "**项目:** ${jobName}\n" +
  27. "**分支:** ${branch}\n" +
  28. "**构建:** #${buildNum}\n" +
  29. "**结果:** ${result}\n" +
  30. "**耗时:** ${duration}\n" +
  31. "**链接:** [查看详情](${buildUrl})"
  32. ]
  33. ]
  34. ]
  35. ]
  36. // 3. 发送
  37. feishu.send(webhook, card)
  38. }