luhaowen 6 өдөр өмнө
parent
commit
6af09f513c

+ 9 - 3
src/com/example/FeishuNotifier.groovy

@@ -13,19 +13,25 @@ class FeishuNotifier implements Serializable {
 
 
     void send(String status, String jobName, String buildNumber, String buildUrl,
     void send(String status, String jobName, String buildNumber, String buildUrl,
               String duration, String branch, String commitMsg, String author,
               String duration, String branch, String commitMsg, String author,
-              String commitTime) {              // 新增参数
+              String commitTime) {
+
+        // 根据状态决定标题颜色
+        def title = (status == 'START') ? '🔵 构建开始' :
+                    (status == 'SUCCESS') ? '✅ 构建成功' :
+                    (status == 'FAILURE') ? '❌ 构建失败' : '🟡 构建不稳定'
+
         def payload = [
         def payload = [
             msg_type: 'text',
             msg_type: 'text',
             content: [
             content: [
                 text: """
                 text: """
-                构建状态: ${status}
+                ${title}
                 任务名称: ${jobName}
                 任务名称: ${jobName}
                 构建编号: #${buildNumber}
                 构建编号: #${buildNumber}
                 持续时间: ${duration}
                 持续时间: ${duration}
                 分支: ${branch}
                 分支: ${branch}
                 提交: ${commitMsg}
                 提交: ${commitMsg}
                 作者: ${author}
                 作者: ${author}
-                提交时间: ${commitTime}        // 新增
+                提交时间: ${commitTime}
                 查看详情: ${buildUrl}
                 查看详情: ${buildUrl}
                 """.stripIndent()
                 """.stripIndent()
             ]
             ]

+ 38 - 12
vars/feishuNotify.groovy

@@ -1,27 +1,53 @@
 #!/usr/bin/env groovy
 #!/usr/bin/env groovy
 import com.example.FeishuNotifier
 import com.example.FeishuNotifier
 
 
-def call(String webhookUrl, String status) {
-    // 1. 采集常规信息
+// 公共采集逻辑
+@NonCPS
+private Map gatherCommonInfo() {
     def duration = currentBuild.durationString.replace(' and counting', '')
     def duration = currentBuild.durationString.replace(' and counting', '')
     def commitMsg  = sh(returnStdout: true, script: "git log -1 --pretty=%B").trim()
     def commitMsg  = sh(returnStdout: true, script: "git log -1 --pretty=%B").trim()
     def author     = sh(returnStdout: true, script: "git log -1 --pretty=%an").trim()
     def author     = sh(returnStdout: true, script: "git log -1 --pretty=%an").trim()
     def commitTime = sh(returnStdout: true, script: "git log -1 --pretty=%ci").trim()
     def commitTime = sh(returnStdout: true, script: "git log -1 --pretty=%ci").trim()
-    def branch     = env.brach ?: env.GIT_BRANCH ?: env.BRANCH_NAME ?: 'unknown'
+    def branch     = env.BRANCH_NAME ?: env.GIT_BRANCH ?: 'unknown'
+    def buildUrl   = env.BUILD_URL.replace('http://68.79.46.88:8080', 'https://jenkins.adwebcloud.com')
+    return [
+        duration : duration,
+        commitMsg: commitMsg,
+        author   : author,
+        commitTime: commitTime,
+        branch   : branch,
+        buildUrl : buildUrl
+    ]
+}
 
 
-    // 2. 把原始 BUILD_URL 改成走 Nginx 代理的 https 域名
-    def buildUrl = env.BUILD_URL.replace('http://68.79.46.88:8080', 'https://jenkins.adwebcloud.com')
+// 任务开始时调用
+def start(String webhookUrl) {
+    def info = gatherCommonInfo()
+    new FeishuNotifier(this, webhookUrl).send(
+        'START',                // 自定义状态
+        env.JOB_NAME,
+        env.BUILD_NUMBER,
+        info.buildUrl,
+        '0s',                   // 刚开始,持续时间为 0
+        info.branch,
+        info.commitMsg,
+        info.author,
+        info.commitTime
+    )
+}
 
 
-    // 3. 发送飞书消息
+// 任务结束时调用(就是你现在的 call)
+def call(String webhookUrl, String status) {
+    def info = gatherCommonInfo()
     new FeishuNotifier(this, webhookUrl).send(
     new FeishuNotifier(this, webhookUrl).send(
         status,
         status,
         env.JOB_NAME,
         env.JOB_NAME,
         env.BUILD_NUMBER,
         env.BUILD_NUMBER,
-        buildUrl,   
-        duration,
-        branch,
-        commitMsg,
-        author,
-        commitTime
+        info.buildUrl,
+        info.duration,
+        info.branch,
+        info.commitMsg,
+        info.author,
+        info.commitTime
     )
     )
 }
 }