/ docker

Using Docker to build a Jekyll site with Jenkins

This probably wonʼt be particularly helpful for you. Its probably much easier for you to just run jekyll build and then deploy however you currently do. I donʼt like to do that though. I prefer to have my site built on git push. That is exactly what I set up here. Its an improvement on my old process of using Jenkins to build my Jekyll site.

Prerequisites:

  • Jenkins up and running
  • A Jekyll site in a git repo
  • A job in Jenkins to build the site
    • The build agent that will build your site needs to have Docker installed
    • It also needs to have pull access to the git repo that holds the Jekyll site
    • For it to be useful to you, you should have it so that pushing to the git repo will cause a build

Once you have all of that already set up, we start by building the docker container that will be used to generate the site.

FROM ubuntu:12.04
RUN apt-get update && \
  apt-get install -y python-software-properties && \
  apt-add-repository ppa:brightbox/ruby-ng && \
  apt-add-repository ppa:chris-lea/node.js && \
  apt-get update && \
  apt-get install -y ruby2.1 ruby rubygems ruby-switch ruby2.1-dev nodejs sudo rsync && \
  apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
RUN ruby-switch --set ruby2.1
RUN gem install bundler

RUN useradd --uid 1000 s
ADD build.sh /bin/

VOLUME ["/jekyll", "/deploy"]

CMD ["/bin/build.sh"]

One consideration is that you should set the uid of the Jenkins user in the container to the uid of the Jenkins agent. If you do not, youʼll end up with some rather painful permissions issues.

Iʼm sure that you noticed that it has a script to run specified in the Dockerfile. That is up next.

#!/bin/bash
cd /jekyll
chown -R jenkins .
sudo -u jenkins -H bundle install -j4
sudo -u jenkins -H bundle exec jekyll build || exit 1
sudo -u jenkins -H rsync -avh --delete /jekyll/_site/ /deploy/

I put both of those files into _docker so that they wonʼt be included in the site. Commit them to your Jekyll siteʼs repo. Now, we add the build script for in Jenkins

git checkout master
git pull
cd _docker
docker build -t btobolaski/jekyll-builder .
docker run --name=jekyll-builder -v "/$WORKSPACE:/jekyll" -v /var/www/ruin:/deploy btobolaski/jekyll-builder
docker rm jekyll-builder

Overall, thats pretty simple. If you didn't put the Dockerfile and script into the _docker directory, change the cd _docker line to match what you chose. You will also need to choose where the built site will end up. I chose to put it in /var/www/ruin. Feel free to use your user name instead of btobolaski in the script.

This is what I use to build Ruin.io. All I have to do is commit a new post into the git repo and push it. In a few seconds, it appears on the site. It works really great for me but, I'm not sure that any one else will find it very useful.