What we want to do
The facts
- If you don't know Middleman, is a static site generator. We love using Middleman for our sites (this site is an example).
- We use and love Jenkins for CI
- We use and love Docker for app deployment and isolation.
The problem
The traditional way of deploying a Middleman site is to build it in your computer and deploy it to the server using sftp, scp, etc.
What we want to achieve
We want Middleman web sites to be build and deployed by Jenkins when the git repository is updated. We want to use Docker so that Jenkins does not know anything about gems. We will not create a Docker image with the website, we don't think this is necessary.
Let's do it
Dockerizing Middleman
First we need to dockerize our middleman site. To do so, we just need to create the following Dockerfile
FROM ruby:2.2.3
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs && rm -rf /var/lib/apt/lists/*
ENV INSTALL_PATH /app
RUN mkdir -p $INSTALL_PATH
WORKDIR $INSTALL_PATH
COPY Gemfile Gemfile.lock ./
RUN gem install bundler && bundle install --jobs 20 --retry 5
COPY . ./
Íts main job is to install all the gems.
Add the Jenkins job
Now we need to create a new Jenkins job.
Configure the job to be triggered on git master push (just Google it if you don't know how).
We will create two shells steps. The first one will build the site, the second one will deploy it.
The build script will build the Docker image, build the Middleman site, and copy it to the current location. Replace trito for you site name:
sudo rm build -rf
sudo docker build -t trito_middleman .
sudo docker rm trito_middleman
sudo docker run --name trito_middleman trito_middleman middleman build
sudo docker cp trito_middleman:/app/build "$(pwd)/build"
The deploy script will deploy the relase in a new folder and update a current sym link to the new release.
export folder=$(date +%Y-%m-%d-%H.%M.%S)
cd build
ssh deploy@trito.dev "mkdir /trito.dev/releases/$folder"
scp -r . deploy@trito.dev:/trito.dev/releases/$folder
ssh deploy@trito.dev "cd /trito.dev; rm current; ln -s releases/$folder current"
That's all folks!