K Cartlidge

C#/DotNet. Go. Node. Python/Flask. Elixir.

Push to deploy using Git

Deploying from source control to a server

A few years ago I switched to Git for source control.

Git Although I use C# and DotNet for my day job, my employers use Git in preference to TFS and once I became familiar with it I agreed more and more with that decision.

This site is created automatically from markdown-style content files, so changing the site is all done in the file system on my local Macbook Air or Surface Pro. I make changes locally and push them to the server (for TFS mavens, this is like checking in). Using push to deploy those changes can also make their way live automatically - when I reach the point where I'm ready, I push to the repository on my web server instead of the origin on GitHub and a git 'hook' automatically copies the result into place and bumps the server.

The way it works sounds lengthy, but is actually quite straightforward.

Setting things up

The process I follow

Provisos and options

Push-deploy permissions

By default, your html folder will be set up so that git cannot write to it. Edit the sudo users:

sudo visudo

Add the git user:

git ALL = (root) NOPASSWD: /var/www/html

Save and close the editor. You may need to add your user to the sudo group:

adduser <your-user> sudo

If you've reached this point and already have files deployed, remove them first:

sudo rm -r /var/www/html/*

You may also need to change the ownership of your web root:

sudo chown -R <your-git-user> /var/www/html

You can test your hook without pushing by:

cd [your git repository folder]
hooks/post-receive whatever triggered

Sample post-receive script

Your requirements will vary. Here is a small example.

#!/bin/bash

LOGFILE=./post-receive.log
TEMPDIR=/home/my-user/build
DEPLOYDIR=/var/www/html

mkdir -p $TEMPDIR

echo "PROCESSING GIT POST-RECEIVE"
while read oldrev newrev refname
do
    echo -e "Received Push Request at $( date +%F )" >> $LOGFILE
    echo " - Old SHA: $oldrev New SHA: $newrev Branch Name: $refname" >> $LOGFILE
    echo "Starting Deploy" >> $LOGFILE
    echo " - Starting code update"
    GIT_WORK_TREE="$TEMPDIR" git checkout -f
    echo " - Finished code update"
    echo "Finished code update." >> $LOGFILE
    cd $DEPLOYDIR
    cp -r $TEMPDIR/output/* .
    echo "Deployed (subject to any errors displayed)."
    echo "Finished deploy attempt." >> $LOGFILE
done

Your relevant paths are set at the top (no checking is done).

Notes

There are a few assumptions here, like knowing how to set up a repository or add SSH keys. I've not gone into more detail on those as it is out of the scope of this post, those details are easily found with your favourite search engine, and if this post is relevant to you at all then there's a good chance you are technically-minded enough to investigate further yourself.