feishuStart.groovy 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #!/usr/bin/env groovy
  2. def call(Map args=[:]) {
  3. String webhook = args.webhook ?: credentialsId('feishu-webhook')
  4. String secret = args.secret ?: ''
  5. def result = currentBuild.currentResult // SUCCESS / FAILURE / ABORTED
  6. def emoji = result == 'SUCCESS' ? '✅' : '❌'
  7. def ts = (System.currentTimeMillis()/1000) as int
  8. def sign = ''
  9. if (secret) {
  10. def mac = javax.crypto.Mac.getInstance("HmacSHA256")
  11. mac.init(new javax.crypto.spec.SecretKeySpec("${ts}\n${secret}".bytes,"HmacSHA256"))
  12. sign = mac.doFinal().encodeBase64().toString()
  13. }
  14. def body = [
  15. msg_type: "interactive",
  16. card: [
  17. header: [title: [tag: "plain_text", content: "${emoji} Jenkins 构建${result}"]],
  18. elements: [
  19. [tag: "div", text: [tag: "lark_md", content:
  20. "**项目**:${env.JOB_NAME} #${env.BUILD_NUMBER}\n**结果**:${result}\n**耗时**:${currentBuild.durationString.replace(' and counting','')}"]],
  21. [tag: "action", actions: [
  22. [tag: "button", type: "primary", text: [tag: "plain_text", content: "查看构建"], url: env.BUILD_URL]
  23. ]]
  24. ]
  25. ]
  26. ]
  27. if (secret) {
  28. body += [timestamp: ts as String, sign: sign]
  29. }
  30. withCredentials([string(credentialsId: webhook, variable: 'URL')]) {
  31. httpRequest url: env.URL,
  32. httpMode: 'POST',
  33. contentType: 'APPLICATION_JSON',
  34. requestBody: groovy.json.JsonOutput.toJson(body),
  35. consoleLogResponseBody: true
  36. }
  37. }