Automate Hugo

Continuously deploy Hugo to S3 with Bitbucket Pipelines

Spend your time doing things that matter, automate everything else.

This brief guide will take about 10-15 minutes if you're reasonably familiar with AWS and Bitbucket. At the end of the guide, your Hugo site will automatically build and deploy to S3 after you commit.


If you haven't used Bitbucket Pipelines, check it out. It's a continuous deployment service with a pretty reasonable free tier.


Thankfully, Pipelines configuration is code-based, which really speeds up our setup process. To kick things off, create a bitbucket-pipelines.yml file in the root of our repository. Here's what should be in the file:

# use a Docker image with Hugo and AWS installed
image: charlesrumley/docker-alpine-hugo:6fc41d9ffee8891c9cd03bbf6c5201c32d182e7e

pipelines:
  branches:
    # only run this script on the 'master' branch
    master:
        - step:
            name: Build & Deploy
            # mark this as a production deployment
            deployment: production
            script:
            # run the Hugo build
            - hugo
            # sync the build output of Hugo to S3
            - aws s3 sync --delete public s3://mybucketname

Next, create an AWS IAM user with Programmatic Access. Bitbucket Pipelines will use this IAM user to update your S3 bucket. Give your new user a meaningful name, perhaps bitbucket-pipelines, then attach the following IAM policy which grants the IAM user limited access to a specific bucket. Remember to update the bucket name.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "s3:DeleteObject",
                "s3:Put*"
            ],
            "Resource": [
                "arn:aws:s3:::mybucketname",
                "arn:aws:s3:::mybucketname/*"
            ]
        },
        {
            "Effect": "Allow",
            "Action": "s3:List*",
            "Resource": [
                "arn:aws:s3:::mybucketname",
                "arn:aws:s3:::mybucketname/*"
            ]
        }
    ]
}

Once you've created your IAM user, grab the credentials and add them as environment variables in Settings > Pipelines > Environment variables. In addition to the IAM credentials, specify the region your S3 bucket was created in. All told, you should have the following variables specified:

  • AWS_ACCESS_KEY_ID
  • AWS_SECRET_ACCESS_KEY
  • AWS_DEFAULT_REGION - More than likely, this'll be us-east-1

You're all set! Whenever you push a commit, Bitbucket will build your Hugo site and update your S3 bucket.

Drafts and Scheduled Posts

If you take advantage of Hugo's scheduled post publishing, you can configure Pipelines to run on a schedule.

No worries, your drafts won't be built and deployed by Hugo.