Jenkins构建后发送企业微信消息提醒

1,638 阅读1分钟

一般会用Jenkins进行打包,如果打包失败能及时通知开发人员,就会非常方便。
话不多说,直接开整。
发送markdown格式的通知,具体内容可自行调整。
企业微信机器人的key要替换为自己的企业微信群的机器人的key。

image.png

脚本如下:
使用pipeline:

post {
        success {
            script {
                def builduser=''
                def buildUserNameId =''
                wrap([$class: 'BuildUser']) {
                    builduser = env.BUILD_USER
                    builduser = builduser.replaceAll("\\s", "\" \"")
                }

                if (params.MODULE_NAME.isEmpty()) {
                    MODULE_NAME = "无"
                }

                def endtime = new Date().format('yyyy-MM-dd HH:mm:ss')

                def jsonstr = '{"markdown":{"content":"<font color=\\"info\\">【' + PROJECT_NAME + '】<\\/font>test环境项目构建<font color=\\"info\\">成功<\\/font>,服务重启中,3~5分钟后可验证\\n ' +
                        '>分支:<font color=\\"comment\\">' + BRANCH_NAME + '<\\/font>\\n' +
                        '>模块:<font color=\\"comment\\">' + MODULE_NAME + '<\\/font>\\n ' +
                        '>构建人:<font color=\\"comment\\">' + builduser + '<\\/font>\\n ' +
                        '>构建完成时间:<font color=\\"comment\\">' + endtime + '<\\/font>\\n ' +
                '>"},"msgtype":"markdown"}'

                echo "httpRequest jsonstr:" + jsonstr


                def response2 = httpRequest \
                customHeaders: [[name: 'Content-Type', value: 'application/json;charset=UTF-8']],
                httpMode: "POST",
                ignoreSslErrors: true,
                requestBody: jsonstr,
                url: "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxxxx"
                println response2.content
            }
        }
        failure{
            script {
                def builduser=''
                def buildUserNameId =''
                wrap([$class: 'BuildUser']) {
                    builduser = env.BUILD_USER
                    builduser = builduser.replaceAll("\\s", "\" \"")
                    buildUserNameId = env.BUILD_USER_ID
                }

                if (params.MODULE_NAME.isEmpty()) {
                    MODULE_NAME = "无"
                }

                def endtime = new Date().format('yyyy-MM-dd HH:mm:ss')

                def jsonstr = '{"markdown":{"content":"<font color=\\"warning\\">【' + PROJECT_NAME + '】<\\/font>test环境镜像构建<font color=\\"warning\\">失败<\\/font>,请查看jenkins日志分析失败原因\\n ' +
                        '>分支:<font color=\\"comment\\">develop<\\/font>\\n ' +
                        '>模块:<font color=\\"comment\\">' + MODULE_NAME + '<\\/font>\\n ' +
                        '>构建人:<font color=\\"comment\\">' + builduser + '<\\/font>\\n ' +
                        '>构建完成时间:<font color=\\"comment\\">' + endtime + '<\\/font> \\n' +
                '>"},"msgtype":"markdown"}'

                echo "httpRequest jsonstr:" + jsonstr


                def response2 = httpRequest \
                customHeaders: [[name: 'Content-Type', value: 'application/json;charset=UTF-8']],
                httpMode: "POST",
                ignoreSslErrors: true,
                requestBody: jsonstr,
                url: "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxx"
                println response2.content
            }
        }
    }

效果如下:

image.png