Automated Git Project Deployment

November 11, 2016 | Personal Projects, Web Development

git-automated-deployment

While there are plenty of apps that can handle deployment for you (such as Heroku) or Beanstalk (for general workflow control and project deployment), with a simple script you can automate your Git project deployment to be as easy as typing a few words into your terminal! In this post I’ll walk you through how to set up automated git project deployment.

Requirements

– A web server to host your site or project (I’m using a basic HostGator web package to host this site)

– SSH access to your remote server (find your web host’s docs on how to setup SSH access)

– Git installed on your server (check with your hosting provider to see if this is already installed).

1. Create the directory where your project will live

In your terminal, SSH into your remote web server and create a new directory:

2. Create a bare Git repository

CD into the new directory and create a bare Git repository:

Here’s a more in depth explanation of the differences between bare and regular repositories, but essentially a bare repository doesn’t have a working tree and is only used for sharing and tracking code changes. Since this is going to be our primary repository in this example, we want it to be a bare repository.

3. Create the post-receive hook

A post-receive hook is a snippet of code that runs after a Git repository has received commits. In this case, we use the post-receive hook to change our working directory to the directory on the server and check out a copy of the build into that directory.

The last line makes sure we have our file permissions set correctly for the hook.

4. Add the new remote to your repository

Once your remote repo is setup, and the remote to your local development repo and presto! You’ll be able to push your work to this remote and deploy it on your webserver.

Below is a BASH script by Richard Westenra, which automates this process very well. Copy this script onto your server, save it as a .sh file (eg filename.sh) and run it via SSH from your terminal using bash filename. Note: I’ve changed the folder paths in here to work on my server – you’ll likely need to adjust them if you want them to work on your server. I’ve bolded them below.

This is great for static projects, but you might have some trouble with more advanced projects. For example, if you implement this with a WordPress project, you may also need to also migrate the database when you deploy work (either manually or using a plugin such as MigrateDB Pro). For other projects, such as a Rails project, you may be better off using a deployment app such as Heroku so you don’t have to spend time managing your dependencies and environment.

In a subsequent post, I’ll expand on this and talk about a script I made for my company to further automate this process. I also added in security via .htaccess to prevent unwanted users from peering on deployed work.

Sources:

Simple Git Deployment Strategy for Static Sites

Automating Git Deployment

Further Reading:

Git on the Server

Leave a Reply

Your email address will not be published. Required fields are marked *