Bladeren bron

Initial commit8

luhaowen 1 week geleden
bovenliggende
commit
e56c3ed35b
4 gewijzigde bestanden met toevoegingen van 78 en 76 verwijderingen
  1. 0 36
      src/org/feishu/FeishuNotifier.groovy
  2. 0 14
      src/org/feishu/SignUtil.groovy
  3. 39 14
      vars/feishuEnd.groovy
  4. 39 12
      vars/feishuStart.groovy

+ 0 - 36
src/org/feishu/FeishuNotifier.groovy

@@ -1,36 +0,0 @@
-package org.feishu
-import groovy.json.JsonOutput
-def send(String webhook,
-         String secret,
-         String title,
-         String text,
-         String btnTitle = '查看构建',
-         String btnUrl   = env.BUILD_URL) {
-
-    def signObj = secret ? new SignUtil().genSign(secret) : [:]
-    def card = [
-        msg_type: "interactive",
-        card: [
-            header: [
-                title: [tag: "plain_text", content: title]
-            ],
-            elements: [
-                [tag: "div", text: [tag: "lark_md", content: text]],
-                [tag: "hr"],
-                [tag: "action", actions: [
-                    [tag: "button", type: "primary", text: [tag: "plain_text", content: btnTitle],
-                     url: btnUrl]
-                ]]
-            ]
-        ]
-    ]
-    if (signObj) {
-        card += [timestamp: signObj.timestamp as String, sign: signObj.sign]
-    }
-    httpRequest consoleLogResponseBody: true,
-                contentType: 'APPLICATION_JSON',
-                httpMode: 'POST',
-                requestBody: JsonOutput.toJson(card),
-                url: webhook,
-                timeout: 30
-}

+ 0 - 14
src/org/feishu/SignUtil.groovy

@@ -1,14 +0,0 @@
-package org.feishu
-@Grab('commons-codec:commons-codec:1.15')
-import org.apache.commons.codec.binary.Base64
-import javax.crypto.Mac
-import javax.crypto.spec.SecretKeySpec
-
-def genSign(String secret) {
-    def ts = System.currentTimeMillis() / 1000 as int
-    def stringToSign = "$ts\n$secret"
-    def mac = Mac.getInstance("HmacSHA256")
-    mac.init(new SecretKeySpec(stringToSign.bytes, "HmacSHA256"))
-    def sign = Base64.encodeBase64String(mac.doFinal())
-    return [timestamp: ts, sign: sign]
-}

+ 39 - 14
vars/feishuEnd.groovy

@@ -1,16 +1,41 @@
 #!/usr/bin/env groovy
-def call(String secret = '') {
-    def notifier = new org.feishu.FeishuNotifier()
-    def status = currentBuild.currentResult  // SUCCESS/FAILED/ABORTED
-    def emoji  = status == 'SUCCESS' ? '✅' : '❌'
-    def msg = """**构建结束**  
-项目:${env.JOB_NAME} #${env.BUILD_NUMBER}  
-结果:**${status}**  
-耗时:${currentBuild.durationString.replace(' and counting', '')}"""
-    notifier.send(
-        webhook: env.FEISHU_WEBHOOK,
-        secret : secret,
-        title  : "$emoji Jenkins 构建$status",
-        text   : msg
-    )
+
+def call(Map args=[:]) {
+    String webhook = args.webhook ?: credentialsId('feishu-webhook')
+    String secret  = args.secret  ?: ''
+
+    def result  = currentBuild.currentResult          // SUCCESS / FAILURE / ABORTED
+    def emoji   = result == 'SUCCESS' ? '✅' : '❌'
+    def ts = (System.currentTimeMillis()/1000) as int
+    def sign = ''
+    if (secret) {
+        def mac = javax.crypto.Mac.getInstance("HmacSHA256")
+        mac.init(new javax.crypto.spec.SecretKeySpec("${ts}\n${secret}".bytes,"HmacSHA256"))
+        sign = mac.doFinal().encodeBase64().toString()
+    }
+
+    def body = [
+        msg_type: "interactive",
+        card: [
+            header: [title: [tag: "plain_text", content: "${emoji} Jenkins 构建${result}"]],
+            elements: [
+                [tag: "div", text: [tag: "lark_md", content:
+                 "**项目**:${env.JOB_NAME} #${env.BUILD_NUMBER}\n**结果**:${result}\n**耗时**:${currentBuild.durationString.replace(' and counting','')}"]],
+                [tag: "action", actions: [
+                    [tag: "button", type: "primary", text: [tag: "plain_text", content: "查看构建"], url: env.BUILD_URL]
+                ]]
+            ]
+        ]
+    ]
+    if (secret) {
+        body += [timestamp: ts as String, sign: sign]
+    }
+
+    withCredentials([string(credentialsId: webhook, variable: 'URL')]) {
+        httpRequest url: env.URL,
+                    httpMode: 'POST',
+                    contentType: 'APPLICATION_JSON',
+                    requestBody: groovy.json.JsonOutput.toJson(body),
+                    consoleLogResponseBody: true
+    }
 }

+ 39 - 12
vars/feishuStart.groovy

@@ -1,14 +1,41 @@
 #!/usr/bin/env groovy
-def call(String secret = '') {
-    def notifier = new org.feishu.FeishuNotifier()
-    def msg = """**开始构建**  
-项目:${env.JOB_NAME}  
-构建编号:#${env.BUILD_NUMBER}  
-启动人:${currentBuild.getBuildCauses()[0].userId}"""
-    notifier.send(
-        webhook: env.FEISHU_WEBHOOK,
-        secret : secret,
-        title  : "🔔 Jenkins 构建开始",
-        text   : msg
-    )
+
+def call(Map args=[:]) {
+    String webhook = args.webhook ?: credentialsId('feishu-webhook')
+    String secret  = args.secret  ?: ''
+
+    def result  = currentBuild.currentResult          // SUCCESS / FAILURE / ABORTED
+    def emoji   = result == 'SUCCESS' ? '✅' : '❌'
+    def ts = (System.currentTimeMillis()/1000) as int
+    def sign = ''
+    if (secret) {
+        def mac = javax.crypto.Mac.getInstance("HmacSHA256")
+        mac.init(new javax.crypto.spec.SecretKeySpec("${ts}\n${secret}".bytes,"HmacSHA256"))
+        sign = mac.doFinal().encodeBase64().toString()
+    }
+
+    def body = [
+        msg_type: "interactive",
+        card: [
+            header: [title: [tag: "plain_text", content: "${emoji} Jenkins 构建${result}"]],
+            elements: [
+                [tag: "div", text: [tag: "lark_md", content:
+                 "**项目**:${env.JOB_NAME} #${env.BUILD_NUMBER}\n**结果**:${result}\n**耗时**:${currentBuild.durationString.replace(' and counting','')}"]],
+                [tag: "action", actions: [
+                    [tag: "button", type: "primary", text: [tag: "plain_text", content: "查看构建"], url: env.BUILD_URL]
+                ]]
+            ]
+        ]
+    ]
+    if (secret) {
+        body += [timestamp: ts as String, sign: sign]
+    }
+
+    withCredentials([string(credentialsId: webhook, variable: 'URL')]) {
+        httpRequest url: env.URL,
+                    httpMode: 'POST',
+                    contentType: 'APPLICATION_JSON',
+                    requestBody: groovy.json.JsonOutput.toJson(body),
+                    consoleLogResponseBody: true
+    }
 }