When you work commonly with slack you can use the git integration to get the list of commits but you can also just rely on webhooks if you need more informations.

Since Slack also supports incoming webhooks you just have to define one - you need to be an administrator to do so. It will basically give you an URL you can post messages to.

Globally using curl syntax we want to execute:

curl -s \
  -d "payload={\"channel\": \"#slackchannel\", \"text\": \"Hello slack\"}" \
  "https://hooks.slack.com/services/XXX/YYY/ZZZ"

The question then is how to link it to git? Git also provides hooks to most of actions. If you don't know them just list files in .git/hooks/ folder of your clone. You have one for most of git phases (pre-pushpost-commit etc...).

Just edit the one you want to execute an action at the corresponding time. In our case we want to send a message when committing - but it is doable when pushing as well. To match commit time we'll edit .git/hooks/post-commit

Now it sounds obvious we will just execute our curl command in this file, the slack url and channel parameters are easy to replace but what about the text? Sending Hello each time we commit wouldn't help much ;).

Here the trick is to rely on git to get the last commit message well formatted and just use it as text:

#!/bin/sh
msg=$(git log -n 1 HEAD --format=format:%s%n%b)
text=$(echo "$msg" | sed 's/"/\"/g' | sed "s/'/\'/g")
curl -s \
  -d "payload={\"channel\": \"#slackchannel\", \"text\": \"$text\"}" \
  "https://hooks.slack.com/services/XXX/YYY/ZZZ"

With this hook configured, each time you will commit it will send a message on slack with your commit message. You kind of bridge your local git changelog to slack.

Of course for that simple use case there are automotion already but if you add some more logic you can enable a lot of use cases which can be useful like:

  • "i add an awesome feature cc @myColleague", direct ping from the commit line!
  • "i add an awesome feature #logJIRA", direct jira creation from the git context!
  • "i update the configuration repository and ##redeploy", kind of continuous deployment but user controlled - this case if of course when operation team uses a git repository to store the configuration
  • "i add this feature and #mail @myboss"
  • ...

You now have a way to execute anything based on git actions.

Now, and because this post is also about slack, you probably know or guessed that there are outgoing webhooks since we used the "incoming" one. This means you can create a kind of slack bot pretty easily and makes this awesome integration bidirectional:

  • from slack you can trigger a git tagging
  • from slack you can trigger deployments
  • from slack you can grab your production logs
  • etc....

Are these kind of integrations important? Indeed you should define the way you want to work first but having a centralized way to trigger actions is very efficient and avoids to loose time which is important when it comes to production - in development it is less impacting.

What are the alternatives? We are effectively building a command based solution so naturally you can think to slack but it then misses a small thing...a server to ensure it is bidirectional. Next self hosted and open source solution can use IRC but the UI is not nice enough for all cases - in particular when it comes to grabbing logs from a server and log some output :s. I didn't investigated it completely but gitter looks a potential nice alternative.

What should you keep of this post: we are in a world where easiness is the keyword (you can order a boat from your phone!) so ensure to match this lifestyle evolution in your everyday work making all small action taking a few minutes N times per day automated and hurtless.

 

 

From the same author:

In the same category: