How to Build CI(Continous Integration) Pipeline for Hugo With Github Actions

Posted by Arun Tomar on 6 February 2021

In my previous post, I started writing my first blog post. Now i want to build a CI pipeline for my blog site.

Workflow for this pipeline

  • Create/modify posts or content.
  • Commit those changes to local git repo.
  • Push those changes to remote github repo.
  • Github actions will trigger, build/tag the image and push the image to a private container registry.

Build custom container image

Create a Dockerfile. I'll be using nginx to run my blog. Best part about hugo is, it generates static pages from posts written in markdown. I'll use the alpine version to keep the image size as low as possible.

# Dockerfile
FROM nginx:alpine

COPY ./public /usr/share/nginx/html

Next, setup github actions workflow to build and push the image.

Continuous integration using GitHub Actions

Create a file .github/workflows/build.yaml

# build.yaml
name: Docker Image CI

on:
- push
- pull_request

jobs:

  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
      with:
        submodules: true
        fetch-depth: 0

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

    - name: build hugo blog
      run: hugo --minify
    
    - name: Build the Docker image
      run: docker build . --file Dockerfile --tag <image-name>:${{ github.sha }}
    
    - name: login to DO container registry
      run: docker login -u ${{ secrets.DO_API_TOKEN }} -p ${{ secrets.DO_API_TOKEN }} <container-registry>
    
    - name: tag the image
      run: docker tag <image-name>:${{ github.sha }} <container-registry>/<registry-name>/<image-name>:${{ github.sha }}

    - name: push image to DOCR
      run: docker push <container-registry>/<registry-name>/<image-name>:${{ github.sha }}

Note:

  • GitHub actions will be executed for every push or pull request. This can be restricted to specific branches, if needed.
  • Using, custom github action to install hugo. The same was searched from github actions marketplace.
  • use appropriate container-registry, image-name, registry-name
  • DigitalOcean api token is stored in Github secrets and being referenced here as ${{ secrets.DO_API_TOKEN }}
  • I'm using DigitalOcean Container Registry(DOCR) & DigitalOcean Kubernetes Service (DOKS).