在构建失败时从Jenkins管道通知松弛用户

时间:2020-06-01 06:53:09

标签: jenkins groovy

我正在从Jenkins管道向通道发送松弛通知,我已经安装了Jenkins松弛插件https://plugins.jenkins.io/slack,并配置了Jenkins松弛应用程序在构建失败或成功时将通知发送到该通道。我不只是向失败的通道发送失败消息,而是要通知用户说构建失败。 例如:@user error in deploying following project

我从jenkins松弛插件中引用了此步骤

def userIds = slackUserIdsFromCommitters()
def userIdsString = userIds.collect { "<@$it>" }.join(' ')
post {
        // Send the build result to slack channel
        success {
          slackSend (color:'good', message: "<@$userIds>Successfully deployed")
        }
        failure {
            slackSend (color:'danger', message: "<@$userIds>Error in build ${env.JOB_NAME}")
        }
    }

我得到$userIds变量的空值。

enter image description here

2 个答案:

答案 0 :(得分:2)

如果它仍然与某人相关,我这样做了:

    post {
        always {
            script {
                env.GIT_COMMIT_MSG = sh (script: 'git log -1 --pretty=%B ${GIT_COMMIT}', returnStdout: true).trim()
                env.GIT_AUTHOR = sh (script: 'git log -1 --pretty=%ae ${GIT_COMMIT} | awk -F "@" \'{print $1}\'', returnStdout: true).trim()
                slackSend(
                    color: color_slack_msg(),
                    message: "*${currentBuild.currentResult}:* Job `${env.JOB_NAME}` build `${env.BUILD_DISPLAY_NAME}` by <@${env.GIT_AUTHOR}>\n Build commit: ${GIT_COMMIT}\n Last commit message: '${env.GIT_COMMIT_MSG}'\n More info at: ${env.BUILD_URL}\n Time: ${currentBuild.durationString.minus(' and counting')}",
                    channel: 'CHANNEL',
                    tokenCredentialId: 'TOKEN'
                )
            }
        }
    }

......

def color_slack_msg() {
    def COLOR_MAP = [
        'SUCCESS': 'good', 
        'FAILURE': 'danger',
    ]
    return COLOR_MAP[currentBuild.currentResult]
}

如果邮件 = 在 slack 中登录,这将起作用

答案 1 :(得分:0)

此功能仅适用于处于松弛状态的机器人用户,不适用于Jenkins机器人应用程序。 您创建的机器人用户应该有权向该通道读写消息。