• Home
  • Blog
  • Custom Email Notifications in GitLab

Custom Email Notifications in GitLab

Vincent Danen

January 07, 2015

I started playing around with GitLab last month in order to get to know it a better and, while I like it well enough, the one thing that drove me nuts was the email that it sent out alerting of changes. My old git setup used the wonderful git-notifier script to send out emails and I much prefer the format it used than the format GitLab uses. Unfortunately, at that time, without ponying up for the enterprise edition it didn't look feasible to change without some serious work that I didn't have the time or effort to invest.

Yesterday I was looking at the latest version (7.6.2) and noticed the community edition support for custom hooks. After upgrading, I fiddled with it and git-notifier to try to make the two work well together. With a little elbow-grease (git-notifier works well with straight git repos or gitolite) I got it to work, although it is a bit of a nuisance because, with regular git or gitolite, you can get some information from the repo exposed via the calling scripts and environment that does not seem to be present in GitLab.

If you follow the instructions on the custom hooks document referenced above, you'll end up with something along the lines of /var/opt/gitlab/git-data/repositories//.git/custom_hooks (in my case it is /srv/git-data/repositories//.git/custom_hooks). In this directory (which must be owned git:git, including all its contents) lives a post-receive script which looks like:

#!/bin/sh
repo_name="mygroup/myrepo"

base_dir="/srv/git-data/repositories"
git_host="gitlab.myhost.com"
send_from="mailer@myhost.com"
send_to="commits@myhost.com"

pushd ${base_dir}/${repo_name}.git >/dev/null 2>&1

/srv/git-hooks/git-notifier $@ --link="http://${git_host}/${repo_name}/commit/%s" --sender="${send_from}"  \
  --mailinglist="${send_to}" --repouri="ssh://git@${git_host}:${repo_name}.git" --emailprefix="[git/%r]"

popd >/dev/null 2>&1

I have git-notifier in a directory called /srv/git-hooks and it's owned root:root and mode 0755. This will tell git-notifier to send an email to the $send_to address, from the $send_from address, and defines a few things like the repository itself and the host (all things that would normally be exposed via the environment in a git or gitolite setup but are lacking with GitLab). But this can be used as a template and the only thing you should have to change is the value of $repo_name (everything else can be the same unless you need to define them differently per-repo or per-group).

The downside to this is that you need shell access to set it up, which may prove troublesome for larger installations or shared environments. For a personal or work environment this is probably an ok requirement. Make sure that you disable the "Emails on push" service for the repository in GitLab or you'll get both the stock GitLab commitdiff email and git-notifier's email.

I'm extremely grateful for those who contributed this support to GitLab as it means I spent a lot less time dorking around with this than I would have had I done it all myself, and while it was a bit of a nuisance to setup, it works quite well and I'm back to getting my old style of email notifications which are much more useful (for one thing, GitLab seems to have an upper size limit and if that is exceeded it sends no mail at all, whereas git-notifier will send you a list of changed files without the actual diff... a much more useful and meaningful email than sending nothing at all (if you look at git-notifier's changelog you'll see that was contributed by me in version 0.3-18, almost 2.5 years ago... that's how long I've been using git-notifier)).

I wish I could contribute some sane code back to git-notifier to support GitLab, but without GitLab exposing things like the repository name or committer name to the environment I don't think it would be possible unless I've missed something non-obvious.

Leave a Comment

Comments use MarkDown. Need help? MarkDown Cheatsheet