feishuNotify.groovy 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package com.example
  2. import groovy.json.JsonOutput
  3. import jenkins.model.Jenkins
  4. class FeishuNotifier implements Serializable {
  5. def script
  6. String webhookUrl
  7. FeishuNotifier(script, String webhookUrl) {
  8. this.script = script
  9. this.webhookUrl = webhookUrl
  10. }
  11. void send(String status, String jobName, String buildNumber, String buildUrl, String duration) {
  12. def colorMap = [
  13. 'SUCCESS': 'green',
  14. 'FAILURE': 'red',
  15. 'UNSTABLE': 'yellow',
  16. 'ABORTED': 'gray'
  17. ]
  18. def title = "Jenkins构建通知"
  19. def text = """
  20. 🚀 **构建状态**: ${status}\n
  21. 📦 **任务名称**: ${jobName}\n
  22. 🔢 **构建编号**: #${buildNumber}\n
  23. ⏱️ **持续时间**: ${duration}\n
  24. 🔗 [点击查看详情](${buildUrl})
  25. """.stripIndent()
  26. def payload = [
  27. msg_type: "post",
  28. content: [
  29. post: [
  30. zh_cn: [
  31. title: title,
  32. content: [[ tag: "text", text: text ]]
  33. ]
  34. ]
  35. ]
  36. ]
  37. script.httpRequest(
  38. url: webhookUrl,
  39. httpMode: 'POST',
  40. requestBody: JsonOutput.toJson(payload),
  41. contentType: 'APPLICATION_JSON',
  42. consoleLogResponseBody: true
  43. )
  44. }
  45. }