Enabling threaded Slack messages while using the Jenkins Slack plugin
Most of the time, when reading documentation, everything seems clear how to implement a particular piece of software. In some cases it can be very time consuming which I found out during the implementation of the Slack plugin in the OpenShift Jenkins (Ephemeral) version. One way or another I wasn’t able to send threaded messages to Slack. I finally found a working solution which I would like to share.
I assume that you already have a slack account and a working instance of OpenShift Jenkins (Ephemeral). In this case we used Jenkins (Ephemeral) version 2.89.x and Jenkins Slack Plugin version 2.14.
What’s the problem
The Jenkins Slack plugin documentation stated that you have to configure the Jenkins CI App in your Slack environment and link the App to your desired Slack channel. After installing the Slack Plugin you’re able to send messages from Jenkins to Slack. So far everything is working as documented.
I use scripted Groovy pipelines in Jenkins which will send a message to Slack when a stage is finished. When more than one pipeline is started just sending messages in sequence you will have trouble reading which message relates to which pipeline. So we want to use threaded messages. In the channel you would like to see the main message regarding the started pipeline and build and every message after that will be in this thread so it keeps the consecutive messages together. When creating threaded messages you have to have set a Slack plugin property
botUser=true so that the Jenkins CI App will use the
@slackbot user to create and update the threaded messages. I couldn’t get this to work correct!
The solution of using threaded Slack messages is easy, create your own Slack App, set the correct permission(s) and link the App to your channel.
Step 1 – Install Jenkins Slack Plugin
In OpenShift Jenkins add slack:2.14 to the Jenkins environment property INSTALL_PLUGINS and restart the Pod.
Step 2 – Create your Jenkins Bot App in Slack
Login with your Slack account and visit Your Apps page and click Create New App.
Create Slack App
The following popup will be shown, Create a Slack App. Type your preferred App Name, select your Development Slack Workspace where the App will be created and click Create App.
After the App is created you will see a Basic Information screen. On the left you see a Features menu and click OAuth & Permissions. Scroll down to the Scopes section, select Permission Scope for value Send messages as Jenkins Bot
chat:write:bot and click Save Changes.
Install App to Slack Workspace
On the top of the page OAuth & Permissions there’s a section OAuth Tokens & Redirect URLs. Click on Install App to Workspace. Confirm the installation, click Authorize.
Create Bot User
Still on the left there’s the Feature menu and select Bot Users and click Add a Bot User. Update your popup screen with the values as shown above and click Add Bot User. After this, reinstall your App because you’ve changed the permission scope!
Link Jenkins Bot App to Channel
Go online and visit your Slack Workspace. On the left you see the Workspace menu and add the Jenkins Bot App, click on Apps +.
Select the Jenkins Bot App and it will now be in your workspace menu on the left.
Select the Jenkins Bot App, go to the settings menu and click Add this app to a channel. A popup screen is shown, select the channel #jenkins-pipelines and click Add to confirm!
Find your Bot User OAuth Access Token
In your Groovy script you need the Bot User OAuth Access Token to send messages by the Bot User. Now everything is configured in Slack to receive threaded messages.
Step 3 – Groovy pipeline example
In OpenShift you now can add this example BuildConfig file. This example creates a new Jenkins Pipeline in your OpenShift namespace.
When the pipeline has finished, this is what you see in Slack. One message with two replies.
When you click on 2 replies, you can see the whole thread.
I hope this will help demystify configuring threaded messages in Slack in combination with the Jenkins Slack Plugin.
Check out these external links to gain more info related to this post.
- Jenkins Slack Plugin v2.14 – GitHub code and documentation
- OpenShift Container Platform 3.7 – Pipeline Strategy
- OpenShift Container Platform 3.7 – OpenShift Pipeline
- Slack – Your Slack Apps page
The post Enabling threaded Slack messages while using the Jenkins Slack plugin appeared first on Edwin’s Tech Blog.