Jenkins管道上传文件到Slack

guz6ccqo  于 12个月前  发布在  Jenkins
关注(0)|答案(1)|浏览(92)

我有一个Jenkins管道,可以向Slack发送警报。现在我想添加上传文件到Slack的功能。但文件没有发送。
这是在向斯莱克传递信息。而且很有效。

slackSend(
    color: color_slack_msg(),
    message: """
        Some message
    """.stripIndent().trim(),
    channel: "${env.SLACK_ID}, ${env.SLACK_NOTIFICATION_ID}",
    tokenCredentialId: 'slack_notification'
)

这是一个上传到Slack的文件。

slackUploadFile (
    channel: '${env.SLACK_METRICS_ID}', 
    credentialId: 'slack_notification', 
    filePath: 'metrics_editor.txt', 
    initialComment: 'Test comment'
)

在执行slackslogadFile脚本后,我在Jenkins日志中看到了以下消息:

Using dirname=some\path\to\project and includeMask=metrics_editor.txt
Adding file some\path\to\project\metrics_editor.txt

但文件不会上传到Slack。
我看到凭据配置正确,因为slackSend正在工作。
我试过这个:

curl -sSL -D - -X POST -F file=@<path_to_file> -F token=<bot_token> https://slack.com/api/files.upload

我收到的答复是:

file=@metrics_editor.txt -F channels=<channel id> -F token=<token>https://slack.com/api/files.upload
HTTP/1.1 200 OK
date: Thu, 21 Sep 2023 14:37:51 GMT
server: Apache
vary: Accept-Encoding
x-slack-req-id: <id>
x-content-type-options: nosniff
x-xss-protection: 0
x-robots-tag: noindex,nofollow
pragma: no-cache
cache-control: private, no-cache, no-store, must-revalidate
expires: Sat, 26 Jul 1997 05:00:00 GMT
content-type: application/json; charset=utf-8
access-control-expose-headers: x-slack-req-id, retry-after
access-control-allow-headers: slack-route, x-slack-version-ts, x-b3-traceid, x-b3-spanid, x-b3-parentspanid, x-b3-sampled, x-b3-flags
strict-transport-security: max-age=31536000; includeSubDomains; preload
x-slack-unique-id: <id>
x-slack-backend: r
referrer-policy: no-referrer
access-control-allow-origin: *
content-length: 35
via: 1.1 slack-prod.tinyspeck.com, envoy-www-iad-maoxshte, envoy-edge-fra-uzwtdosi
x-envoy-attempt-count: 1
x-envoy-upstream-service-time: 101
x-backend: files_normal files_canary_with_overflow files_control_with_overflow
x-server: slack-www-hhvm-files-iad-xskh
x-slack-shared-secret-outcome: no-match
x-edge-backend: envoy-www
x-slack-edge-shared-secret-outcome: no-match

{"ok":false,"error":"invalid_auth"}

我还发现,在Slack中Jenkins应用的设置中,你需要给予权限才能上传文件files:write,但我不知道在哪里可以找到这个权限。我在安装的应用程序中找到了Jenkins,但权限页面是空的。
enter image description here

yqyhoc1h

yqyhoc1h1#

这可能是插件中的bug,因为我看到slack端点最近被更改了
由于某些原因,它不能与简单令牌(jenkins ci webhook)一起工作,但可以与松弛应用程序bot令牌一起工作
我测试了这个:

node {
    sh "echo hey > blah.txt"
    slackUploadFile filePath: "*.txt", channel: "#testing-slack-api", initialComment:  "HEY HEY", credentialsId: "slack-bot-token"
}

其中,slack-bot-token是来自slack应用“Bot User OAuth Token”的令牌。
你可以从代码中传递token,或者在jenkins global settings -> slack和slack app configured via https://plugins.jenkins.io/slack/#plugin-content-creating-your-app中进行全局配置
我还添加了应用程序到松弛频道作为@myappname ->是的,邀请应用程序到频道
您可以通过以下方式找到您的Slack应用程序:
1.转到任何频道,然后单击名称
1.转到集成选项卡
1.按下添加应用程序
1.按下管理应用程序(左上角)
1.点击右上角的Build
1.按下创建新应用程序
1.你可以在文档中找到的所有东西

相关问题