Using CircleCI to deploy a Hugo site on S3

This article runs you through how I deployed this blog.

Overview

Static Site Generator

A static site generator turns a set of documents into a static website (HTML pages). If you’re looking for one, this website lists them: StaticGen.

My generator of choice is Hugo.

I wrote another post on how to get started with Hugo:

AWS Infrastructure

I’m using the following components:

This other article explains how I deployed them using Terraform:

CircleCI

I use CircleCI to build and deploy the site to the S3 bucket.

I’m using this Dockerfile to build an image that contains awscli and hugo, in which my blog repository will be pulled by CircleCI:

Dockerfile

1
2
3
4
5
FROM circleci/python:buster
WORKDIR /tmp
RUN curl -LO https://github.com/gohugoio/hugo/releases/download/v0.74.3/hugo_0.74.3_Linux-64bit.deb
RUN sudo dpkg -i hugo_0.74.3_Linux-64bit.deb
RUN pip install awscli --upgrade --user

Then I added this simple script to my blog repository:

publish.sh

1
2
3
4
#!/bin/bash
rm -R public/
hugo
~/.local/bin/aws s3 sync public/ s3://my-s3-bucket --delete

A minimal CircleCI configuration like this one will execute the publish script when you commit to master:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
aliases:
  defaults: &defaults
    docker:
      - image: lussier/hugo-aws-build:v0.74.3

version: 2

jobs:
  deploy:
    <<: *defaults
    steps:
      - checkout
      - run: hugo
      - run: ./publish.sh

workflows:
  version: 2
  deploy:
    jobs:
      - deploy:
          filters:
            branches:
              only:
                - master

Note that the CircleCI project must be configured with an AWS access key and secret key pair to be authorized to make the API calls to AWS using the AWS CLI.