Deployment with Git Hooks

From lathama
Jump to: navigation, search
Devops without the extra parts

You want to deploy code onto a server into a certain directory. You have SSH to the server and it has Git installed. Your desired destination is /home/user/public_html/production/

Target server

on the remote server lets create a directory, and create a bare repo

cd /home/user/
mkdir myproject
mkdir -p public_html/production
cd myproject
git init --bare

Next lets copy the sample hook to activate it and open it in an editor, use what ever editor you like.

cp hooks/post-update.sample hooks/post-update
<editor> hooks/post-update

Add/change it to be something like this.

branch=$(git rev-parse --symbolic --abbrev-ref $1)
if [ "production" == "$branch" ]; then
  GIT_WORK_TREE=/home/user/public_html/production git checkout -f production
  echo "exported to production"
Local system

Clone or add as remote to your existing repo

git clone ssh://

make a production branch and push.. (note your origin config if you are using many remotes)

cd myproject
git checkout -b production
git add README
git commit -m "Andrew is so nice"
git push origin production

You should see something like

Counting objects: 1, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (1/1), done.
Writing objects: 100% (1/1), 334 bytes | 0 bytes/s, done.
Total 1 (delta 0), reused 0 (delta 0)
remote: exported to production
To ssh://
   ad720c2..8e42f1b  production -> production

So now all you have to do is develop in your development or feature branches and maybe merge to master. When it is time to deploy merge to production and push!

Sponsorship and Advertising space here. Please contact me if interested.