Getting Redmine, Git and Gitosis working together
I love git. It rocks and is my “go to” for source code management. I also love Redmine and it’s now my “go to” for project management. They work nicely together when you don’t mind logging into your git hosting server to set up a new public repository. Why should it? When I can use the Redmine Gitosis plugin to do it all from the Redmine interface. The plugin leaves a lot to be desired as far as setting it up goes, so I’m writing this article to get the changes that I needed to make out into the world.
This article assumes that you have a fully function installation of Redmine and know how to install plugins. If now, the “redmine plugins”:http://www.redmine.org/projects/redmine/wiki/Plugins page is a good place to start.
Gitosis
The first thing we need to do is install Gitosis and get that up and running. It can be a little tricky when you don’t know how it works and it took me a while to figure it out, so I’ll try and explain it here using my limited knowledge.
A quick and nasty intro to Gitosis
Gitosis is a bunch of scripts that work with a central “gitosis-admin” repository. Users interact with git repositories as a single user and access is controlled by Gitosis. To add repositories, you clone the gitosis-admin repo, edit the config file, and export that back to the public repo. At this point, a post-commit hoot script is executed which “does stuff”. This is essentially how Gitosis works. Then you can commit to the admin repo the script runs which sets things up. You don’t really need to know much more than that.
Installing Gitosis
I use Ubuntu (and so should you be) so I’m only going to go into installing it on Ubuntu here. There is already a tonne of documentation on how to install it from the Gitosis repository on github. Use Google. Or if you’re lazy, just “go here”:https://github.com/res0nat0r/gitosis.
The steps involved are pretty much install it from APT, then initialise it with your public key. Just two steps. It couldn’t be easier.
First, you MUST create a public/private key pair that is unique to Gitosis. You *CANNOT* use your public key as this will cause problems later down the track when you start committing things to your project repositories. The reason I’m making a big deal about this is because it caught me out. Most of the documentation out there for installing Gitosis refers to using your own _id_rsa_ private key file. Don’t. You need your own key later and it needs to be unique or this wont work.
I just did the following:
$ cd /tmp $ ssh-keygen -t rsa -f redmine_key
This will create two files in _/tmp_ called *redmine_key* and *redmine_key.pub* which are your private key and public key respectively.
Now to install Gitosis:
$ sudo apt-get install gitosis $ sudo -H -u gitosis gitosis-init < /tmp/redmine_key.pub
In the second command, the _-H_ is absolutely necessary as the script uses the $HOME environment variable which is only set correctly with the _-H_ parameter. When you run the gitosis-init command, you should see the following if it’s successful:
Initialized empty Git repository in /srv/gitosis/repositories/gitosis-admin.git/ Reinitialized existing Git repository in /srv/gitosis/repositories/gitosis-admin.git/
At this point, Gitosis is installed and ready to go. But, to make sure you are going to test it, right? Let’s do that.
Normally at this point you would create a test project to test the gitosis install but seeing as we are using a separate unique private key, we can’t. Trust me though. If you’ve gotten this far without error, it will work.
Install the Gitosis Redmine plugin
Up until now, you’ve been doing stuff that is pretty easy and common to all Gitosis installs. The next part takes some work because the plugin itself while good when it’s working, *WILL NOT* work out of the box. That is, you need to make a few changes to get it working AT ALL. A lot of the changes are to fix silly bugs that the author has not bothered to fix as yet.
So, here we go.
Install
This part is common to all Redmine plugins, and this one is no different. You _should_ know how to do this. Just remember to replace _/usr/share/redmine_ with wherever your version of Redmine is installed.
As your redmine user, do the following to install the plugin:
$ cd /usr/share/redmine $ script/plugin install git://github.com/rocket-rentals/redmine-gitosis.git $ cd vendor/plugins $ mv redmine-gitosis redmine_gitosis
Now we need to fix up the database definition and a tiny little bit of code to make it all work correctly. See, the problem is that the code refers to a field in the database as an integer, but in the database it’s a boolean. This is wrong and wont work. So we need to update the database definition to create the field as an integer instead, then fix the one place that it actually does refer to it as a boolean. Blah. What a pain.
Fix the plugin
From the plugins directory (you should be in it), edit _redmine_gitosis/db/migrate/20091119162427_create_gitosis_public_keys.rb_ (the number _may_ be different, but you’ll find it) and change this:
t.column :active, :boolean, :default => true
to this:
t.column :active, :integer, :default => 1
It should be about the 7th line down and quite easy to find.
Now, edit _redmine_gitosis/app/helpers/gitosis_public_keys_helper.rb_ and change the only two lines with true and false in the from this:
["#{l(:status_active)} (#{key_count_by_active[true].to_i})", GitosisPublicKey::STATUS_ACTIVE],
["#{l(:status_locked)} (#{key_count_by_active[false].to_i})", GitosisPublicKey::STATUS_LOCKED]], selected)to this:
["#{l(:status_active)} (#{key_count_by_active[1].to_i})", GitosisPublicKey::STATUS_ACTIVE],
["#{l(:status_locked)} (#{key_count_by_active[0].to_i})", GitosisPublicKey::STATUS_LOCKED]], selected)Basically we are removing the true/false references and replacing them with 1/0 respectively.
Migrate (create the database stuff)
Now we can migrate the plugin as normal:
$ rake db:migrate:plugins RAILS_ENV=production
Configure the plugin
Before we can start using the plugin, we need to make sure it knows how to talk to Gitosis. The defaults will most likely NOT work.
This is the setup for Ubuntu. But if you’re not on Ubuntu you should know what to replace by now. Just edit _redmine_gitosis/lib/gitosis.rb_ and set:
# server config GITOSIS_URI = 'gitosis@www.funkynerd.com:gitosis-admin.git' GITOSIS_BASE_PATH = '/srv/gitosis/repositories/'
The last thing we need to do is add the private key for Gitosis to the plugin. We created this waaay back in the first step, but it should still be there. Redmine keeps it’s private key in _{plugins_dir}/redmine_gitosis/extras/ssh/private_key_, so just do this:
$ cp /tmp/redmine_key /usr/share/redmine/vendor/plugins/redmine_gitosis/extra/ssh/private_key
Now you can restart you Redmine server. For me that’s just:
$ sudo /etc/init.d/apache2 reload
Using the plugin
Everything is installed and should be ready to go and the plugin should be running in Redmine. There is one final config option we need to do for your Redmine user account.
Add your public key to your account
From the Redmine interface, click on “My Account” to access your account page. You should now see a link that says _Public Keys_, probably on the top right of the page (depends on your Redmine theme). Click through to the Public Keys page and we can add your public key by clicking on _New Value_.
The page that comes up should look like this:

Just give it a name and paste in the contents of your _~/.ssh/id_rsa.pub_ file.
Now you can add repositories to projects as you would normally. Instead of specifying where the repo is located though, the plugin just creates it in the background.
Committing code to the repo
To commit your code you can do the following from your projects source dir (assuming it is not already a local git repo):
$ git init $ git remote add origin gitosis@your.servername.com:projectname.git $ git add * $ git push origin master
Hopefully that will commit correctly and you can go back to hacking you code.