user111
user111

Reputation: 53

Send Slack notification from Jenkins into private channel of other users

What i did:

I managed to send Notifications with Status of every Build to my private Slack Channel. So i configured my Jenkins as well as my Slack app.

What i want to do:

Sending messages to other users private Channels.

What i have tried:

I added channels to my Jenkinsfile and checked them in the Slack App of Jenkins and it wasn't successful. I think i have to create sth like a Bot, which is in the specific Channel i want to send a message to. Very sure that Jenkins can't see the channel because its a private channel of another person(obviously) and is not able to find it. Couldn't find a solution for this Problem.

Thanks a lot for your help, i think i wasted way to much time trying to find an answer for that.

Upvotes: 0

Views: 7824

Answers (2)

MaratC
MaratC

Reputation: 6869

In my experience sending Slack messages to a private channel requires OAuth. I have only succeeded sending to private channels via slackSend() when Jenkins is added to Slack as a bot and invited to the channel.

Here's an example pipeline that works:

#!/usr/bin/env groovy


pipeline {
    agent {
        node {
            label "master"
        }
    }

    stages {
        stage('Send Notification') {
            steps {
                script {
                    def color = "${params.MESSAGE_STATUS}" == "GOOD"? "good" : "warning"
                    slackSend(color: "${color}", message: "${params.MESSAGE}", channel: "${params.CHANNEL}")
                }
            }
        }
    }

    parameters {
        string(name: 'MESSAGE', defaultValue: 'Hello')
        string(name: 'CHANNEL', defaultValue: '#test_private')
        choice(name: 'MESSAGE_STATUS', choices: ['GOOD', 'WARNING'], description: '')
    }
}

Sending via bots etc. hasn't been successful as Slack reports the channel as non-existant.

Upvotes: 0

John Camerin
John Camerin

Reputation: 722

I setup our build pipeline to send Slack notifications to commit authors. The biggest challenge is mapping to the Slack username. I had every developer on the team change their git user.name to match their Slack Display name. This is the most straight-forward way I know to make this work.

git config --global user.name "Mona Lisa" where "Mona Lisa" is the Slack display name of the user. In the Jenkins pipeline, I used env.GIT_COMMIT_AUTHOR to get this value back.

Note, that the environment variable provides the commit author at the HEAD of whatever was checked out. For pull requests in a multi-branch pipeline, if the PR is not a fast forward, the commit is what the merge would produce, resulting in an author of 'Jenkins'. So, in that case, you would need the author from HEAD~1

Upvotes: 0

Related Questions