I’m going to jump around a bit with some of these posts – some will be about what I’m doing at the moment, others will be pulling together the history of how to get a ruby on rails development environment up and working, still others about the concept of what I’m trying to create in a development environment, testing environment and production environment in Amazon. No doubt it will make some sense once I write it. We’ll see.
Today, what I’ve been doing is sorting out a git server, this post describes how to create a git server including a web client to view the git repository, and the process for creating a git new git repository for a ruby on rails project. Git is a distributed version management system for the application code. It’s very powerful, and also very complex (so far as I can tell). It is used by the linux kernel developers, and was written by Linus himself to meet the particular requirements of kernel development. Refer the wikipedia entry for more information.
So, why did I choose git for version control?
Firstly, it’s very powerful. It allows branching and branch merging to be completed very easily. Second, it’s commonly used across a number of open source projects, and it’s open source itself. Finally, it allows you to work disconnected – every machine has a full copy of the repository, so you can work on your laptop on an airplane, or anywhere else you don’t have a network connection, without losing any functionality. Finally, I feel like this is the version control system that more and more people will use over time, so I wanted to get familiar with it.
For the purposes of this post, I’m assuming that you have a central server onto which you’d like to create your new git server, that server has a web server (in my case Apache) already installed on it. You have a local development machine where you’ll do your Ruby on Rails development, it will have the local rails repository.
First step is to install the necessary software on your central repository. In my case, this is my development-services machine, this hosts my central mysql database and my mediawiki wiki for all project documentation. So, I’m assuming you already have an apache installed, that apache has your website stored in the default debian location:
So, first step is to install the git central software and the secure copy utility that runs over ssh:
aptitude install git gitweb scp
Next, we use some paraphrasing from the instructions on this site to create our base configuration. We create a git directory under the web server into which all our git repositories are going to go, this makes the repository easily available to the gitweb application. We also change the owner of this to a username and group that will allow people to upload (i.e. the permissions on this directory need to be suitable for all your git users to ssh in and upload to it):
mkdir /var/www/git chown paul:users /var/www/git
Now, at this stage my notes have a comment that says “change gitweb to point to this new directory”, but I’m pretty sure that I didn’t actually have to do anything. It looks through the entire /var/www directory to see anything that looks exportable as a git repository. Not 100% certain on that though, so let me know if you see something different.
Next, I go to my development-apps server, which is where I actually develop my ruby. In your case, you might be doing this on your laptop or some other machine. The first thing I do is to create a new Ruby on Rails project (you can probably work out how to do this for yourself for an existing Ruby on Rails project). I’m creating my projects using the twitter bootstrap builder, so this command looks something like this:
cd /home/apps/ rails new testapp4 -m https://raw.github.com/RailsApps/rails-composer/master/composer.rb
This creates a directory with everything you need in it, and creates the base git repository. It also puts a .gitignore file into the root of the directory, which ignores some files that you might not want ignored. In my case my aim is that I can check out a code base from git and have it just work immediately. The files that were being ignored that I didn’t like ignored were .rspec (configuration options for rspec – arguably these are user specific, so I can see why it might be ignored), config/application.yml and config/database.yml. Again arguably on a big site these might be files that were different for each developer’s machine, in my case I use a standard file and it’s just easier to not have to remember to edit it. So I comment those lines out in the .gitignore:
# .rspec # can't see a reason to ignore this, so have changed it # /config/config.yml # I think we need this to get a directory that works # /config/database.yml # I think we need this to get a directory that works # /config/application.yml # this has the seed data in it - useful to be in one place
I also would typically change my generated application to use mysql instead of sqlite (maybe this is a reason that the database.yml shouldn’t be in git), so I’d do those changes then commit them:
git commit -a -m "Get base application working with mysql"
Next, I want to create the base repository that I’m going to copy up onto the server. What I want to do is to create a git bare repository – a repository that has only the git stuff in it, but doesn’t have the working directory that you actually edit stuff in. By convention these repositories are named with .git on the end, to let you know it’s a bare repository. To do this you:
cd /home/apps/ git clone --bare testapp4 testapp4.git
Next, you copy that directory onto your central server, in the git folder you created. To do this I’m using scp, which runs over ssh. I’m assuming that you’ve created a trust relationship using public keys, so that the logon is automatic. I’ll put up another post on how to do that another day. To copy up the folder you do as follows, I’m deleting the local bare repository afterwards as we don’t need it locally any more:
scp -r testapp4.git development-services:/var/www/git/ rm -rf testapp4.git
Next, you want to check that this new repository, when checked out, gives you the same thing you had before. To do this, I do:
mv testapp4 testapp4.pre_central_git git clone development-services:/var/www/git/testapp4.git
Check that this new testapp4 directory works just as well as the one you had before – that nothing is missing.
You now want to configure some items directly on the central server in order to make this repository visible via gitweb. Logon to the central server, and create a file that tells gitweb that it can publish this repository:
Now you can visit your web server in your browser to see if you can see the repository. In my case this is http://development-services/gitweb.
Finally, you might want to create a description for your new project, you can do this by editing /var/www/git/testapp4/description. This description will show up in gitweb.
If you’re developing extensively with git, and using branches and the like a lot, you might also be interested in the utilities found here: http://blog.bitfluent.com/post/27983389/git-utilities-you-cant-live-without. I haven’t actually configured them yet, but they look very useful.