This article explains how to install a bash script that posts a new message into a Slack channel when commits are pushed to your project’s git repository. Note that this implementation does not require the usage of GitHub, but rather git repositories hosted on your own servers.
Step 1: Create a bare repository
To create a new repository on your server named
foo.git, first connect to your server, then run the following commands:
# Navigate on your server to the desired repository location cd /desired/path # Create the directory mkdir foo.git # Navigate inside the new directory cd foo.git # Initiate a bare git repository git init --bare --shared=2664
--bare ensures that the repository only contains the version control information and doesn’t have any local files. This is necessary to be able to clone and push to the repository.
--shared=2664 sets the
core.sharedRepository which forces git to do all new file creation with 2664 permissions. This helps to avoid issues with multiple people pushing to the repository.
foo.git repository already exists and you need to fix it, do the following:
# Navigate to the repository cd /path/to/repo/foo.git # Apply fixes chgrp -R swdev * #may require sudo chmod -R ug+rws * #may require sudo
Then edit the file
[core] area, add or modify the following line:
sharedRepository = 2664
Step 2: Install script on the server
Download the bash script to your local machine.
Copy the file from your local machine to your project with the following command:
# Run command from your local machine (not on your server) scp ~/local/path/to/file/post-receive @servername.com:/repo/path/on/server/foo.git/hooks/post-receive
Finally, change the permissions of the
post-receive file on the server to allow it to be executable:
# Navigate to the repository's hooks folder on your server cd /path/to/repo/on/server/foo.git/hooks # Change permissions of the post-receive file chmod +x post-receive
Step 3: Configuration
- Add an incoming WebHook to your Slack team: https://my.slack.com/services/new/incoming-webhook
- Make a copy of the WebHook URL
- Connect to your server and navigate to your repository. Add the WebHook URL via git config along with two other settings:
# Navigate to repository cd /repo/path/on/server/foo.git # Add WebHook URL git config hooks.slack.webhook-url 'https://hooks.slack.com/services/...' # Set option to show all commits pushed git config hooks.slack.show-only-last-commit false # Set repository nice-name git config hooks.slack.repo-nice-name 'Our Awesome Project'
Step 4: Set Options (run commands within repo on your server)
Listed below are the other settings that are configurable. Most of these can also be setup when configuring the Incoming WebHook on Slack, so by not setting them here, they default to the Slack settings. It’s recommended to not set these options here (unless listed in Step 3) so that the default WebHook settings are applied.
Specify a channel/user to post to in Slack instead of the WebHook’s default:
git config hooks.slack.channel '#channel' '#channel' - Post to channel '@username' - Direct message from @slackbot to user 'groupname' - Post to group
Specify a username to post as. If not specified, the default name will be used that was created on Slack:
git config hooks.slack.username 'Marty McFly'
Specify an image to display in Slack instead of the default icon:
git config hooks.slack.icon-url 'https://cdn4.iconfinder.com/data/icons/btf_LIN/LIN/256/delorean.png'
Specify an emoji to display in Slack instead of the default icon:
git config hooks.slack.icon-emoji ':ghost:'
Specify a repository nice-name that will be shown in messages:
git config hooks.slack.repo-nice-name 'Our Awesome Project'
Specify whether you want to show only the last commit (or all commits) when pushing multiple commits:
git config hooks.slack.show-only-last-commit true
Specify if you only want certain branches of the repository:
git config hooks.slack.branch-regexp regexp
An example of a single commit message:
An example of multiple commit messages (with
git config hooks.slack.show-only-last-commit set to