粮草机器人 Lark Notice Plugin(支持飞书/Lark/钉钉)

有两种主流做法,按场景选一个就行:


---

方案一:安装 Jenkins 插件(推荐,最简单)


推荐插件:Lark Notice Plugin(支持飞书/Lark/钉钉)[citation:web:51.2][citation:web:51.14]


1. 安装插件

Jenkins → 系统管理 → 插件管理 → 搜索 Lark Notice Plugin 安装


2. 飞书群添加机器人

打开飞书群 → 设置 → 群机器人 → 添加机器人 → Webhook 机器人

复制 Webhook 地址,格式:https://open.feishu.cn/open-apis/bot/v2/hook/xxxxxxxx


3. Jenkins 全局配置

系统管理 → 系统配置 → 找到 Lark Notice 配置页 → 填写 Webhook URL


4. Pipeline 中使用


pipeline {

 agent any

 stages {

 stage('Build') {

 steps {

 echo 'Building...'

 }

 }

 }

 post {

 success {

 larkNotice(

 webhookUrl: 'https://open.feishu.cn/open-apis/bot/v2/hook/xxxxx',

 message: "✅ ${env.JOBNAME} 构建成功\n构建编号: ${env.BUILDNUMBER}\n查看详情: ${env.BUILD_URL}"

 )

 }

 failure {

 larkNotice(

 webhookUrl: 'https://open.feishu.cn/open-apis/bot/v2/hook/xxxxx',

 message: "❌ ${env.JOBNAME} 构建失败\n构建编号: ${env.BUILDNUMBER}\n查看详情: ${env.BUILD_URL}"

 )

 }

 }

}


---

方案二:Python 脚本 + Webhook(自定义程度最高)


不用装插件,只需在 Jenkins 服务器上放一个 Python 脚本[citation:web:51.8][citation:web:51.12]:


1. feishu_notify.py


#!/usr/bin/env python3

import sys

import requests

import json


webhook_url = 'https://open.feishu.cn/open-apis/bot/v2/hook/xxxxxxxx'


job_name = sys.argv[1]

build_number = sys.argv[2]

build_url = sys.argv[3]

build_status = sys.argv[4] # SUCCESS / FAILURE


color = "#389e0d" if build_status == "SUCCESS" else "#f5222d"

emoji = "✅" if build_status == "SUCCESS" else "❌"


msg = {

 "msg_type": "interactive",

 "card": {

 "header": {

 "title": {"tag": "plaintext", "content": f"{emoji} {jobname} 构建{build_status}"},

 "template": "green" if build_status == "SUCCESS" else "red"

 },

 "elements": [

 {"tag": "div", "text": {"tag": "larkmd", "content": f"构建编号:{buildnumber}\n状态:{build_status}"}},

 {"tag": "hr"},

 {"tag": "action", "actions": [

 {"tag": "button", "text": {"tag": "plain_text", "content": "📄 查看构建报告"},

 "url": build_url, "type": "default"}

 ]}

 ]

 }

}


resp = requests.post(webhook_url, json=msg)

print(resp.json())


2. Jenkins 构建后执行


在 Job 配置 → 构建后操作 → Execute shell,添加:


python3 /path/to/feishunotify.py "$JOBNAME" "$BUILDNUMBER" "$BUILDURL" "$BUILD_STATUS"

注意:自由风格项目需要安装 Post Build Task 插件来获取构建状态变量。


---

方案三:Pipeline 中直接用 curl(最轻量,无依赖)


pipeline {

 agent any

 stages {

 stage('Build') { steps { echo 'Building...' } }

 }

 post {

 success {

 script {

 def webhookUrl = 'https://open.feishu.cn/open-apis/bot/v2/hook/xxxxx'

 def msg = """

 {

 "msg_type": "interactive",

 "card": {

 "header": {"title": {"tag": "plaintext", "content": "✅ ${env.JOBNAME} 构建成功"}},

 "elements":

 {"tag": "div", "text": {"tag": "larkmd", "content": "构建编号: ${env.BUILDNUMBER}\n[查看详情"}}

 ]

 }

 }

 """

 sh "curl -s -X POST -H 'Content-Type: application/json' -d '${msg}' ${webhookUrl}"

 }

 }

 }

}


---

三种方案对比


| 方案 | 优点 | 缺点 |

|:----:|:----|:----|

| 插件(推荐) | UI 配置,开箱即用,支持多种触发时机 | 需要装插件 |

| Python 脚本 | 灵活度高,可做复杂卡片,易调试 | 需要维护脚本文件 |

| curl 直调 | 零依赖,Pipeline 里一行搞定 | 消息格式拼接麻烦,不适合复杂卡片 |


建议: 如果是新项目,直接装 Lark Notice Plugin 最省事。如果需要定制消息卡片样式(颜色、按钮、@人),用 Python 脚本更灵活。