23

I want to use the Slack plugin in Jenkins to ping notifications to a Slack channel.

Jenkins says success when I test the connection, but I don't receive any notifications in my Slack channel.

Are there any known problems?

How can I get Jenkins to send notifications to Slack?

Daniel Serodio
  • 4,229
  • 5
  • 37
  • 33
Felix
  • 5,452
  • 12
  • 68
  • 163
  • Have you configured the channel on (each) build job in the post build action? – xeraa May 16 '15 at 14:21
  • You can also try a build notifier like [CatLight](https://catlight.io). It generates less noise than chat notifications, and always shows current job state in tray. – alex Mar 24 '17 at 00:56

8 Answers8

17

I think that you should add post-build action "Slack Notification" in your Jenkins. Please see the image below

enter image description here

Nguyen Sy Thanh Son
  • 5,300
  • 1
  • 23
  • 33
  • 2
    I had a similar issue from one of my users. I didn't think to check if this was done. Thanks pointing this out. – Brian S. Wilson Feb 02 '16 at 16:35
  • It's rather confusing having configuration options that won't actually apply until you add it as a build step. The options should be initially hidden and then appear in the build step panel. – Peter Clotworthy Mar 12 '16 at 22:12
  • 1
    Is there a way to modify the bot's message to include a `@channel` or `@here` to urgently notify the team? – dialex Jul 27 '16 at 16:31
12

And are you sure that you have a correct configuration. In Build Configuration (Do not forget # character) enter image description here

In General Configuration enter image description here

Nguyen Sy Thanh Son
  • 5,300
  • 1
  • 23
  • 33
8

There are two steps to configure a Jenkins job to be posting on a slack channel.

  1. Go to jenkins job configurations and add a post-build action on each job that you wish to ping the slack channel.

enter image description here

  1. Next, again under the job configurations, you have to configure on each job on which cases you wish to send slack notifications: (true - false) f.e.

enter image description here

In the case that you have to configure a great number of Jenkins jobs, you could configure only one of them manually and verify it is working fine. Then check the config.xml of this Jenkins job to find the auto-generated xml elements for the slack plugin preferences and apply those configs on all Jenkins jobs by using regex or xslt. In this case, you will have to reload the Jenkins configs for the job configurations updates to be applied. ("Manage Jenkins" / "Reload Configuration from Disk")

Prerequisites:

  • Install slack plugin in Jenkins.
  • Obtain a Jenkins CI integration token in your slack domain.
  • Go in Jenkins "Manage Jenkins" / "Configure System". There you have to configure the "Global Slack Notifier Setting".
Dorina Dede
  • 171
  • 2
  • 5
5

I didn't use the Slack Notification because I wanna customize style/state/message, etc. So I wrote a job called send_slack_notification instead. Every time I want to notify slack API I just call this job after build.

Here's the code in "Execute Shell", I used curl, sed and jsawk to do the job:

# URL to get the built info json
# will get "http://JENKINS_PATH/job/JOB_NAME/97/api/json"
NEW_URL="http://jks_username:jks_password@"$(echo ${BUILD_URL} | sed -r 's/http:\/\///g')"/api/json"

# Cut the JOB_NAME part from BUILD_URL
JOB_NAME=$(echo ${BUILD_URL} | sed -n 's/.*\/job\/\(.*\)\/[0-9].*/\1/p' | sed "s@+@ @g;s@%@\\\\x@g" | xargs -0 printf "%b")

# Get the built info json
JSON=$(curl $NEW_URL)

STATUS=$(echo $JSON | /usr/local/bin/jsawk "return this.result")
BUILD_INFO=$(echo $JSON | /usr/local/bin/jsawk "return this.displayName")
TIME=$(echo $JSON | /usr/local/bin/jsawk "return this.duration")
TIME=$(echo "scale=4; $TIME/1000" | bc -l)

# Cut the username
USER=$(echo $JSON | /usr/local/bin/jsawk "return this" | sed -n "s/.*Started\ by\ \([^\"]*\).*/\1/p")

# Customize the message sending to slack
TEXT=$JOB_NAME" Built by "$USER", it took "$TIME" seconds."

# Send notification using Slack API
# will send to https://hooks.slack.com/services/BLABLABLA/BLABLABLA
curl -X POST -H 'Content-type: application/json' --data '{"channel": "#production_info","username": "jenkins-bot","icon_emoji": ":lol:","text": "'"$TEXT"' (<'"$BUILD_URL"'|Open>)", "attachments": [{"color": "#36a64f", "fields": [{"title":"UPDATE INFO","value":"'"$BUILD_INFO"'","short":true},{"title":"RESULT","value":"'"$STATUS"'","short":true}]}]}' https://hooks.slack.com/services/BLABLABLA/BLABLABLA/BLABLABLABLABLABLA
Tom
  • 1,064
  • 1
  • 10
  • 7
  • I'm wondering how to determine the URL to post to. I see that in the slack-plugin configuration it provides a BASE_URL and INTEGRATION_TOKEN can these be used to determine the replacement for `https://hooks.slack.com/services/BLABLABLA/BLABLABLA/BLABLABLABLABLABLA`? – pbatey Aug 23 '17 at 17:35
  • nm - figured it out, they are concatenated. `curl -v -X POST -H 'Content-type: application/json' --data '{ "text": "test msg" }' $BASE_URL/$INTEGRATION_TOKEN` – pbatey Aug 23 '17 at 18:19
4

I though of adding it here for the greater good of the community. This is how you get the integration token

Jenkins Instructions

Get a Slack account: https://slack.com/
Configure the Jenkins integration: https://my.slack.com/services/new/jenkins-ci
Install this plugin on your Jenkins server
Configure it in your Jenkins job and add it as a Post-build action.

https://github.com/jenkinsci/slack-plugin

vishal
  • 640
  • 1
  • 5
  • 21
1
import os
import sys
from slacker import Slacker
import base64

def main():
    myPass=sys.argv[1]
    msgStr= sys.argv[2]
    channel = sys.argv[3]

    slack = Slacker(myPass)
    slack.chat.post_message(channel, msgStr)
    print msgStr

if __name__ == '__main__':
    main()    

python slack.py <token> < message str > <#channel>

I couldn't get anything but 'failure' from the slack connection test in the config. I can use python from the same box so I don't know what the issue is so I may just use this simple script.

Slava.K
  • 3,073
  • 3
  • 17
  • 28
ddtraveller
  • 1,029
  • 12
  • 18
1

If you want to receive notifications using Jenkins declarative pipelines, you can install the Slack plugin, and then use it in the pipeline in this way:

    stage('Clone sources') {
        steps {
            slackSend (channel: "#mychannel", color: '#FFFF00', message: "STARTED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
            git credentialsId: 'git_credentials', poll: false, branch: 'develop', url: 'https://mysource.com/mysimplenodejsexample.git'
        }        
    }

Result: enter image description here

You can customize your message of course. Full example at:

https://pillsfromtheweb.blogspot.com/2020/05/send-notifications-to-slack-from.html

SegFault
  • 2,020
  • 4
  • 26
  • 41
0

I had similar issues.

It worked for me when i unchecked "is Bot User?"

starting Jenkins in console with 'jenkins' not with brew demon, though.

Maybe that helps :) Greetings ^__^

  • Well there are 2 different ways to start Jenkins. with brew and the command 'jenkins' only. (I'm using a MAC btw) and in the configuration of Slack inside the CI Server Jenkins there was an option callend 'is Bot USer?" When i unchecked it it worked for me. Probably cz of the reason I didn't start Jenkins with brew Sorry better XD? – Stefanie Habersatter Aug 02 '17 at 15:15