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 you may also utilize local git repositories hosted on your own server.
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 flag 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. Setting
--shared=2664 sets the
core.sharedRepository setting, which forces git to do all new file creation with 2664 permissions. This helps to avoid issues with multiple users pushing to the repository.
foo.git repository already exists and you need to fix the permissions after-the-fact, perform the following commands
# 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
Next, edit the file
/path/to/repo/foo.git/config. Under the
[core] area, add or modify the following line
sharedRepository = 2664
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
Make note 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'
Listed below are the other options 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 above) 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 multiple commit messages with
git config hooks.slack.show-only-last-commit set to