Jenkins Slack Integration
April 26, 2024
Jenkins Slack Integration
The tutorial outlines how to integrate Jenkins and Slack for monitoring CI/CD pipelines. It involves setting up a custom Slack app to receive messages from Jenkins, configuring Jenkins with the Slack Notification plugin, and adding the OAuth token from Slack as a secret text credential in Jenkins. Both traditional Jenkins jobs and Jenkins pipelines can be configured to send Slack notifications, with pipeline scripts used to send messages in the latter case.
Overview

When our teams are responsible for DevOps practices, we often need to monitor builds and other automated jobs.

In this tutorial, we’ll see how to configure two popular platforms, Jenkins and Slack, to work together and tell us what’s happening while our CI/CD pipelines are running.

Setting up Slack

Let’s start by configuring Slack so Jenkins can send messages to it. To do this, we’ll create a custom Slack app, which requires an Administrator account.

In Slack, we’ll create an application and generate an OAuth token:

  • Visit https://api.slack.com
  • Login to the desired workspace
  • Click the Start Building button
  • Name the application Jenkins and click Create App
  • Click on OAuth & Permissions
  • In the Bot Token Scopes section, add the chat:write scope
  • Click the Install App to Workspace button
  • Click the Accept button
  • When this is done, we’ll see a summary screen:

    Now, we need to take note of the OAuth token — we’ll need it later when we configure Jenkins. We should treat these as sensitive credentials and keep them safe.

    To complete the Slack setup, we must invite the new Jenkins user into the channels we wish it to use. One easy way to do this is to mention the new user with the @ character inside each channel.

    Setting up Jenkins

    To set up Jenkins, we’ll need an administrator account.

    First, let’s start by logging into Jenkins and navigating to Manage Jenkins > Plugin Manager.

    Then, on the Available tab, we’ll search for Slack:

    Let’s select the checkbox for Slack Notification and click Install without restart.

    Now, we need to configure new credentials. Let’s navigate to Jenkins > Credentials > System > Global Credentials and add a new Secret text credential:

    We’ll put the OAuth token from Slack into the Secret field. We should also give these credentials a meaningful ID and description to help us easily identify them later. The Jenkins credentials store is a safe place to keep this token.

    Once we save the credentials, there is one more global configuration to set. Under Jenkins > Manage Jenkins > Configure System, we need to check the Custom slack app bot user checkbox under the Slack section:

    Now that we’ve completed the Jenkins setup, let’s look at how to configure Jenkins jobs and pipelines to send Slack messages.

    A visual depiction of what is being written about
    Configuring a Traditional Jenkins Job
    A visual depiction of what is being written about

    Traditional Jenkins jobs usually execute one or more actions to accomplish their goals. These are configured via the Jenkins user interface.

    In order to integrate a traditional job with Slack, we’ll use a post-build action.

    Let’s pick any job, or create a new one. When we drop down the Add post-build action menu, we’ll find Slack Notifications:

    Once selected, there are lots of available inputs to the Slack Notification action. Generally, most of the default values are sufficient. However, there are a few required pieces of information:

  • Which build phases to send messages for (start, success, failure, etc)
  • The name of the credentials to use – the ones we added previously
  • The Slack channel name or member ID to send messages to
  • We can also specify additional fields if desired, such as commit information used for the Jenkins job, custom messages, custom bot icons, and more:

    When setting things up via the UI, we can use the Test Connection button to ensure that Jenkins can reach Slack. If successful, we’ll see a test message in the Slack channel from the Jenkins user:

    If the message doesn’t show up, the Jenkins log files are useful for troubleshooting. Generally, we need to double-check that the post-build action has all required fields, that the OAuth token was copied correctly, and that the token was granted the proper scopes when we configured Slack.

    A visual depiction of what is being written about
    Configuring a Jenkins Pipeline

    Jenkins Pipelines differ from traditional jobs. They use a single Groovy script, broken into stages, to define a build. They also don’t have post-build actions, so we use the pipeline script itself to send Slack messages

    The following snippet sends a message to Slack from a Jenkins pipeline:

    groovy
    slackSend botUser: true,
    channel: 'builds',
    color: '#00ff00',
    message: 'Testing Jekins with Slack',
    tokenCredentialId: 'slack-token'

    Just like with the traditional Jenkins job setup, we must still specify a channel name and the name of the credential to use.

    Via a Jenkins pipeline, we can also use a variety of additional Slack features, such as file upload, message threads, and more.

    One downside to using Jenkins pipelines is that there’s no test button. To test the integration with Slack, we have to execute the whole pipeline.

    When first setting things up, we can create a new pipeline that contains only the Slack command while we’re getting things working.

    A visual depiction of what is being written about
    Conclusion
    A visual depiction of what is being written about

    In this article, we’ve seen how to integrate Jenkins and Slack to gain feedback on our CI/CD pipelines.

    Using a Jenkins plugin, along with a custom Slack application, we were able to send messages from Jenkins to Slack. This allows teams to notice the status of Jenkins jobs and address issues more quickly.

    Discussion (0)

    Loading...

    Recommended articles

    More articles ➜
    New features of Next.js

    New features of Next.js

    Hey there! Ever heard of Next.js? It's a pretty cool tool for creating single page web apps (SPA) and static web apps (SSG). Built on React, Next.js is packed with handy features that can shave hours off your development time and make your pages load lightning fast. In this piece, we're gonna check out what's new and exciting in the latest version of Next.js. Stay tuned!

    Frontend
    Beiryu

    Beiryu

    Contributor

    0
    Friend Service System Design

    Friend Service System Design

    The Friend feature facilitates the management of user relationships, enabling connections and providing options for blocking interactions when necessary. This functionality enhances player connectivity and serves as a foundation for additional features, such as activity tracking and social engagement opportunities.

    Backend
    Tutorials
    AWS
    Beiryu

    Beiryu

    Contributor

    0
    Subscribe to the newsletter
    Get emails from me about web development, tech, and early access to new articles.