Search code examples
jenkinsgroovydevopsslack

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.


Solution

  • 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