I have a Jenkins pipeline that sends an alert to Slack. Now I want to add the ability to upload files to Slack. But the file is not sent.
This is sending messages to slack. And it works.
slackSend(
color: color_slack_msg(),
message: """
Some message
""".stripIndent().trim(),
channel: "${env.SLACK_ID}, ${env.SLACK_NOTIFICATION_ID}",
tokenCredentialId: 'slack_notification'
)
This is a file upload to Slack.
slackUploadFile (
channel: '${env.SLACK_METRICS_ID}',
credentialId: 'slack_notification',
filePath: 'metrics_editor.txt',
initialComment: 'Test comment'
)
After executing the slackUploadFile script, I see the message in the Jenkins logs:
Using dirname=some\path\to\project and includeMask=metrics_editor.txt
Adding file some\path\to\project\metrics_editor.txt
But the file is not uploaded to Slack.
I see that Credential is configured correctly since slackSend is working.
I tried this:
curl -sSL -D - -X POST -F file=@<path_to_file> -F token=<bot_token> https://slack.com/api/files.upload
In response I receive:
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"}
I also found that in the settings of the Jenkins application in Slack you need to give permissions to upload files files:write
, but I don't know where to find this permission.
I found Jenkins in Installed Applications, but the permissions page is empty.
it might be bug in plugin, as i see slack endpoints were changed recently
it doesnt work with simple token(jenkins ci webhook) by some reason but works with slack application bot token
i tested this:
node {
sh "echo hey > blah.txt"
slackUploadFile filePath: "*.txt", channel: "#testing-slack-api", initialComment: "HEY HEY", credentialId: "slack-bot-token"
}
where slack-bot-token is token from slack app "Bot User OAuth Token".
you can pass token from code, or configure it globally in jenkins global settings -> slack and slack app configured via https://plugins.jenkins.io/slack/#plugin-content-creating-your-app
also added app to slack channel as @myappname -> yes, invite app to channel
you can find your slack apps via