Deploying A Drupal Site With Git and Capistrano

This is my first attempt to write a definitive guide on how to set up Drupal and how to deploy a Drupal site using Git and Capistrano.

**Why bother using Git and Capistrano when you can just upload via FTP? Isn’t it needless effort if you’re a lone developer and you have good retention of the things you have done? **

I have been pretty much a lone developer working on Ruby on Rails applications and recently, a Sinatra application for my own ventures and for my client. In my case, I have all the reasons to have a workflow for myself and the people I work (and will work) with. Git and Capistrano are tools for people who are not so self-assured and wise enough to make sure that small changes cannot or will not cause chaos. Sure, you can just backup locally using copy and paste keys but in this guide, I will show you why it’s a good a idea to create at least two git repositories for a project.

**How can Drupal developers benefit from using git and capistrano? **

With SCM (like Subversion or GIT) system, it is to track each developer’s changes and contributions to a project. Managing change and coping with catastrophe is easier for a team that uses GIT.

Capistrano is a great tool for automating tasks. For Ruby developers, there are a lot of system adminsitration tasks which can be automated using Capistrano and Deprec. The deprec gem is a set of tasks for Capistrano that provide for the installation, configuration and control of system services on servers running Ubuntu linux. You can install nginx, mysql, postgresql and so on using capistrano commands. Pretty cool.

Let’s start with GIT. Install GIT on your local machine if you haven’t yet.

For Unix, particularly, Debian or Ubuntu users:

ruby sudo apt-get install git-core

Create your project’s directory

rubymkdir project

Initialize your repository

cd project && git init

Create a gitignore file

touch .gitignore

If you want to create a directory for backups within the folder and you don’t want that submitted on the git repository, edit the .gitignore file and add /backups or the name of directories and folders you want to ignore.

My .gitignore file has the following:

backups/*
tmp/*
files/*
sites/default/settings.php
*~
\#*
.#*

Now it’s time to add the Drupal project files on the directory. If you’re starting a simple Drupal project, I suggest downloading the 6.x version. Download the most important modules like pathauto, token, cck, filefield, subscriptions and imagefield. For my very simple project, that’s pretty much all I need. I don’t need meta tags module or anything SEO-related as this is a private project.

For Drupal newbies, you need to create modules and themes directory within sites. Add all the modules and themes you need for the project.

cd project && mkdir sites/all/modules
cd project && mkdir sites/all/themes

Create and edit your settings file in /sites/default.

Set up a git repository on your server. Make sure git is installed by calling git –version. It hardly matters what version you are using.

mkdir git && mkdir git/project.git
cd git/project.git && git --bare init

mkdir git/project_backup.git
cd git/project_backup.git && git --bare init

Multiple Remotes with Git

On your local project folder, you have to add the sources previously created.

git remote add origin git@yourhostname.com:~/git/project.git
git remote add backup git@yourhostname.com:~/git/project_backup.git

Submit your files to the branch, “master”.

git status
git add .
git commit -m "Initial commit"
git push origin master

And you’ll get something like this:

Counting objects: 1417, done.
Compressing objects: 100% (1389/1389), done.
Total 1417 (delta 139), reused 0 (delta 0)
 * [new branch]      master -> master

Capistrano Configuration for Drupal

Capistrano is a ruby gem so you need to install Ruby, Ruby gems and capistrano (gem install capistrano) on your local machine.

Let’s start with capistrano by creating configuration file.

capify .

This will create the /config/deploy.rb. Edit that file. Click here to download the configuration file.

cap deploy:setup #setup the directories on the server
cap deploy

These commands should work well for you if you’re deploying on shared host like dreamhost. Remember to keep “set :use_sudo, false” on your configuration file. Do not append “sudo” on the commands if you are using shared hosting.

Additional Drupal Installation Notes

Installing Drupal for the first time

cd sites/default
copy and rename default.settings.php to settings.php 
#Note: Do not make any changes to settings.php. 
# chmod 755 settings.php to make it writable
git add sites/default/settings.php -f 
#Note:Since this is an ignored file, we have force add the file.

Migrating from local server to production server

Import the sql file to your production server’s database.

git add sites/default/settings.php -f 
#Change the details. Since this is an ignored file, we have to force add the file.

Adding New Modules

Twitter is interesting. So I downloaded the twitter module. Dropped the module on sites/all/modules. Naturally, that’s where it should be.

katz@katz bridgeresources :) $ git status
# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#
#	modified:   config/deploy.rb
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	sites/all/modules/twitter/
no changes added to commit (use "git add" and/or "git commit -a")
katz@katz bridgeresources :( $ git add .
katz@katz bridgeresources :) $ git commit -m "Added the twitter module"
Created commit f07ad6f: Added the twitter module
 16 files changed, 2843 insertions(+), 2 deletions(-)
 create mode 100644 sites/all/modules/twitter/CHANGELOG.txt
 create mode 100644 sites/all/modules/twitter/LICENSE.txt
 create mode 100644 sites/all/modules/twitter/twitter.api.php
 create mode 100644 sites/all/modules/twitter/twitter.inc
 create mode 100644 sites/all/modules/twitter/twitter.info
 create mode 100644 sites/all/modules/twitter/twitter.install
 create mode 100644 sites/all/modules/twitter/twitter.js
 create mode 100644 sites/all/modules/twitter/twitter.module
 create mode 100644 sites/all/modules/twitter/twitter.pages.inc
 create mode 100644 sites/all/modules/twitter/twitter.views.inc
 create mode 100644 sites/all/modules/twitter/twitter.views_default.inc
 create mode 100644 sites/all/modules/twitter/twitter_actions/twitter_actions.info
 create mode 100644 sites/all/modules/twitter/twitter_actions/twitter_actions.module
 create mode 100644 sites/all/modules/twitter/twitter_actions/twitter_actions.rules.inc
 create mode 100644 sites/all/modules/twitter/twitter_views_field_handlers.inc
katz@katz bridgeresources :) $ git push origin master
katz@katz bridgeresources :) $ cap deploy