构建失败时从 Jenkins 管道通知 slack 用户

Vin*_*kha 0 groovy jenkins

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

我从 jenkins slack 插件中引用了这个步骤

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}")
        }
    }
Run Code Online (Sandbox Code Playgroud)

我得到$userIds变量的空值。

在此输入图像描述

msk*_*rev 5

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

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

def color_slack_msg() {
    switch(currentBuild.currentResult) {
    case "SUCCESS":
        return "good"
        break
    case "FAILURE":
    case "UNSTABLE":
        return "danger"
        break
    default:
        return "warning"
        break
    }
}

Run Code Online (Sandbox Code Playgroud)

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