Recently I have discovered the beauty of git hooks for rapid deployment while developing websites hosted on my own server. This takes advantage of public key authentication with ssh (a means of logging in remotely with a key rather than using a password). I also have drush aliases configured so that I can run commands on the website and synchronize file and database changes in an automated fashion, however this is a topic for another series of posts in the future. I have made use of the post-receive hook in git so that when a commit is pushed to the remote repository, the code is automatically deployed to the server's web directory.

Variations of this script could be used to deploy from different branches to other web directories for a development, test, and production environment. To set up your origin repository you will need to first create a git repository for housing your code. Keep in mind that the command I will show you creates a bare repository which means it does not have a working directory, and you will not be able to do development on the server in any fashion. (If you make changes to the web directory they can easily be replaced by new code as you make changes to the repository)

Make a directory outside of the web root on the server (within the home directory or /var/repos will work). Make sure the directory permissions are 755 and owned by the user who has a ssh key on the account.

$ mkdir project.git
$ cd project.git
$ git init --bare

Edit hooks/post-receive and place the following content inside:

#!/bin/sh
git --work-tree=/home/projectaccount/public_html --git-dir=/home/projectaccount/project.git checkout -f

Finally, make the file you just made executable:

$ chmod +x hooks/post-receive

On your development workstation

$ git clone projectaccount@myprojectserver.com:~/project.git project
$ cd project
$ touch README.txt
$ git add .
$ git commit -m "Initial Commit"
$ git push

Now you should be able to browse to myprojectserver.com/README.txt if you allow text files to be served by your host. You can now take these minimal steps to set up your development environment when deploying websites that will require on-going work.

Author

arosborough's picture

Andrew Rosborough

VP of Software Development