Jan 24, 2014

Setting up a git server like a Paas


I have this new server that I wanted to use like the services offered by Paas's. After some disappointments with OpenShift and Appfog, I am now using Heroku. And I really love like it! On Heroku, you just Push with Git and your site is up-to-date.

So, until I had to install my own Git server, I never made the difference between the bare repository and the working tree.

Just to make things clear, the bare repo does not contain files, it's just the information about who did what on which file. It's the center/server of your git network. More details here
All the files you have on your repo are a working tree. You can't push to a working tree.

But if you can easily find free server services (Assembla, Bitbucket), it's not a deploy option like in Heroku.
If you are working on a web project, you would like to check the result "live" without having to update it manually.

This is what I did :
1. I installed git on the server
(yum or apt-get or ...) install git-core
2. I created a git user
adduser git 
It will set the new user with no password or '!!'. It may cause problems, so to be sure you can unlock the user
passwd -u git
3. I had to register my SSH key (switching to 'git' user is really important)
su - git
cd
mkdir .ssh
ssh-keygen -q -t rsa -N 'pass' -f ~/.ssh/id_rsa
ssh-add
# on CentOS you may need to 'exec ssh-agent bash' to avoid the 'Could not open a connection to your authentication agent.'

cat .ssh/id_rsa.pub >> .ssh/authorized_keys 
4. I did the work tree in a web-browser-accessible folder (public_html, www ...) and put a test file to avoid any bug related to cloning an empty repo
git init deploy
cd deploy
touch test
git add .
git commit -m 'first'
5. Now the bare repo. You should put in your /home/git folder
git clone --bare /home/user/www/deploy deploy.git
6. To clone it on my PC, I append my own SSH public key to .ssh/authorized_keys ...
#On the server
cat /tmp/id_rsa.pub >> .ssh/authorized_keys
You can also copy the content of your id_rsa.pub and paste with
nano authorized_keys
7. ... and cloned the repo on my machine
git clone git@myserver:/home/git/test.git
If you change your code and Push it, it will work but you won't change the deploy folder

8. I had a hook to the bare repo to tell update the live version
cd /home/git/deploy.git/hooks
touch post-receive
nano post-receive
Paste
#!/bin/sh
GIT_WORK_TREE=/home/user/public_html/deploy/ git checkout -f
 Save and exit, then make it executable
chmod +x post-receive
9. Done! You can push and it will appear on the web folder