Jenkins 管道将文件上传到 Slack

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

我有一个 Jenkins 管道,可以向 Slack 发送警报。现在我想添加将文件上传到 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'
)

执行 slackUploadFile 脚本后,我在 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

jenkins jenkins-pipeline jenkins-plugins slack slack-api
1个回答
0
投票

这可能是插件中的错误,因为我看到松弛端点最近已更改

由于某种原因,它不适用于简单令牌(jenkins ci webhook),但可以使用slack应用程序机器人令牌

我测试了这个:

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”的令牌。

以及通过

https://plugins.jenkins.io/slack/#plugin-content-creating-your-app 配置的 slack 应用程序

还以 @myappname 的形式将应用程序添加到 Slack 频道 -> 是的,邀请应用程序加入频道

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