In this post I’m gonna explain how I created a very simple GitHub Action to deploy Hugo static websites to AWS S3.

In case you missed my post where I explained how I prepare all my static websites you can find it here.

What is a GitHub Action?

A GitHub Action it’s like a precooked CI job that we can use in our GitHub repositories to do automatic things when some actions happens in our repository.

The cool thing is that you can use actions that other people created. You can find them all in GitHub Marketplace.

How to create a GitHub Action step by step

1. Create a folder in your machine

First thing you have to do is to create a folder where we’ll put all the needed code and where we’ll init a nit git repository.

mkdir deploy-hugo-to-s3-action

2. Create a action.yml file

We need to setup a YAML file where we’ll put all the action configuration.

See GitHub Docs to know more about the syntax.

In my case this is how the file ended up:

name: 'Deploy Hugo to S3'
description: 'Build and deploy Hugo static websites to AWS S3'
author: 'AlbertMorenoDEV'
branding:
  icon: package
  color: yellow
inputs:
  hugo-version:
    description: 'Choose a valid Hugo version'
    required: true
runs:
  using: "composite"
  steps:
    - id: install-hugo
      run: |
        HUGO_DOWNLOAD=hugo_extended_${{ inputs.hugo-version }}_Linux-64bit.tar.gz
        wget https://github.com/gohugoio/hugo/releases/download/v${{ inputs.hugo-version }}/${HUGO_DOWNLOAD}
        tar xvzf ${HUGO_DOWNLOAD} hugo
        mv hugo $HOME/hugo
      shell: bash
    
    - id: download-themes
      run: |
        git submodule init
        git submodule update
      shell: bash
    
    - id: hugo-build
      run: $HOME/hugo -v
      shell: bash
    
    - id: deploy-to-s3
      run: $HOME/hugo -v deploy --invalidateCDN --maxDeletes -1
      env:
        AWS_ACCESS_KEY_ID: ${{ inputs.aws-access-key-id }}
        AWS_SECRET_ACCESS_KEY: ${{ inputs.aws-secret-access-key }}
      shell: bash

Note that in runs section I just added a bunch of bash command line executions. In my case it has been that easy, for more complex ones you might need to spin up a Docker container. We’ll explain how to do it in another post.

3. Create a README.md file

Then we need a file where we’ll explain how to use our brand new action.

I just created the README.md file like the following:

# Deploy Hugo To S3 Action

This GitHub action makes it easy to build and deploy any Hugo static website to AWS S3 with just one step.

## Example

\`\`\`
name: Hugo Build and Deploy to S3

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  build:
    name: Build and Deploy
    runs-on: ubuntu-latest
    steps:
      - name: Check out master
        uses: actions/checkout@master
      
      - name: Build and deploy
        uses: AlbertMorenoDEV/deploy-hugo-to-s3-action@v0.0.3
        with:
          hugo-version: 0.85.0
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
\`\`\`

4. Create a new public GitHub repository

Our code is ready so now we just need to put all these files in a public GitHub repository.

Create a new repository here.

Then setup this repository in our folder where we have all the code.

First initialize a git repository there:

git init

Add all the files:

git add .

Create the first commit:

git commit -m "First commit"

Change to branch main to match with new GitHub repository:

git branch -M main

Add the origin of the GitHub repository:

git remote add origin git@github.com:AlbertMorenoDEV/deploy-hugo-to-s3-action.git

And finally push all the changes to GitHub:

git push -u origin main

5. Create a new release and publish to GitHub Marketplace

You’ll find Releases section in the right bar in GitHub. Go there and you’ll see a button Draft a new release. Click it.

Check the first checkbox Publish this Action to the GitHub Marketplace.

Then a checklist will appear to alert you in case your action.yml file has something wrong.

Choose a primary category at least. In my case I chose Deployment as primary and Container CI as secondary.

Enter a valid tag version following semantic versioning. I used v0.0.1 since was a very initial release.

Also, add a release title and optionally you can add a description too.

Click to Publish release button and if everything it’s ok you should have your new GitHub Action published.

My GitHub Action

Find my GitHub Action here. Feel free to fork the project.

Resources