Skip to content

Jenkins 自定义钉钉机器人消息

Jenkins 自带的钉钉插件固定只能通知 Job 开始,执行成功或失败,如果要自定义消息内容,需要 POST 消息内容到机器人的 webhook 地址。消息格式参照 钉钉开放中心 - 文档中心

这里使用 curl 命令(curl for windows)来 Post 请求。由于我的 Jenkins 是装在 windows 上的,所以命令行的写法有些要注意的地方。

注意点

  1. URL 地址要用双引号包起来,用单引号会报如下错误。其它的参数也需要用双引号包起来。

    bash
    Protocol "'https" not supported or disabled in libcurl
  2. 汉字需要使用 Unicode 格式(即使在 Content-Type 中设置了 charset=utf-8),否则发出来的消息会乱码。找了个在线转码网站 Unicode 编码转换

  3. 消息内容中的双引号需要使用转义符【\】。

示例

bash
curl "https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxx" -H "Content-Type: application/json" -d "{\"msgtype\": \"text\", \"text\": {\"content\": \"\u6211\u5c31\u662f\u6211\u002c\u0020\u662f\u4e0d\u4e00\u6837\u7684\u70df\u706b\"}}"

参考

  1. curl : (1) Protocol https not supported or disabled in libcurl
  2. 钉钉报警