Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send messages from Jenkins to multiple Slack channels

Tags:

jenkins

slack

According to the main Jenkins-Slack documentation you have to configure the Jenkins app for slack and there you specify a single channel where the plugin can post. Now in my case, I need to post to multiple channels and users from a Jenkins server and it is very cumbersome to manage multiple auth tokens. Is there a way to have a single token that would work with all the channels and users? Are there other approaches? I see that the Jenkins plugin has a bot checkbox, but there is almost no documentation on how to make this work.

like image 913
Uko Avatar asked Jan 18 '18 10:01

Uko


3 Answers

You can add channel: '#CHANNEL_NAME:

More info: https://jenkins.io/doc/pipeline/steps/slack/

post {
    success {
      slackSend (channel: '#ch1', color: '#00FF00', message: "SUCCESSFUL: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
    }

    failure {
      slackSend (channel: '#ch1', color: '#FF0000', message: "FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
    }
}
like image 136
Dhanasekaran Anbalagan Avatar answered Oct 07 '22 08:10

Dhanasekaran Anbalagan


I just implemented the solution for my project channels.

There is an Advanced button available in the Post build actions -> Slack notification of a Job.

You can give the base URL and token along with the channel name you want to send the notifications to.

like image 2
Sarath Avatar answered Oct 07 '22 08:10

Sarath


If one has enough permissions to their organization's slack account, one can create an app at https://api.slack.com/apps?new_app=1 which acts as a bot.

Once this bot is installed to workspace [ https://api.slack.com/apps/<org-id>/install-on-team? ] and was given permission to post to targeted channels ( just send a message /invite @<bot-name> in the channel and it will have permissions to post to that channel]. Not sure how to do it for users, though.

Apart from that, slackSend supports sending to multiple channels in a single go.

Multiple channels may be provided as a comma, semicolon, or space delimited string.

https://jenkins.io/doc/pipeline/steps/slack/

For ex., any one of the following are valid:

channel: "#ch-1,#ch-2,#ch-3"

(OR)

channel: "#ch-1;#ch-2;#ch-3"

(OR)

channel: "#ch-1 #ch-2 #ch-3"

Full command may be as:

slackSend (channel: '#ch1 #ch2 #ch3', color: '#00FF00', message: "SUCCESSFUL: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
like image 1
VanagaS Avatar answered Oct 07 '22 07:10

VanagaS