如何在 slackSend 中使用凭证

问题描述 投票:0回答:1

因此,我尝试在 Jenkinsfile 中使用 slackSend 在通道上发布构建状态,并且我尝试显式定义这样的所有内容 - (出于某种原因,我必须这样定义它)

WithCredentials([string(credentialsId: 'theSlackToken', variable: 'slackCredentials')]) {
    slackSend (channel: '@johnsmith', message: 'hello there', color: '#3eb991', failOnError: true, teamDomain: 'myteamsubdomain', token: slackCredentials)
}

但是当我在 Jenkinsfile 中使用以上这些行时,我收到错误消息,提示没有这样的 DSL 方法字符串。

请帮助我,我将非常感谢任何建议。

jenkins groovy jenkins-pipeline jenkins-groovy
1个回答
2
投票

这里有几个问题:

  1. 首先要让你的命令起作用,你不应该使用“withCredentials”,你唯一需要的是credentialsId,然后将其设置为tokenCredentialId,如下所示:
    slackSend (channel: '@johnsmith', message: 'hello there', color: '#3eb991', tokenCredentialId: 'theSlackToken')

重要提示:凭据应该是密文!!!

  1. 无需在每个 slackSend 命令中都使用 tokenCredentialId,您可以将其设置为该 Jenkins 的通用 slack 令牌。通过 Jenkins -> 管理 Jenkins -> 配置系统,然后查找 Slack,并输入您要使用的令牌的正确凭据。然后你可以使用:

    slackSend (channel: '@johnsmith', message: 'hello there', color: '#3eb991'

  2. 在 slack 命令中使用 tokenCredentialId 可以覆盖当前的全局令牌,这样您就可以在 Jenkins 内的 slack 应用程序之间切换。

欲了解更多信息: https://plugins.jenkins.io/slack/

© www.soinside.com 2019 - 2024. All rights reserved.