Our production environment is running a Debian 6 Squeeze box. To make deployments as easy as possible we decided to setup GIT on our local virtual machines and to push directly to production after testing.

This is how we did it:

Local machines Vagrant and Virtual Box

First we need to install on our local machine Vagrant and VirtualBox.

Vagrant is a tool to easily create development environments. So let’s grab a copy and install it:

http://www.vagrantup.com/downloads.html

I’m using Mac, but for other flavours there are installers to. The installation is straightforward.

VirtualBox

The next thing we need to install is VirtualBox because Vagrant uses that for virtualisation. Let’s grab that one here:

https://www.virtualbox.org/wiki/Downloads

Again I’m using Mac but for other distros the installation should be straightforward.

Configuring Vagrant to resemble your production machine

The first thing we need to do is to find a Debian Squeeze box to add to our virtual machine. You can find the boxes (distributions) here:

https://vagrantcloud.com/discover/featured

To add the distribution to vagrant type in the following command. Of course change the distribution name to fit your needs. I’m using ffuenf/debian-6.0.9-amd64

$ vagrant box add ffuenf/debian-6.0.9-amd64

Next choose 1. for VirtualBox

Now initiate the box with:

$ vagrant init ffuenf/debian-6.0.9-amd64

Edit the box name with the name that fits your distribution.

Now with the following command command you can bring the virtual machine up:

$ vagrant up

Now SSH directly into the machine:

$ vagrant ssh

Now sudo su to install the software packages you need to resemble your production environment.

$ sudo su

Now you have a copy of your production environment it’s time to save it as an internal box and configure Vagrant to use that box as the standard. For that we need to package the currently running VirtualBox environment into a re-usable box.

Run:

$ vagrant package

The new package will be saved as package.box.

Now edit your Vagrantfile to let it start-up as the saved box. The Vagrantfile is located in the home directory of the user you are installing Vagrant with.

Change this line to read:

# Every Vagrant virtual environment requires a box to build off of. config.vm.box = "package.box"

It’s worth it to examine the Vagrant file, you can change lots of things there e.g. the port numbers the virtual machines listens to etc.

Now we have that in place we can start installing GIT and creating the right infrastructure for our migration process.

Installing GIT on the production server

Let’s install GIT

apt-get install git

Let’s create a repository for our production code

mkdir /opt/git && cd /opt/git

mkdir projectname.git && cd projectname.git

Now let's create a empty repostitory:

git --bare init

Now make a copy of your production code on your local development environment (your Vagrant virtual machine). E.g. /home/projectname

Install GIT on your local development environment

apt-get install git

cd into your project directory

cd /home/projectname

git init

cd .git

Create a .gitignore with all the stuff you don’t want (important don’t forget, specially if it’s going to be public remove all configuration passwords, database configs etc..)

vim .gitignore

Mine looks something like this:

## generic files to ignore *~ *.lock *.DS_Store *.swp *.out ## ignore files and folders we don't want public_html/somefiletoignore.html public_html/somedirtoignore/*

Make sure the .gitignore file is in the .git directory so other repositories wil not ignore it.

Now add all the files to the stage

git add -A

Commit

git commit -m "initial commit"

Add a remote

git remote add projectname user@yourproductionserver:/opt/git/projectname.git

Now make sure the user doing the SSH session has the right permissions to write to /opt/git/* or else the transfer will not work.

Now just push to the production server

git push projectname master

At this point, the others can clone it down and push changes back up just as easily:

$ git clone user@yourproductionserver:/opt/git/projectname.git $ cd projectname $ vim README $ git commit -am 'fix for the README file' $ git push projectname master

Create a post-receive hook to update your production environment

Now let’s say your production environment is on /home/projectname and you want to checkout your files to that directory:

$ vi /opt/git/projectname.git/hooks/post-receive

#!/bin/sh GIT_WORK_TREE=/home/projectname git checkout -f

Save the file. Now everytime you push en new version, your production environment get’s updated.

Summary

To startup your Vagrant development environment do:

$ vagrant up

To commit changes to your production environment do:

$ git commit -am “New change done!” $ git push projectname

To revert to the previous commit because you screwed up:

$ git reset HEAD^ —hard $ git push projectname -f

This is a great way to work on your local machine and migrate to production environments without to much hassle.

That’s all, I hope it was useful for somebody. If you have any comments please leave them downhere.