Simple WordPress deploys using Git

A few weeks back, Clifton Griffin asked me a question about deploying WordPress sites:

I do not use Capistrano for deployments anymore, for one simple reason: it was massive overkill for most of the sites I manage, and maintaining it was not worth the benefit.

My current deployment system for WordPress sites is simple: I use Git.

I’m already using Git for version control of the site’s code, so using Git for deployments is not that much more work. There are a few ways to do this, but the simplest way is to just make your site root a Git checkout of your site files.

Then, if your server has read-access to your Git remote, you can run some Git commands to sync everything. Here are your options:

  1. git pull — Simple, but might fail if someone naughty has made code modifications on the server.
  2. git fetch && git reset –hard origin/master — The hard reset method will wipe any local modifications that someone has mistakenly made.

But wait. Before you implement this, it is very important that you ensure that your server’s .git directory is not readable, as it might be able to leak sensitive information about your site’s code. How you do this will depend on what web server you’re running. In Nginx, I do the following:

location ~ /\.(ht[a-z]+|git|svn) {
deny all;
}

In Apache, you could put the following in your .htaccess file:

RedirectMatch 404 /\.git

SSHing into your server every time is tedious, so let’s script that:

#!/bin/bash
ssh example.com 'cd /srv/www/example.com && git pull'

Save that to deploy.sh in your Git repo, run chmod +x deploy.sh, and commit it to the repo. Now when you’re ready to deploy the site, just type ./deploy.sh and the public site will pull down the latest changes from your main Git remote.

Bonus points if you make deploy.sh take an optional commit hash, so you can also use this tool to roll back to a previous hash, in case a commit goes wrong.

This method has served me well, for years, and has required no maintenance.

What methods are you using for WordPress code deploys?


Do you need WordPress services?

Mark runs Covered Web Services which specializes in custom WordPress solutions with focuses on security, speed optimization, plugin development and customization, and complex migrations.

Please reach out to start a conversation!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s