Date: Mon Nov 19 22:13:21 EST 2018

Automatic deployment via git push

The corporate solution to this is “Use Jenkins” or some other bloated third party piece of software instead of the simple straightforward solution of using a small shell script and git’s powerful built in hooks.

The easiest way to show you how this works is with a couple of examples.

You have a git repository in /var/www/htdocs/mitchriedstra.com/site which contains your website’s source code or markdown files. ( You are using my awesome un-framework aren’t you? ) You want this to be automatically updated to the latest commit on master every time you push.

Although you could do some hacking with the Git Environment variables to make this work without duplicating the repository, IMO the easiest, and most straightforward method of making this work is as follows:

$ export orig_repo="/var/www/htdocs/mitchriedstra/site"
$ cd "$orig_repo"
$ git clone --bare "$orig_repo" ~/site
$ cd ~/site
$ ed hooks/post-receive
a
#!/bin/sh
set -e
PATH="$HOME/go/bin:$PATH"

site_dir="/var/www/htdocs/mitchriedstra.com/site"
remote="local"
branch="master"

unset GIT_DIR
unset GIT_WORK_TREE

cd "$site_dir"
pwd
set -x
git pull --ff-only "$remote" "$branch"
# In the case of the "framework" I'm using I need to run the bin/render
# command before the updated content is accessible to the world. If
# the application your using has other command you can put them here.
bin/render ..

# You might have a build script you wish to call them with '&' to fork
# to the background
.
w
q
$ chmod +x hooks/post-receive
$

And that’s about it. This assumes the work is done on the remote host in which you’re deploying to.

If you really need me to hold your hand setting up the local repository:

$ git clone username@remote.your.host:~/site site

And that’s about it. All you have to do is commit to your local branch and git push to see them on the server.

A few things you might not know if you’re new to git and haven’t read the excellent documentation:

GIT(1)                            Git Manual                            GIT(1)

NAME
       git - the stupid content tracker