Manage a blog with Gitlab

Thought it’s actually time to write something about how this blog is built. All posts are written in Markdown language, which is then transformed by Jekyll into plain static HTML.

The Jekyll files and posts are all in a (private) Gitlab repository. Before I write a new entry I create a new branch, then commit the new post and changes to the branch. When I’m finished I create a ‘Merge request’ and merge it. The merge then triggers a pipeline job which builts the actual website using Jekyll. This is done by a .gitlab-ci.yml file in the root directory of the repository:

image: ruby:2.3

variables:
  JEKYLL_ENV: production

before_script:
- bundle install

buildSite:
  stage: build
  script:
  - ./createTagPages.sh
  - bundle exec jekyll build -d _site
  artifacts:
    paths:
    - _site
  only:
  - master
  after_script:
    - 'curl https://floki.cc/<SOME_IDENTIFIER>/deploy/$CI_JOB_ID'

It’s probably time to update this file at some point…

Anyway, what it does is: It uses the ruby docker image (Jekyll is written in Ruby). Installs Jekyll and the dependencies (which are specified in a ‘Gemfile’, see at the bottom of the page). Then calls Jekyll to build the website in the _site directory. Ignore the ./createTagPages.sh for now, this only - surprise - updates the tags which you see on the left hand side of the page. When Jekyll’s done, the _site directory is used as ‘artifact’ of the build, i. e. a zip file which contains the built website is created. When the build is finished I just ‘ping’ the webserver hosting it on a specific URL passing on the ID of the build job.

A script on the webserver runs periodically and checks for this call in the nginx logs and if it’s found, it will get the artifact from gitlab and deploy the new website:

#!/bin/bash

deployedId=`cat /var/www/version`

tmp=`cat /var/log/nginx/access.log | awk '/<SOME_IDENTIFIER>\/deploy/ {print $7}'`
jobId=${tmp##*/}
if [[ -n "$jobId" && "$jobId" != "$deployedId" ]]
then
	cd /tmp
	curl -L --header "PRIVATE-TOKEN: <GITLAB_TOKEN>" https://gitlab.com/api/v4/projects/<PROJECT_ID>/jobs/$jobId/artifacts >> artifacts.zip
	unzip artifacts.zip

	cd _site
	chown -R www-data:www-data *
	cp -r * /var/www/html/

	cd ..
	rm -rf _site artifacts.zip

	echo $jobId > /var/www/version

	dt=`date`
	echo "$dt - Deployed $jobId" >> /var/log/blog.log
fi

Finally the Gemfile for the Jekyll installation:

source "https://rubygems.org"

gem "jekyll", "~> 3.8.3"

group :jekyll_plugins do
  gem 'jekyll-paginate'
  gem 'jekyll-sitemap'
  gem 'kramdown'
  gem 'rouge'
end