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
[codesyntax lang=”bash”]
sudo useradd git
sudo usermod -a -G git myuser
[/codesyntax]
Make the git directory readable and writeable by the git group:
[codesyntax lang=”bash”]
sudo chmod -R g+rwX /home/git
[/codesyntax]
Make all files and directories created under /home/git
to retain the git group:
[codesyntax lang=”bash”]
sudo chmod g+s /home/git
[/codesyntax]
Create the folder for repositories:
[codesyntax lang=”bash”]
cd /home/git
mkdir repos
sudo chown git /home/git/repos
sudo chgrp git /home/git/repos
[/codesyntax]
Setup repositories
Init prime-repository:
[codesyntax lang=”bash”]
cd /var/www
mkdir site1
cd site1
git init
git add .
git commit -m “first init”
[/codesyntax]
Init hub-repository:
[codesyntax lang=”bash”]
cd /home/git/repos
mkdir site1_hub.git
cd site1_hub.git
git –bare init
[/codesyntax]
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.
[codesyntax lang=”bash”]
cd /var/www/site1
git remote add hub /home/git/repos/site1_hub.git
git push hub master
[/codesyntax]
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
[codesyntax lang=”bash”]
#!/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
[/codesyntax]
Set the permissions:
[codesyntax lang=”bash”]
chmod +x /home/git/repos/site1_hub.git/hooks/post-update
[/codesyntax]
post-commit hook in the prime-repository
Edit or create the file /var/www/site1/.git/hooks/post-commit
[codesyntax lang=”bash”]
#!/bin/sh
echo
echo “**** pushing changes to Hub [Prime’s post-commit hook]”
echo
git push hub
[/codesyntax]
permissions on .git directory
deny access to .git directory. Create .htaccess file inside .git directory:
[codesyntax lang=”bash”]
RewriteEngine On
RewriteRule \.git – [F,L]
[/codesyntax]
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/
One thought on “GIT Work Flow and Deployment of sites using Git”