Simple git deployment

I haven't used this in a long while. Now a very happy CodeShip user. Check them out.

Git

This is a pretty simple setup för deploying with git, but might be useful for smaller projects. There are tons of ways of doing git based deployments, this is just one simple way. This doesn’t cover more advanced stuff.

This method uses git-hooks, it is the Git way to fire off custom scripts when certain important actions occur.

This example requires that you have access to your server via ssh and has git installed. You need to have your ssh key on the server and also access to your server without password. I you pay for a hosting company that doesn’t allow you to do this, reconsider changing host. I recommend using a VPS, where you are in charge.

So, some requirements:

  • A server
  • SSH access
  • Git is installed on your server

Connect to your server

ssh user@53.142.131.80

Do into your website folder and create a new git repo

cd /var/www/mysite.com
git init

You can also use bare repos. Bare repos only contains the .git folder itself, with no working tree attached to it (no files). In this case, you have two folders. One folder containing your git repo and another directory where you check out your files. In this case use git init --bare. In this example i’am going to use a regular git repo. Read more about git bare repos, här och här

On your local system, add this repo as a remote repo. I will call it “production”, but you can call it what you want.

git remote add production user@53.142.131.80:../../var/www/mysite.com

On your server. Navigate to the hooks folder inside your git repo (.git/hooks) and rename the file ``post-receive.sample to just post-receive`

mv post-receive.sample post-receive

Edit this file and add

#!/bin/sh
cd ..
GIT_DIR='.git'
umask 002 && git reset --hard

#!/bin/sh
git checkout -f

Set the correct file permissons

chmod +x .git/hooks/post-receive

Configure git

git config receive.denycurrentbranch ignore

This setting removes the standard warning messade when an updated branch is pushed to the remote repo

Deploy

You’re now ready to make your first git deploy.

git push production master