feishuNotify.groovy 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/env groovy
  2. import com.example.FeishuNotifier
  3. // 公共采集逻辑
  4. @NonCPS
  5. private Map gatherCommonInfo() {
  6. def duration = currentBuild.durationString.replace(' and counting', '')
  7. def commitMsg = sh(returnStdout: true, script: "git log -1 --pretty=%B").trim()
  8. def author = sh(returnStdout: true, script: "git log -1 --pretty=%an").trim()
  9. def commitTime = sh(returnStdout: true, script: "git log -1 --pretty=%ci").trim()
  10. def branch = env.BRANCH_NAME ?: env.GIT_BRANCH ?: 'unknown'
  11. def buildUrl = env.BUILD_URL.replace('http://68.79.46.88:8080', 'https://jenkins.adwebcloud.com')
  12. return [
  13. duration : duration,
  14. commitMsg: commitMsg,
  15. author : author,
  16. commitTime: commitTime,
  17. branch : branch,
  18. buildUrl : buildUrl
  19. ]
  20. }
  21. // 任务开始时调用
  22. def start(String webhookUrl) {
  23. def info = gatherCommonInfo()
  24. new FeishuNotifier(this, webhookUrl).send(
  25. 'START', // 自定义状态
  26. env.JOB_NAME,
  27. env.BUILD_NUMBER,
  28. info.buildUrl,
  29. '0s', // 刚开始,持续时间为 0
  30. info.branch,
  31. info.commitMsg,
  32. info.author,
  33. info.commitTime
  34. )
  35. }
  36. // 任务结束时调用(就是你现在的 call)
  37. def call(String webhookUrl, String status) {
  38. def info = gatherCommonInfo()
  39. new FeishuNotifier(this, webhookUrl).send(
  40. status,
  41. env.JOB_NAME,
  42. env.BUILD_NUMBER,
  43. info.buildUrl,
  44. info.duration,
  45. info.branch,
  46. info.commitMsg,
  47. info.author,
  48. info.commitTime
  49. )
  50. }