Jenkins配置釘釘通知


Jenkins配置釘釘通知

說起來添加釘釘通知的原理很簡單,釘釘生成一個訂閱地址(API接口),Jenkins相關步驟去調用這個訂閱地址即達到通知效果。

創建釘釘機器人

注意:釘釘機器人需要群主開放權限才能創建。

建群,打開【群設置】->【智能群助手】->【添加機器人】->【自定義 WebHook接入】

安全設置選三者之一。這里我選擇【自定義關鍵詞】方式。

點擊完成后會有一串webhook接口地址,也需記住。

安裝釘釘插件

打開Jenkins的Dashboard,安裝插件。

打開【系統管理】->【插件管理】,安裝"DingTalk"插件並重啟。

打開【系統管理】->【系統配置】翻到“釘釘”配置位置,新增配置。

機器人Id不用填,保存時自動分配,在webhook上添加頂頂機器人接口地址,加密處添加加簽密碼。

點擊測試,釘釘群會收到配置信息。

安裝用戶構建插件

安裝用戶構建插件“build user vars”,安裝步驟同上。

可獲得如下參數

Variable Description
BUILD_USER Full name (first name + last name)
BUILD_USER_FIRST_NAME First name
BUILD_USER_LAST_NAME Last name
BUILD_USER_ID Jenkins user ID
BUILD_USER_GROUPS Jenkins user groups
BUILD_USER_EMAIL Email address

安裝后還要在【系統管理】->【系統配置】滑到【Build User Variables】選項,勾選生效。勾選后直接在全局變量中生效。否則需要用warp包裝使用。

包裝使用

always {
    echo 'This will always run'
    wrap([$class: 'BuildUser']) {
        sh 'echo "${BUILD_USER}"'
    }
}

釘釘機器人開發權限

不確定是否一定要機器人開發權限。我是遇到webhook地址curl失敗的問題,然后讓管理員給了釘釘機器人開發權限。

開通方式參照如下地址。

https://developers.dingtalk.com/document/app#/serverapi2/qf2nxq

流水線配置

在工程中添加Jenkinsfile文件。內容如下:

pipeline {
    agent any
    stages {
        stage("stage 1: Test dingding notify") {
            steps {
                echo 'Test dingding notify'
                script {
                    env.commit = "${sh(script:'git log --oneline --no-merges|head -1', returnStdout: true)}"
                }
            }
            
        }
    }
    post {
        always {
            echo 'This will always run'
            wrap([$class: 'BuildUser']) {
                sh 'echo "${BUILD_USER}"'
                sh """curl 'https://oapi.dingtalk.com/robot/send?access_token=3dfb***9ece'  -H 'Content-Type: application/json' -d '{"msgtype":"text","text":{"content": "Jenkins提醒。部署服務:${JOB_NAME} \n構建分支: ${BRANCH_NAME}\n構建ID: ${BUILD_ID}\n提交信息:${commit}構建狀態:${currentBuild.currentResult}"}}'"""
            }
        }
        success {
            echo 'successful'
        }
        failure {
            echo 'failed'
        }
    }
}

將推送消息放到always里,任何構建都會推送消息。

warp包裝器圈定BuildUser使用范圍。本例安全機制是使用關鍵字的方式,Jenkins作為關鍵字才能推送成功。

currentBuild.currentResult可以打印出結果,詳情可參考https://issues.jenkins.io/browse/JENKINS-56402

注意:文件中的中文可能會亂碼,注意添加轉碼配置。

釘釘消息格式

消息格式參考管網接口參數說明,可以配置純文本、markdown、連接等。結合審計流程制造卡點審批等效果

https://developers.dingtalk.com/document/robots/message-types-and-data-format
text
sh """curl 'https://oapi.dingtalk.com/robot/send?access_token=3dfb***9ece'  -H 'Content-Type: application/json' -d '{"at":{"atMobiles":[153****]},"msgtype":"text","text":{"content": "Jenkins Pipeline. \n:${JOB_NAME} \nBuild branch: ${BRANCH_NAME}\nBuild ID: ${BUILD_ID}\nCommit message:${commit}\nBuild state:${currentBuild.currentResult}"}}'"""
markdown
sh """curl 'https://oapi.dingtalk.com/robot/send?access_token=3dfb***9ece'  -H 'Content-Type: application/json' -d '{"at":{"atMobiles":[153****]},"msgtype":"markdown","markdown":{"title":"Jenkins 構建提醒", "text":"## Jenkins Message \n![](http://47.119.xx.xx/src/assets//img/girls/11.jpg) \n **Build ID**: ${BUILD_ID} \n**Commit message**: ${commit}\n**Build state**: ${currentBuild.currentResult}"}}'"""
actionCard
"""curl 'https://oapi.dingtalk.com/robot/send?access_token=3dfb***9ece'  -H 'Content-Type: application/json' -d '{"at":{"atMobiles":[153***]}, "msgtype":"actionCard","actionCard":{"title":"Jenkins Pipeline.", "text":"## Jenkins Message<br/>![](http://47.119.XX.XX/src/assets//img/girls/15.jpg)<br/>**Build ID**: ${BUILD_ID}<br/>**Commit message**: ${commit}<br/>**Build state**: ${currentBuild.currentResult}<br/>@153","btns":[{"title":"Yes","actionURL":"#"},{"title":"No","actionURL":"#"}]}}'"""

附錄

問題1:No such property: BUILD_USER for class: groovy.lang.Binding

安裝后還要去全局配置中勾選

問題2:{"errcode":310000,"errmsg":"sign not match, more: [https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq]"}

調用webhook接口報錯,原因是自定義機器人安全機制定義了“加簽”,要驗證方式。有三種驗證方式,具提參考釘釘官方說明文檔。https://developers.dingtalk.com/document/robots/customize-robot-security-settings

問題3:中文亂碼

//TODO


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM