onBuildEvent.groovy 1.3 KB

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