Drupal Development and Deployment using Git

posted in: drupal, git, wordpress | 0

Most of the current CMSs mix their “bare” source code with configuration data, third party plugins, themes, etc. One example is Drupal where your settings.php configuration is found under the sub folder sites/default, third party themes under themes and third party modules under sites/all/modules. If you want to have your code in a version control system and at the same time support easy updates to all those software parts, a clever setup is required.

WordPress is similar, e.g. the “wp-content” folder – hence the approach shown also applies to WordPress.

This guide is based on the Git WebDAV setup from kernel.org – if something doesn’t work for you, especially in the first part of this guide, you may find an answer there.

Install Git on the server for WebDAV

First you have to initialize a new Git repository on the server. I’m using the name server-repo in this example. Change it if you like 😉

sudo mkdir /var/git
cd /var/git
sudo mkdir server-repo
cd server-repo
sudo git init --bare
sudo git-update-server-info
cd ..
sudo chown -R www-data:www-data server-repo

We also need the WebDAV users file (replace username with your user name):

sudo htpasswd -c /var/git/server-repo.gitusers username

Now add the following to your apache config:

Alias /git /var/git
<Location /git/server-repo>
  DAV on
  AuthType Basic
  AuthName "Git repository"
  AuthUserFile /var/git/sever-repo.gitusers
  Require valid-user
</Location>

Restart Apache:

/etc/init.d/apache2 reload

Create an new repository locally

This part is similar to the method shown by the Version Control Blog, except I will not use cloning but branching instead.

So, let’s create an empty Git repository on your development machine:

cd ~
mkdir local-repo
cd local-repo
git init

Create a branch for Drupal:

git checkout -b drupal

… and extract the drupal installation archive into your local repository:

tar xvzf drupal-6.5.tar.gz
mv drupal-6.5 drupal

Great, let’s commit this branch:

git add .
git commit -a -m "Initial Drupal commit"

The drupal branch will contain the “pure” drupal source code. Nothing else should ever end up in this branch.

In order to add modules to your drupal installation, let’s create an new branch containing drupal plus all modules you require:

git checkout drupal
git checkout -b modules

Create the modules folder under sites/all an copy all your modules inside:

cd drupal/sites/all
mkdir modules
cd modules
tar xzvf yourmodule.tar.gz
...

Now comes a third branch with all your changes, e.g. .htaccess, own themes, modifications to the Drupal core, the modules used, etc. I’ll name this branch “production” and it’s based on the modules branch.

git checkout modules
git branch -b production

Inside this branch your first want to create the sites/default/settings.php file and create the files folder – basically everything you do when you setup a new Drupal based web site.

That’s the basic setup.

Pushing your local repository to the server repository

Set the remote repository URL on your server:

git-config remote.origin.url http://your-server/git/server-repo/

First lets push the drupal branch:

git checkout drupal
git push origin drupal:drupal

… and the other two branches:

git checkout modules
git push origin modules:modules
git checkout production
git push origin production:production

Performing updates

If there is a new Drupal version you checkout the drupal branch and extract drupal there:

git checkout drupal
rm -rf drupal
tar xvzf drupal-6.6.tar.gz
mv drupal-6.6 drupal

… and commit your modifications to the drupal branch and push it to the server.

git add .
git commit -a -m "Drupal 6.6 update"
git push

Next you’ll have to pull the updated drupal branch into the other two branches:

git checkout modules
git pull . drupal
git checkout production
git pull . modules

And push it to the server repository:

git push

Deploy it to the server

Simply ssh to your server and clone the server repository and checkout the production branch:

git clone http://your-server/git/server-repo/
cd server-repo
git checkout remotes/origin/production
git checkout -b production

And for updates:

git pull origin production

Hints

On authenticated WebDAV Git currently only supports netrc. So you have to create a .netrc file in your home directory:

machine your-server
login username
password your-password

If you are using a self-signed certificate on your web server and get errors use the following …

GIT_SSL_NO_VERIFY=1 git pull ...

… or if you already have a local repository:

git-config http.sslverify false