STEP 1: Web Server - Code Repo
For this example, I’ll use a fictitious blog web site… blog.git
cd ~
mkdir code
cd code
mkdir blog.git
git init --bare blog.git
STEP 2: Web Server - Live Site Folder
Assuming that the path for our live site is /var/www/html/blog
cd /var/www/html
git clone ~/code/blog.git blog
# now protect the .git folder from public viewers
cd blog
chmod -R og-rx .git
STEP 3: Create a Local SSH Alias to the Web Server
In order to simplify deployments, I’ll set up an SSH alias that points to a particular private key for my SSH connection to the web server. This allows us to do Git pushes/commits without having to enter the server’s username/password.
Create an empty, or append to the, config file in your user’s .ssh folder .ssh/config and paste the following text. Update IdentityFile with the correct path to your private key. This example uses / in a windows enironment under MINGW32 (Git Bash).
Host webserver
HostName webserver.com
User username
IdentityFile /c/Users/myname/.ssh/my_custom_private_rsa
Now you can use “webserver” like in $ ssh webserver to connect to webserver.com using the local private SSH key.
STEP 4: Local Dev Machine
Now that we have created an ssh shortcut, alias, to webserver.com, the following command can be re-written like this:
git clone ssh://username@webserver.com/home/username/code/blog.git ./blogdev
to
git clone ssh://webserver/home/username/code/blog.git ./blogdev
STEP 5: Automate Deploys (Server-side Hooks)
This example uses the Git server-side hook, post-receive /code/blog.git/hooks/post-receive
Git Hooks - http://git-scm.com/book/ch7-3.html
touch /home/username/code/blog.git/hooks/post-receive
chmod u+x /home/username/code/blog.git/hooks/post-receive
nano -w /home/username/code/blog.git/hooks/post-receive
…and paste the following code:
#!/bin/bash
#CONFIG
LIVE="/var/www/html"
read oldrev newrev refname
if [ $refname = "refs/heads/master" ]; then
echo "===== DEPLOYING TO LIVE SITE ====="
unset GIT_DIR
cd $LIVE
git pull origin master
echo "===== DONE ====="
fi
That’s it! You can now start coding locally and pushing to the live site like $ git push origin master
This post was inspired by http://www.saintsjd.com/2011/03/automated-deployment-of-wordpress-using-git/