Skip to main content
Hello — this guide shows how to design Slack Block Kit messages (attachments + blocks) and integrate them into Jenkins notifications using the slackSend step. You’ll learn how Block Kit structures messages as JSON, how to prototype with the Block Kit Builder, and how to construct and send attachments from a Jenkins shared library. The Slack API messaging docs are a great reference:
A screenshot of the Slack API documentation page titled "Creating rich message layouts," showing the left navigation menu and main content describing how to add blocks to messages. The browser window also shows tabs along the top and a small circular user photo in the upper-right.
Overview
  • Block Kit composes messages from modular blocks (e.g., section, divider, header, actions).
  • Use the Block Kit Builder to preview desktop and mobile renderings before sending.
  • Block Kit payloads are JSON. Build the structure locally or programmatically (Groovy) and send via Jenkins’s slackSend.
Quick notes:
Use the Block Kit Builder to prototype your JSON payload and test “Send to Slack” (you must be logged in). When integrating with Jenkins, construct the same JSON structure in Groovy and then either pass the Groovy list/map directly or convert it to a JSON string depending on your slackSend implementation.
Simple Block example (Block Kit)
Block Kit Builder example (multiple blocks)
Attachments + Blocks
  • An attachment is a wrapper that may contain fields such as color and a blocks array.
  • Many Slack integrations send attachments that include blocks for rich layouts.
  • Build your attachments payload in the same shape the Block Kit Builder shows, then deliver it via slackSend.
Example attachment payload (representative)
You can test “Send to Slack” directly from the Block Kit Builder to a channel you choose (you must be logged in). The preview will match how Slack renders the message in-channel. When sent, a message similar to this appears in the selected channel.
A screenshot of a Slack workspace open to the #jenkins channel, showing Jenkins app notifications in the chat. Below the notifications is a user message containing Thai restaurant reviews with small food thumbnail images.
Integrating attachments into Jenkins
  • Jenkins’s Slack Notifications plugin exposes the slackSend pipeline step for sending messages.
  • Different plugin versions accept attachments differently: some accept Groovy lists/maps, others expect a JSON string. If necessary wrap your Groovy structure with groovy.json.JsonOutput.toJson(attachments).
  • Best practice: build the attachments payload as Groovy maps/lists in a shared library and call slackSend() in post-build steps.
Below is a compact, corrected Groovy function suitable for a shared library (save as vars/sendNotification.groovy). It:
  • selects color and emoji by build status,
  • composes an attachments payload with header, job fields, failed stage, and action buttons,
  • calls slackSend (either with Groovy structures or JSON string depending on your plugin).
Call from your Jenkinsfile
  • Ensure the shared library is configured and the library name matches your @Library('...') directive.
  • Call sendNotification from post-build blocks (e.g., success, failure) and set helpful environment variables like failedStage and emoji to provide contextual data.
Example snippet from a Jenkinsfile (declarative pipeline):
Troubleshooting and tips
  • If junit fails with “Test reports were found but none of them are new,” it means test results were published but no tests ran in this build. Guard or comment out report publishers when you skip tests during Slack testing.
  • If Jenkins errors with “No such DSL method ‘sendNotifications’ found”, confirm the shared library function name matches the call site (sendNotification vs sendNotifications).
  • If images/icons do not appear:
    • Ensure image URLs are publicly accessible.
    • Alternatively upload the images to Slack and use a custom emoji name for iconEmoji.
    • Custom emoji names must match exactly when referenced (e.g., :deadpool:).
If your slackSend implementation requires a JSON string for attachments, convert the Groovy structure with groovy.json.JsonOutput.toJson(attachments) before sending. Also verify that image URLs are reachable from Slack (public or Slack-hosted).
Uploading custom emojis
  • In Slack: Workspace menu → Customize → Emoji, upload an image, assign a name, and then use that name (for example, :deadpool:) with iconEmoji in slackSend.
A Slack workspace window with the "Add emoji" dialog open, showing an uploaded deadpool.png preview and the name field for the custom emoji. In the background the #jenkins channel and messages are visible.
Testing and expected results
  • After configuring the shared library and payload, run a test build that triggers the post step.
  • A successful message will include:
    • header with pipeline name,
    • job name and build number,
    • failed stage (if any),
    • Kubernetes deployment info and node port,
    • Git commit and branch info,
    • action buttons for Jenkins build and Git repo URLs.
Example Jenkins build screenshot (for sanity check):
A Jenkins build page screenshot showing "Build #77 (Jun 25, 2021)" with status, Git revision and repository details in the main pane and a navigation menu on the left. A small circular video/profile thumbnail of a person appears in the top-right of the browser.
Final Slack message example (what you should see in the channel):
A screenshot of a Slack workspace open to the #jenkins channel showing a Jenkins Slack App message about a "K8S Deployment - devsecops Pipeline" with job details (job name, build number, Kubernetes node port, Git commit, etc.). The left sidebar shows other channels and apps while the main pane displays the deployment notification and action buttons.
Quick reference table Links and references Wrap-up
  • Prototype Block Kit messages in the Block Kit Builder, then recreate the same attachments/blocks structure in Groovy.
  • Keep the payload consistent between Block Kit Builder and Jenkins; convert to JSON if your plugin requires a string.
  • Upload any custom icons to Slack and reference them via iconEmoji when calling slackSend.
  • Add logic in your shared library to capture failed stage names and other contextual metadata to improve the usefulness of notifications.

Watch Video