GIT Work Flow and Deployment of sites using Git

My working environment is as follows:

– I work mostly all time on laptop with Windows 7 because I work on projects in both .NET and LAMP.

– I have a virtual machine with Ubuntu server (using VMWare) to test LAMP projects in Linux enviroment.

– Amazon EC2 instance with Ubuntu for testing my sites in the Internet.

 

Architecture:

– working copies of sites on my local PC;

– two repositories on the server: hub and prime;

– all working repositories work with a hub-repository;

– Live sites are run from prime-repository;

 

Work flow:

– Changes made to projects locally in working copies are pushed to the hub-repository.  Changes are automatically aplplied from hub-repository to prime-repository where live sites are.

– Changes can be also pushed to the prime-repository. In this case the changes are automatically applied to the hub-repository. So working copies of project continue to work with the last version.

 

This solution has the following advantages:

– I can test my changes immediately in the Internet after I push to the remote hub-repository.

– Changes to the live copy of site do not break the system.

 

Below is a step-by-step description how to setup this system.

Install Git

First read my post how to install Git on Windows and Ubuntu.

 

We need two repositories for a hub and prime:

– /var/www/site1 – prime-repository, live version of site,

– /home/git/repos/site1_hub.git – hub-repository, (bare repository).

Git hub-repositories will be at /home/git/repos.

We create a git user and add users to the git group.

Setup permissions for git

sudo useradd git
sudo usermod -a -G git myuser

Make the git directory readable and writeable by the git group:

sudo chmod -R g+rwX /home/git

Make all files and directories created under /home/git to retain the git group:

sudo chmod g+s /home/git

Create the folder for repositories:

cd /home/git
mkdir repos
sudo chown git /home/git/repos
sudo chgrp git /home/git/repos

 

Setup repositories

Init prime-repository:

cd /var/www
mkdir site1
cd site1
git init
git add .
git commit -m "first init"

Init hub-repository:

cd /home/git/repos
mkdir site1_hub.git
cd site1_hub.git
git --bare init

You may create a hub-repository somewhere outside www-folder.

Setup prime-repository

Add the hub-repository as a remote repo for the prime-repository and pull changes from the prime to the hub.

cd /var/www/site1
git remote add hub /home/git/repos/site1_hub.git
git push hub master

 

Setup hooks to synchronize repositories

 

post-update hook in the hub-repository

Edit or create the file /home/git/repos/site1_hub.git/hooks/post-update

#!/bin/sh
echo
echo "**** get changes for the Prime [Hub's post-update hook]"
echo
cd /var/www/site1 || exit
unset GIT_DIR
git pull hub master
exec git-update-server-info

 

Set the permissions:

chmod +x /home/git/repos/site1_hub.git/hooks/post-update

 

post-commit hook in the prime-repository

Edit or create the file /var/www/site1/.git/hooks/post-commit

#!/bin/sh
echo
echo "**** pushing changes to Hub [Prime's post-commit hook]"
echo
git push hub

 

permissions on .git directory

deny access to .git directory. Create .htaccess file inside .git directory:

RewriteEngine On
RewriteRule \.git - [F,L]

 

 

Read also:

* Setting up Rails, MySQL, PHP, Apache, and Git on EC2 – http://codelikezell.com/setting-up-rails-mysql-php-apache-and-git-on-ec2/