CI/CD Extending Slack Support

As I continue to extend functionality for the jenkins-project-sample-dotnet, I’m wandering into areas I haven’t explored before. I have refined previous posts to simplify steps based on what I learned; however, revising what I did before won’t scale.

In the case of Slack notifications, the previous post is fine for simple posts, but I wanted to do something a little extra. I want to post a Slack message to a channel and post follow-up messages with additional information.

I need to use a Slack bot user instead of the Jenkins CI integration to achieve this.

Create the Slack App

We’ll need to create a new app in Slack at api.slack.com.

  • Select “Create an App”
  • Select “From Scratch”
  • Choose an app name, such as JenkinsBot
  • Select the workspace for these notifications
  • Choose “Basic Information” in the left nav
  • Scroll to “Display Information”:
  • Set up any additional information, particularly an icon that will show up for this bot user when it posts in Slack.
  • Select “OAuth and Permissions” in the left nav.
  • Under “Scopes” we need two Bot Token Scopes:
  • Add the OAuth scopes “chat:write” and “chat:write.public”. This allows posting and posting to channels the bot user isn’t in.
  • Scroll back up to the top once and choose “Install to Workspace”. This will trigger a permissions check:
  • Select “Allow”
  • There should now be a Bot User OAuth Token generated that you can use that replaces the token in Jenkins for signing in:

Jenkins Script Changes

This now allows for posting a Slack message to a thread using the following:

def sent = slackSend(color: status.slackColour, message: "Build ${status.notifyText}: <${env.BUILD_URL}|${env.JOB_NAME} #${env.BUILD_NUMBER}>")
slackSend(color: status.slackColour, channel: sent.threadId, "3/3 Tests Passed")

The real information I want to post to the thread will be warnings and errors parsed from the build log, but the above demonstrates the functionality.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.