diff --git a/vars/notifyLark.groovy b/vars/notifyLark.groovy new file mode 100644 index 0000000..95dbb01 --- /dev/null +++ b/vars/notifyLark.groovy @@ -0,0 +1,58 @@ +def call(Map args = [:]) { + + // ===== 参数 ===== + def publishEnvs = args.publishEnvs ?: 'production' + def branchName = args.branchName ?: (env.BRANCH_NAME ?: 'unknown') + + // ===== Jenkins 内建上下文 ===== + def buildStatus = currentBuild.currentResult ?: 'UNKNOWN' + def buildNumber = env.BUILD_NUMBER + def buildUrl = env.BUILD_URL + def jobName = env.JOB_NAME + def triggerBy = env.BUILD_USER ?: 'unknown' + + // ===== 状态标签 ===== + def statusTag = [ + SUCCESS : "通过", + FAILURE : "失败", + UNSTABLE: "不稳定", + ABORTED : "已中止" + ][buildStatus] ?: "未知状态" + + def commitDiff = "[查看变更记录](${buildUrl}changes)" + + // ===== 凭证 ===== + withCredentials([ + string(credentialsId: 'FEISHU_WEBHOOK', variable: 'FEISHU_WEBHOOK') + ]) { + + def payload = [ + msg_type: 'interactive', + card: [ + type: 'template', + data: [ + template_id: 'AAq2iv39m3lv5', + template_version_name: '1.0.5', + template_variable: [ + build_title : jobName, + status_tag : statusTag, + build_number: buildNumber, + publish_envs: publishEnvs, + commit_diff : commitDiff, + build_branch: branchName, + trigger_by : triggerBy, + build_url : [ url: buildUrl ] + ] + ] + ] + ] + + httpRequest( + httpMode: 'POST', + contentType: 'APPLICATION_JSON', + url: env.FEISHU_WEBHOOK, + requestBody: groovy.json.JsonOutput.toJson(payload), + validResponseCodes: '200:299' + ) + } +}