So, today I spent a lot of the day getting my Jenkins server to build/deploy this website automatically. Right now, this site is statically built using Jekyll and I've been manually copying it up to the S3 bucket where it's hosted. I didn't like that process very much, and I wanted to automate it a bit more.

It's been a git repo since the beginning, but I've added a Dockerfile so the Jekyll dependencies are always pulled and the generated site is consistent.

The Dockerfile looks like this:

FROM ruby:2.3.0-alpine

RUN apk add --update \
  build-base \
  libxml2-dev \
  libxslt-dev \
  && rm -rf /var/cache/apk/*

EXPOSE 4000

COPY Gemfile* /src/

WORKDIR /src
RUN bundle install

COPY . /src

RUN bundle exec jekyll build --destination ./dist

CMD bundle exec jekyll serve -H 0.0.0.0 --no-watch

Using docker allows me to have a consistent build, regardless of platform, and lets me also test it pretty easily without having to pull all the Ruby prereqs into my environment. Since I don't do any Ruby development, I'd rather not clutter my system with it just for this.

Jenkins builds the docker image, and then I have a deployment script that copies the /src/dist folder into the work folder, which is then synced using aws s3 sync.

Here's a groovy snippet for that:

	def dockerConfigOption = "--config .docker"
	sh "docker ${dockerConfigOption} pull ${dockerImage}"
	def container=readSh("docker create ${dockerImage}").trim()
	sh "docker cp ${container}:/src ."
	sh "docker rm -f ${container}"

	withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', credentialsId: 'aws-deploy']]) {
		sh "aws s3 sync ./src/dist s3://${bucket}"
	}

Earlier in the script, I set up the docker config in .docker which grants me access to my private docker repo. readSh() just returns the stdout from the shell command, so I have the ID of the container that was created.

As long as the /src/dist folder will contain all the static files, this will always work, and it's a simple way to have a static site built and copied to S3 using Jenkins.