How Do I Manage Repositories
Location
The repositories directory is located at /home/user/public_html/git.domain.com/.
Directory structure
Each project consist three different repositories (master, beta and live). Master repository will be named using project name. so I use this structure:
home
user
public_html
git.domain.com
projectname
projectname.git
beta.git
live.git
anotherprojectname
anotherprojectname.git
beta.git
live.git
Creating a repository
Create at least three git repositories for each project (projectname.git, beta.git and live.git). Also create sub domains for each repository because each repository will represent different deploy url.
Master
projectname.git is where the developers clone the repository (or you can say this is the “master repository for each project”), it has deploy hook to the domain under domain.com. The deploy url for projectname.git will be something like http://projectname.master.domain.com.
Create a folder named projectname.git under projectname directory and run git init to initialize an empty git repository git init --bare.
Next, activate the post-update and post-receive hook for projectname.git. We can use the existing hook files provided by git.
# These command executed from /home/user/public_html/git.user.com/projectname/projectname.git
mv hooks/post-update.sample hooks/post-update
chmod a+x hooks/post-update
mv hooks/post-receive.sample hooks/post-receive
chmod a+x hooks/post-receive
We don’t want to do anything else for post-update hook except to activate it. But for post-receive hook, we have to update the code a bit for automatic deployment.
Put this on post-receive hook:
git --work-tree=/home/user/public_html/projectname.master.domain.com --git-dir=/home/user/public_html/git.domain.com/projectname/projectname.git checkout -f
Beta
beta.git is used for beta testing, it also has deploy hook but to http://projectname.beta.domain.com. Deploying to beta is executed not by the developers.
Create a folder named beta.git under projectname directory and run git init to initialize an empty git repository git init --bare.
Next, you need to activate the post-update and post-receive hook for beta.git. We can use the existing hook files provided by git.
# These command executed from /home/user/public_html/git.domain.com/projectname/beta.git
mv hooks/post-update.sample hooks/post-update
chmod a+x hooks/post-update
mv hooks/post-receive.sample hooks/post-receive
chmod a+x hooks/post-receive
We also don’t want to do anything else for post-update hook except to activate it. But for post-receive hook, we have to update the code a bit for automatic deployment.
Add this on post-receive hook:
git --work-tree=/home/user/public_html/projectname.beta.domain.com --git-dir=/home/user/public_html/git.domain.com/projectname/beta.git checkout -f
For deploying from projectname.git to the beta url, you have to add git remote to the beta.git from projectname.git.
# This command executed from /home/user/public_html/git.domain.com/projectname/projectname.git
git remote add beta ../beta.git
To deploy from projectname.git, simply run command git push beta master from /home/user/public_html/git.domain.com/projectname/projectname.git.
Live
live.git is used for live version when all bugs was solved during the beta testing. live.git has deploy hook to http://projectname.domain.com. Deploying from projectname.git to live.git also not executed by the developers. Someone that has access to the server with SSH should do this.
Create a folder named live.git under projectname directory and run git init to initialize an empty git repository git init --bare.
Next, you need to activate the post-update and post-receive hook for live.git. We can use the existing hook files provided by git.
# These command executed from /home/user/public_html/git.domain.com/projectname/live.git
mv hooks/post-update.sample hooks/post-update
chmod a+x hooks/post-update
mv hooks/post-receive.sample hooks/post-receive
chmod a+x hooks/post-receive
Put this on post-receive hook:
git --work-tree=/home/user/public_html/projectname.domain.com --git-dir=/home/user/public_html/git.domain.com/projectname/live.git checkout -f
For deploying from projectname.git to the live url, you also need to add git remote to the live.git from projectname.git.
# This command executed from /home/user/public_html/git.domain.com/projectname/projectname.git
git remote add live ../live.git
To deploy from projectname.git, simply run command git push live master from /home/user/public_html/git.domain.com/projectname/projectname.git.
Let me know what you think of this article on twitter @mtasuandi or leave a comment below!