Saturday, October 4, 2014

Configuring Git Email Notifications via Post-receive hook

Here I'm going to explain how to configure Git to send email notifications when 'central' repo is updated on Linux and windows machines. 
LINUX:
Step 1: Copy (or symlink) the post-receive script, or download it here. The script must be in the ‘hooks’ directory like this:
$GIT_DIR/hooks/post-receive
Make sure it is named “post-receive” so that Git recognizes it.
Step 2: Make sure that the script is executable:
chmod a+x $GIT_DIR/hooks/post-receive
Step 3: Edit the first line of $GIT_DIR/description to be your repository name. This will be in the subject of the email.
Step 4: Edit $GIT_DIR/config with some email settings such as recipient / sender email and subject-line prefix. Here’s some basic settings (read the script for all possible settings).
[hooks]
mailinglist = "receiver1@receivers.com, receiver2@receivers.com"
envelopesender = sender@senders.com
emailprefix = "[GIT] "
Note: you can also set these by using git-config:
git-config hooks.mailinglist "receiver1@receivers.com, receiver2@receivers.com"
git-config hooks.envelopesender sender@senders.com
git-config hooks.emailprefix "[GIT] "
That’s it ! Try doing a git-push to your shared repository, and see if you get email notifications.
WINDOWS:
STEP 1: Same as Linux.
STEP 2: The post-receive script is using 'sendmail' for sending mails. But Git for Windows provides msmtp not sendmail. So we need to configure smtp server and use it in the script. Replace sendmail with msmtp in the post-receive script as follows
send_mail()
{
    if [ -n "$envelopesender" ]; then
        msmtp --host="$smtpserver" -t -f "$envelopesender"
    else
        msmtp --host="$smtpserver" -t
    fi
}

And finally, near the end of the file is a section that reads in the git config variable so add a new line to read server values:

smtpserver=$(git config sendemail.smtpserver)

STEP 3: Same as Linux.
STEP 4: It requires all configuration as mentioned in step 4 Linux. Apart from that it requires one more variable to configure "sendemail.smtpserver"
git-config sendemail.smtpserver 192.168.16.12
That's it for windows!.

No comments:

Post a Comment