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
相关推荐
凡人叶枫9 小时前
Effective C++ 条款24:若所有参数皆须要类型转换,请为此采用 non-member 函数
linux·前端·c++·算法·嵌入式开发
零陵上将军_xdr9 小时前
Shell流程控制:if/case/for/while让脚本活起来
linux·运维·服务器
坤昱9 小时前
cfs调度类深入解刨——pelt细节篇
linux·linux内核·cfs调度·eevdf·cfs调度类深入解刨·pelt·pelt细节篇
故渊at10 小时前
第十二板块:Android 系统启动与初始化 | 第二十九篇:Init 进程、RC 脚本与属性服务(Property Service)
android·linux·内存映射·权限控制·init进程·rc脚本·属性服务
某林21210 小时前
ROS2 并行编译死锁与 Linux 后台声卡/提权踩坑实录:大型轮足机器人架构复盘
linux·架构·机器人·iassc
无足鸟ICT10 小时前
【RHCA+】末行模式
linux
拼搏的小浣熊10 小时前
【通用教程】Windows\+Linux\+银河麒麟系统 固定静态IP地址|解决打印机扫描IP变动、网络掉线问题
linux·网络·windows·麒麟·固定ip·麒麟系统·统信系统
小生不才yz10 小时前
Shell脚本精读 · S02-02 | 转义、续行与注释
linux
zzqssliu10 小时前
Next.js图片自适应压缩:跨境站点图片加载提速代码方案
linux·javascript·ubuntu
苏宸啊10 小时前
IPC(二)Syestem V
linux