luhaowen 1 тиждень тому
батько
коміт
bc9d09a869

+ 51 - 0
src/com/example/feishuNotify.groovy

@@ -0,0 +1,51 @@
+package com.example
+
+import groovy.json.JsonOutput
+import jenkins.model.Jenkins
+
+class FeishuNotifier implements Serializable {
+    def script
+    String webhookUrl
+
+    FeishuNotifier(script, String webhookUrl) {
+        this.script = script
+        this.webhookUrl = webhookUrl
+    }
+
+    void send(String status, String jobName, String buildNumber, String buildUrl, String duration) {
+        def colorMap = [
+            'SUCCESS': 'green',
+            'FAILURE': 'red',
+            'UNSTABLE': 'yellow',
+            'ABORTED': 'gray'
+        ]
+        def title = "Jenkins构建通知"
+        def text = """
+        🚀 **构建状态**: ${status}\n
+        📦 **任务名称**: ${jobName}\n
+        🔢 **构建编号**: #${buildNumber}\n
+        ⏱️ **持续时间**: ${duration}\n
+        🔗 [点击查看详情](${buildUrl})
+        """.stripIndent()
+
+        def payload = [
+            msg_type: "post",
+            content: [
+                post: [
+                    zh_cn: [
+                        title: title,
+                        content: [[ tag: "text", text: text ]]
+                    ]
+                ]
+            ]
+        ]
+
+        script.httpRequest(
+            url: webhookUrl,
+            httpMode: 'POST',
+            requestBody: JsonOutput.toJson(payload),
+            contentType: 'APPLICATION_JSON',
+            consoleLogResponseBody: true
+        )
+    }
+}

+ 0 - 41
vars/feishuEnd.groovy

@@ -1,41 +0,0 @@
-#!/usr/bin/env groovy
-
-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
-    }
-}

+ 15 - 0
vars/feishuNotify.groovy

@@ -0,0 +1,15 @@
+#!/usr/bin/env groovy
+
+import com.example.FeishuNotifier
+
+def call(String webhookUrl, String status) {
+    def duration = currentBuild.durationString.replace(' and counting', '')
+    def notifier = new FeishuNotifier(this, webhookUrl)
+    notifier.send(
+        status,
+        env.JOB_NAME,
+        env.BUILD_NUMBER,
+        env.BUILD_URL,
+        duration
+    )
+}

+ 0 - 41
vars/feishuStart.groovy

@@ -1,41 +0,0 @@
-#!/usr/bin/env groovy
-
-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
-    }
-}