onBuildEvent.groovy 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/usr/bin/env groovy
  2. def call(String phase) {
  3. // 先读凭据
  4. String webhook = credentials('feishu-webhook')
  5. // 组装卡片(同上,略)
  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. String headerIcon = phase == 'start' ? 'rocket' : (result == 'SUCCESS' ? 'check_circle' : 'error')
  14. def card = [
  15. header: [
  16. title: [ tag: "plain_text", content: "Jenkins ${phase == 'start' ? '开始' : '结束'}通知" ],
  17. template: headerColor
  18. ],
  19. elements: [
  20. [
  21. tag: "div",
  22. text: [
  23. tag: "lark_md",
  24. content: "**项目:** ${jobName}\n" +
  25. "**分支:** ${branch}\n" +
  26. "**构建:** #${buildNum}\n" +
  27. "**结果:** ${result}\n" +
  28. "**耗时:** ${duration}\n" +
  29. "**链接:** [查看详情](${buildUrl})"
  30. ]
  31. ]
  32. ]
  33. ]
  34. // 把真正的 url 传进去
  35. feishu.send(webhook, card)
  36. }