Oct 12, 2014

Deploying Jekyll website with GitLab

Deploying Jekyll website is really simple. Just download latest version and build it. But you can still come across some problems, just like me..

The first thing I’ve made was listener for webhook from GitLab. This is it:

<?php

$token = 'xxxxxxxxx';
$allowedIpAddresses = ['185.8.236.23'];

if (!isset($_GET['token']) || $_GET['token'] !== $token) {
	echo 'Bad token';
	exit(1);
}

if (!in_array($_SERVER['REMOTE_ADDR'], $allowedIpAddresses)) {
	echo 'Not allowed IP address';
	exit(1);
}

exec(__DIR__. '/deploy > '. __DIR__. '/deploy.log 2>&1', $output);

As you can see, it’s just some kind of gate which will run the deploy script in same directory. Simple right? so lets see my deploy script.

#!/bin/bash

set -x

REPOSITORY=[email protected]:david/web.git
TMP_BUILD=/tmp/davidkudera_build
PUBLIC_WWW=/var/www/webs/davidkudera

git clone ${REPOSITORY} ${TMP_BUILD}
jekyll build -s ${TMP_BUILD} -d ${PUBLIC_WWW}

chown -R www-data:www-data ${PUBLIC_WWW}

rm -Rf ${TMP_BUILD}

exit 0

Also simple and actually almost same as the example at Jekyll’s documentation.

But there is one little problem… It doesn’t work…

I don’t know ruby, so it was quite difficult to find the problem. It just said that it could not require some file or library in jekyll. So I tried to login as www-data user with sudo -u www-data -s but everything worked there.

After some time I found that the PATH environment variable was empty, but only in jekyll, not in my deploy script.

Luckily solution was simple, just export the PATH variable.

#!/bin/bash

set -x

export PATH

REPOSITORY=[email protected]:david/web.git
TMP_BUILD=/tmp/davidkudera_build
PUBLIC_WWW=/var/www/webs/davidkudera

git clone ${REPOSITORY} ${TMP_BUILD}
jekyll build -s ${TMP_BUILD} -d ${PUBLIC_WWW}

chown -R www-data:www-data ${PUBLIC_WWW}

rm -Rf ${TMP_BUILD}

exit 0