CodeBuild and Jekyll

I recently had to rebuild my PC, and as a result the environment I was using for my website updates with Jekyll was lost. Rather than going through to process of reinstalling Ruby on Windows (which felt dirty the first time!), I decided it was time to work out a better solution. Enter CodeBuild.

My code was already sitting on CodeCommit, as it was a good way to have a private Git repository. All I needed was a way to build the site from the Jekyll project.

This was simple enough- a matter of defining a new CodeBuild project with the CodeCommit repository as the Source and a Ruby 2.3.1 environment.

Now, the biggest catch I found with using this approach was the artifact output. I couldn’t find any way to output the _site directory to the root of my S3 bucket. If anyone knows a way around this, please let me know!

So the workaround for this was to use the AWS CLI (that the build environment has installed by default) to sync the directory to my S3 bucket. This was a simple enough change to my buildspec.yml as follows:

   version: 0.2

   env:
   variables:
      S3_BUCKET: "dev.sneddo.net"
      
   phases:
   install:
      commands:
         - gem install bundler
         - bundle update
   build:
      commands:
         - jekyll build
   post_build:
      commands:
         - aws s3 sync _site/ s3://$S3_BUCKET

As you can see, the install phase sets up the required gems, which is then built in the build phase, and then in the post_build phase I sync the _site directory to my S3 bucket.

I have also used an environment veriable for my bucket name so that I can use the same buildspec.yml to deploy my dev and production website by simply changing the variable in the build.

The next step will be to trigger an automatic CodeBuild when I commit changes to my repository. This appears to be more difficult for a project that has no artifact output, at least at a brief look at CodePipeline.