Automatic Drupal site deployment with git

Automatic Drupal site deployment with git

Submitted by Jitesh Doshi on Sat, 02/18/2012 - 09:51

In this article I will show how to setup git and Drupal in such a way that you can keep editing your site's modules, themes and libraries on your local machines and every time to do 'git push' it automatically deploys to your pubilc Drupal site - without having to login into your webhosting provider and doing checkout/reset on your working tree.

Assumptions

  1. You're using Git for version control and Drupal for site hosting.
  2. You're hosted in a place where you have ssh access and git installed on the server.
  3. You already know the basics of Git and Drupal.

Setup

  1. Your site is example.com hosted on a remote server (Linux).
  2. Your Drupal site directory on the remote server is ~/drupal/sites/example.com.
  3. You have some modules/themes/libraries etc. in ~/drupal/sites/example.com.

Steps

On remote server:

cd <drupal-root>/sites/example.com
git init

This turns your site-dir into a git repository and a git working directory (non-bare). Now on your local machine:

git clone ssh-username@example.com:~/drupal/sites/example.com

Make some code changes, commit locally, and then try to push.

... make changes ...
git commit -am'your commit message'
git push

Oops! Push is rejected, with a bad error message. That's because a non-bare Git repository does not accept pushes automatically. To fix that do this on remote server (git repo:

cd ~/drupal/sites/example.com
git config
git config receive.denyCurrentBranch ignore

Now push again. On local machine:

git push

It should work this time. Now your remote repository has your changes, but those changes are sitting in the repository database, not in the working directory. For automatic deployment, you need every push from local machine to also cause the server to update the working directory.

That is where Git hooks come in. One of the Git hooks is post-receive hook. It executes every time a push is accepted. On the remote server, put the following in ~/drupal/sites/example.com/.git/hooks/post-receive

#!/bin/sh

# all hook scripts start out in repo-dir/.git, so cd to repo-dir
cd ..

# set the directory that contains git metadata
GIT_DIR='.git'
 
# protect files before creating them and then to reset --hard to checkout
umask 002 && git reset --hard

# now clear Drupal cache
export DRUSH_PHP=/usr/local/bin/php5
~/drush/drush cc all 

In the above script we are checking out the files in the working directory (Drupal site-dir) and then clearing Drupal cache using Drush. That last step is optional, in case you prefer clearing cache from browser UI.

That's it. From here on, you can make changes to your local Git repository and every time you do git push, your changes are reflected in the live site (remote git repository).

Jitesh Doshi

Profile picture for user Jitesh Doshi
Managing Partner & CTO
  • A seasoned technology entrepreneur and enthusiast
  • A regular speaker at industry conferences and universities
  • Host and organizer of technology user groups
  • Active in management of non-profit organizations serving the local community
  • Leader and contributor for multiple open-source projects
  • Expert in cloud, application integration, web and mobile technologies
  • Author of open-source projects, including on Drupal.org - Popular Tags and PRLP.
  • Developed several highly successful software platforms and frameworks for clients