Introduction

Deploying a static website can often seem daunting, especially when aiming for automation and continuous integration/continuous deployment (CI/CD). Hugo, a popular static site generator, simplifies the creation process. Coupled with Amazon S3 for hosting and GitHub Actions for automation, you can set up a robust CI/CD pipeline to deploy your Hugo site seamlessly. This guide will walk you through the steps to deploy a Hugo site to Amazon S3 using GitHub Actions in 2024.

Prerequisites

Before we dive into the setup, ensure you have the following:

Setting Up Your Hugo Site

First, if you haven’t already, set up your Hugo site: Description of Image

# Install Hugo
brew install hugo

# Create a new Hugo site
hugo new site my-hugo-site

# Navigate to the site directory
cd my-hugo-site

# Add a theme (optional | e.g., Ananke)
git init
git submodule add https://github.com/theNewDynamic/gohugo-theme-ananke.git themes/ananke
echo 'theme = "ananke"' >> config.toml

# Create some content
hugo new posts/my-first-post.md

Configuring Amazon S3

Next, configure your S3 bucket to host the static site:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::your-bucket-name/*"
        }
    ]
}
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "s3:PutObject",
      "Resource": "arn:aws:s3:::your-bucket-name/*"
    }
  ]
}
- Note down the Access Key ID and Secret Access Key

Setting Up GitHub Repository

Initialize a Git repository and push your Hugo site to GitHub:

# Initialize Git
git init
git remote add origin https://github.com/yourusername/my-hugo-site.git

# Commit and push your site
git add .
git commit -m "Initial commit"
git push -u origin master

Creating GitHub Actions Workflow

Create a GitHub Actions workflow to automate the deployment process:

name: Deploy Hugo Site to S3

on:
  push:
    branches:
      - master

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout code
      uses: actions/checkout@v3

    - name: Setup Hugo
      uses: peaceiris/actions-hugo@v2
      with:
        hugo-version: 'latest'

    - name: Install dependencies
      run: |
        npm install        

    - name: Build site
      run: |
        hugo        

    - name: Deploy to S3
      uses: jakejarvis/s3-sync-action@v0.5.0
      with:
        args: --acl public-read --follow-symlinks --delete
      env:
        AWS_S3_BUCKET: ${{ secrets.AWS_S3_BUCKET }}
        AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
        AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        SOURCE_DIR: 'public'
- Push your local code to your GitHub repository.

Testing the CI/CD Pipeline

Push a commit to the main branch to trigger the workflow:

git add .
git commit -m "Trigger deployment"
git push origin master

Monitor the Actions tab in your GitHub repository to ensure the workflow runs successfully. Once completed, your Hugo site should be deployed to your S3 bucket.

Congratulations 🎉🥳

You have successfully set up a CI/CD pipeline to deploy your Hugo site to Amazon S3 using GitHub Actions.

This automated process ensures that your site is always up-to-date with the latest changes, making your workflow more efficient and reliable.

Source