How do I Post | Toke's page

How do I Post

Or how this site is updated

This site is build via jekyll and the sources are coming from a git repository on github. I do not write something special about git or jekyll here. Just how the “deployment” works.

Github webhook

Github has a nice feature called “Service hooks” - especially a “Post-Receive URLs” hook. I use this hook to notify my server to do a “git pull” on a local repository, then rebuild the site with jekyll and rsync changed data to the document root.

The webhook endpoint:

package ReceiveHook;
use Dancer ':syntax';

our $VERSION = '0.3';

# Configuration for different projects
my $config = {
    "PROJECTNAME" => {
        run => "/home/user/bin/updateblog.sh",
    },
};

prefix '/notify';

get '/*' => sub {
    header 'Allow' => 'POST';
    status '405';
    "Not for you\n";
};

post '/:project' => sub {
    if (not defined $config->{params->{project}}) {
        status 'not_found';
        return "No such project: ".params->{project}."\n";
    }

    my $payload = params->{'payload'};
    if (not defined $payload) {
        status '415';
        return "I need a payload\n";
    }

    # Read the configuration for that repo
    my $repo_config = $config->{$repo->{name}};
    if (defined $repo_config && defined $repo_config->{run}) {
        eval {
            system $repo_config->{run};
        };
    }

    return "OK";
};

true;

The update itself is done by this little shellscript:

#!/bin/sh

REPPATH="/home/user/git/localgitrepository"
DEPLOYTARGET="/var/www/sites/mydomain"
LIVEURL="http://mydeploydomain.tld"

if [ -d ${REPPATH} ] ; then
    cd "${REPPATH}"
    git pull
    jekyll --url "${LIVEURL}"
    rsync -rXogpEAt _site/ "${DEPLOYTARGET}"
fi

To be continued…

Posted at by toke