linux+Jenkins+飞书机器人发送通知(带签名)

文章目录

🌞如何使用

自定义机器人使用指南飞书官网https://open.feishu.cn/document/client-docs/bot-v3/add-custom-bot



🌻在linux 上安装python 环境

sh 复制代码
yum install python3 python3-devel python3-pip
sh 复制代码
# 请求工具
python3 -m pip install requests

🌻发送消息python脚本

我这个设置了签名的,具体怎么设置签名可以查看飞书官方文档

python 复制代码
#!/usr/bin/env python
# -*- encoding:utf-8 -*-

import sys

import requests

import time
import hashlib
import base64
import hmac
import calendar


def gen_sign(timestamp, secret):
    string_to_sign = '{}\n{}'.format(timestamp, secret)
    hmac_code = hmac.new(string_to_sign.encode("utf-8"), digestmod=hashlib.sha256).digest()

    # 对结果进行base64处理
    sign = base64.b64encode(hmac_code).decode('utf-8')

    return sign


JOB_URL = sys.argv[1]
JOB_NAME = sys.argv[2]
BUILD_NUMBER = sys.argv[3]
isFinish = "开始构建" if str(sys.argv[4]) == str(0) else "构建完成"
timestamp = str(calendar.timegm(time.gmtime()))
currenttime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
sign_key = '你的签名'
sgin = gen_sign(timestamp, sign_key)
url = '你的webhook 地址'
method = 'post'
headers = {
    'Content-Type': 'application/json'
}
json = {
    "timestamp": "" + timestamp + "",
    "msg_type": "interactive",
    "sign": "" + sgin + "",
    "card": {
        "config": {
            "wide_screen_mode": True,
            "enable_forward": True
        },
        "elements": [{
            "tag": "div",
            "text": {
                "content": "项目名称:" + JOB_NAME + "\n构建编号:第" + BUILD_NUMBER + "次构建\n运行时间:" + currenttime,
                "tag": "lark_md"
            }
        }, {
            "actions": [{
                "tag": "button",
                "text": {
                    "content": "查看报告",
                    "tag": "lark_md"
                },
                "url": JOB_URL,
                "type": "default",
                "value": {}
            }],
            "tag": "action"
        }],
        "header": {
            "title": {
                "content": JOB_NAME + " "+isFinish+"",
                "tag": "plain_text"
            }
        }
    }
}
requests.request(method=method, url=url, headers=headers, json=json)

🦋把脚本上传倒linux上

可以看到上传去是没有执行权限的,一定要记得叫上执行权限

sh 复制代码
chmod +x sendMsg.py

🦋jenkins 上执行脚本

sh 复制代码
# 最后一个参数标识 0 开始构建 1 构建完成
# /opt/java/script/sendMsg.py这一段不要跟着我写 根据自己存放的位置和名字来
python3 /opt/java/script/sendMsg.py $JOB_URL $JOB_NAME $BUILD_NUMBER 0
相关推荐
猫猫的小茶馆17 小时前
【ARM】BootLoader(Uboot)介绍
linux·汇编·arm开发·单片机·嵌入式硬件·mcu·架构
Yeliang Wu18 小时前
LLaMA-Factory 模型评估理论与实战:基于 Ubuntu 22.04 的系统化指南
linux·ubuntu·llama·评估·llamafactory
生信大表哥18 小时前
单细胞测序分析(十一)轨迹分析
linux·rstudio·数信院生信服务器·生信云服务器
躺着听Jay18 小时前
【1267 - Illegal mix of collations 】mysql报错解决记录
java·linux·前端
不秃头的帅哥18 小时前
程序地址空间(基于c++和linxu的一些个人笔记
linux·开发语言·c++·操作系统·内存空间
Yweir18 小时前
Linux性能监控的工具集和分析命令工具
java·linux·jvm
XH-hui18 小时前
【打靶日记】群内靶机Monkey
linux·网络安全
赖small强18 小时前
【Linux C/C++开发】C语言函数深度技术指南 (Deep Dive into C Functions)
linux·c语言·函数指针·stack frame
XH-hui18 小时前
【打靶日记】群内靶机Alluser
linux·网络安全
4t4run18 小时前
21、Linux常用命令-进程内存CPU相关命令
linux·运维·服务器