Jenkins 飞书消息通知_jenkins

Jenkins 飞书消息通知_作业状态_02

https://open.feishu.cn/open-apis/bot/v2/hook/bba16e19-55e9-4293-8c6b-e23ae2904cd8

 

 

消息通知

机器人概述  


Jenkins 集成实践消息通知

Jenkins 飞书消息通知_作业状态_03

Jenkins 飞书消息通知_群组_04

注意: 为了获取构建用户的名称, 需要安装插件build user vars plugin

Jenkins 飞书消息通知_jenkins_05

完整的Jenkinsfile:

注意: 飞书的body中换行符要用双斜杠;(9499 参数错误)

pipeline{
    agent { label "master"}

    stages{

        stage("Dingding"){
            steps{
                script {
                    wrap([$class: 'BuildUser']){
                            echo "full name is $BUILD_USER"
                            echo "user id is $BUILD_USER_ID"
                            echo "user email is $BUILD_USER_EMAIL"
                            env.BUILD_USER = "${BUILD_USER}"
                    }

                    if ("${env.msgType}" == "dingding") {
                        DingDing("${env.BUILD_USER}")
                    } 

                    if ("${env.msgType}" == "weixin") {
                        WeiXin("${env.BUILD_USER}")
                    }

                    if ("${env.msgType}" == "feishu") {
                        FeiShu("${env.BUILD_USER}")
                    }
                    

                }
            }
        }
    }
}


//飞书

def FeiShu(users){
    sh """
        curl --location --request POST 'https://open.feishu.cn/open-apis/bot/v2/hook/c9dde6d6-d4dc-405c-b1df-2dc2f448f49d' \
        --header 'Content-Type: application/json' \
        --data '{
            "msg_type": "interactive",
            "card": {
                "config": {
                        "wide_screen_mode": true,
                        "enable_forward": true
                },
                "elements": [{
                        "tag": "div",
                        "text": {
                                "content": "## ${JOB_NAME}作业构建信息: \\n### 构建ID: ${BUILD_ID} \\n### 构建人:${users} \\n### 作业状态: ${currentBuild.currentResult} \\n### 运行时长: ${currentBuild.durationString} \\n###### 更多详细信息点击 [构建日志](${BUILD_URL}/console) \\n",
                                "tag": "lark_md"
                        }
                }, {
                        "actions": [{
                                "tag": "button",
                                "text": {
                                        "content": "作业链接",
                                        "tag": "lark_md"
                                },
                                "url": "${JOB_URL}",
                                "type": "default",
                                "value": {}
                        }],
                        "tag": "action"
                }],
                "header": {
                        "title": {
                                "content": "DEVOPS作业构建信息",
                                "tag": "plain_text"
                        }
                }
            }
        } '
    """
}

// 企业微信
def WeiXin(users){
    withCredentials([string(credentialsId: 'b992073c-c06d-4794-b36c-b0a845255977', variable: 'ACCESS_TOKEN')]) {

        sh """
            curl --location --request POST 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=${ACCESS_TOKEN}' \
                --header 'Content-Type: application/json' \
                --data '{
                    "msgtype": "markdown",
                    "markdown": {
                        "content": "## ${JOB_NAME}作业构建信息: \n  ### 构建ID: ${BUILD_ID} \n ### 构建人:${users} \n   ### 作业状态: ${currentBuild.currentResult} \n  ### 运行时长: ${currentBuild.durationString} \n  ###### 更多详细信息点击 [构建日志](${BUILD_URL}/console) \n"
                    }
                }'
        """
    }
}

def DingDing(users){
    withCredentials([string(credentialsId: 'b191eaa5-0bb7-40e2-bb0f-4d3ca8bad72f', variable: 'TOKEN')]) {
        sh """
            curl --location --request POST "https://oapi.dingtalk.com/robot/send?access_token=${TOKEN}" \
                --header 'Content-Type: application/json' \
                --data '{
                    "msgtype": "markdown",
                    "markdown": {
                        "title": "DEVOPS通知",
                        "text": "## ${JOB_NAME}作业构建信息: \n  ### 构建ID: ${BUILD_ID} \n ### 构建人:${users} \n   ### 作业状态: ${currentBuild.currentResult} \n  ### 运行时长: ${currentBuild.durationString} \n  ###### 更多详细信息点击 [构建日志](${BUILD_URL}/console) \n"
                    },
                    "at": {
                        "isAtAll": true
                    }
                }'
            """
    }

}