Send Slack Notifications with Jenkins

Send Slack Notifications with Jenkins


No alt text provided for this image

How automation build environment to run build every single push happens on the source repository we need to broadcast the status of the build to the team members will ensure code base sanity

Jenkins is an open-source CI server that allows you to easily set up pipelines for the automation of testing, building, and deploying. At certain stages of the pipeline, you may decide you want to send out a Slack notification to you and your team to notify you of the build status.

Install Jenkins app for Slack

Go to your Slack app on your desktop.

On the bottom of your left side navigation bar, you will see Recent Apps.

Click Add more apps.

No alt text provided for this image

Search for “Jenkins CI” and click Add.

No alt text provided for this image

You will be sent to the Jenkins app page in your browser. Click Install.

Select the Slack channel you want to post to and click Add Jenkins CI integration. (You can also create a new channel by selecting the bottom right link where it says create a new channel).

No alt text provided for this image

On the next page, scroll down to Integration Settings to find all the settings and info you need to customize your Slack notification in Jenkins.

We will need the auto-generated Token later for configuration in Jenkins.

No alt text provided for this image

Install Slack Notifications plug-in for Jenkins

Now, go to your Jenkins admin portal in your local browser.

On the top left corner, go to Jenkins → Manage Jenkins → Manage Plugins

No alt text provided for this image

Under the Available tab, filter and search for “slack notification.”

No alt text provided for this image

Select Slack Notification, then Install without restart on the bottom left.

No alt text provided for this image

It will take you to the installation page, and it should only take a few seconds to finish installing.

No alt text provided for this image

Once the “Success” message appears, go to back to the main page by clicking Go back to the top page

No alt text provided for this image

Connect Jenkins to your Slack

Before we set up a new pipeline job, we need to configure Jenkin’s settings to connect to your Slack.

We will do this in Manage Jenkins → Configure System

No alt text provided for this image

Scroll all the way down on the Configure System page until you see the settings for Slack

No alt text provided for this image
  • Workspace: Your team’s workspace name. If you sign in to slack at https://example.slack.com/, your workspace is ‘example’.
  • Credential:
  • - Click Add → Jenkins and a pop-up will appear.
  • Under the Kind dropdown, select secret text.
No alt text provided for this image

Copy and paste your Slack Token into Secret, and click Add.

If you forgot to save/copy your token, go to:

Slack app directory (https://<YOURWORKSPACE>.slack.com/apps/manage→ Jenkins CI → Configurations → Edit Configuration → Integration Settings

No alt text provided for this image

Optional: I added SlackToken for Description to keep track of my credentials/tokens

No alt text provided for this image

so that it’s readable in the drop-down after you’ve added it. You can choose any name you want.

No alt text provided for this image

Make sure to select the newly added token credential from the drop-down.

  • Default channel/member id: Enter the channel name (i.e. #channel) or user ID (i.e. UUU123UU4) you want the notification to be sent to.
No alt text provided for this image

Once everything is filled out, click Save.

Create a new job to test the Slack notification.

Click create new jobs.

No alt text provided for this image

Name your new job, select Pipeline, then click OK on the bottom left.

No alt text provided for this image

Scroll all the way down to Pipeline. We will write or copy our JenkinsFile script into the Script text box.

No alt text provided for this image

Copy and paste the following code into the pipeline script. This is the bare bones you need for sending a Slack message

No alt text provided for this image

Click Save.

Click Build Now. Wait a second…

Click into your newest build which should appear flashing under Build History.

No alt text provided for this image


Click on Console Output to view what happens as it would in your terminal or command prompt.

No alt text provided for this image

If all goes well, a Slack message has been sent to you by a jolly butler.

No alt text provided for this image



要查看或添加评论,请登录

?? Saral Saxena ???????的更多文章

社区洞察

其他会员也浏览了