Monday November 19 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 want this to be
automatically updated to the latest commit on master every time you push.
The easiest, and most straightforward method of making this work is as follows:
$ cd /var/www/htdocs/mitchriedstra.com/site
$ git config receive.denyCurrentBranch "ignore"
$ ed hooks/post-receive
a
#!/bin/sh
set -e
# Note that you may want to adjust your path to include your custom binaries
PATH="$HOME/bin:$PATH"
# Requires `git config receive.denyCurrentBranch "ignore"` in order to be useful
unset GIT_DIR
unset GIT_WORK_TREE
cd ..
git reset --hard HEAD
bin/render ..
.
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:
- You can have multiple remotes
- Tags and branches are effectively the same thing on disk
- Git works based off of a “filesystem” snapshot view so-to-speak
- It’s the stupid content tracker. The manual even says so:
GIT(1) Git Manual GIT(1)
NAME
git - the stupid content tracker