🚀Hugo: Deploy with GitHub Actions
Apr 14, 2021 23:00 · 507 words · 3 minute read
This post discusses how Hugo, a blazingly fast static site generator written in Golang, can be deployed with GitHub Actions to GitHub pages.
This blog which you are currently reading, is built using Hugo and is hosted on the Github pages.
Repository structure
I’ve got a private repository just for the contents & another separate repository to hold the generated files (HTML/CSS).
Initially, I relied on the Hugo CLI to generate & serve the static page contents each time I create/edit posts. However, it was a pain in the neck if you wanted to make/edit a post from a different machine where you cannot have Hugo CLI installed.
Moving to forestry
Later I moved into forestry, a git-backed CMS for static generated websites. Publishing workflow, i.e. to write, update & publish any blog post, was a lot easier & always felt like a piece of cake in forestry.
No deployment support
Last year they removed the support to deploy/publish the posts for Hugo generated websites.
When it comes to automate your deployments and host your static website on a CDN, it’s the responsibility of a continuous deployment and hosting service. https://forestry.io/docs/hosting/.
So the choice left with me was to go with GitHub Actions.
Get into Action with GitHub Action
GitHub Actions make it easy to automate all your software workflows. I must say it is so easy to get your head around.
I just wanted to share the YML file here, which I’ve used for the continuous deployment to help somebody in my situation.
# Workflow to help you get started with Hugo deployment
name: Deploy
# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on:
push:
branches: master
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "deploy"
deploy:
# The type of runner that the job will run on
runs-on: ubuntu-latest
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
# Github Actions fo Hugo
- name: Setup Hugo
uses: peaceiris/actions-hugo@v2
with:
hugo-version: "0.81.0"
# Creates ./public directory
- name: Build
run: hugo --minify
# Upload/Commit the contents from publish_dir -> external_repository
- name: Deploy
uses: peaceiris/actions-gh-pages@v3
with:
external_repository: aneesahammed/aneesahammed.github.io
publish_branch: master
personal_token: ${{secrets.GH_TOKEN}}
user_name: aneesahammed
user_email: aneesahammed@mail.com
publish_dir: ./public
Please check out the below link for more info on Hugo setup GitHub Actions: https://github.com/marketplace/actions/hugo-setup.
Also, be noted that the below fields are mandatory since you are deploying to an external repository.
- external_repository: external repository where you wanted to deploy.
- publish_branch: the branch you wanted to publish to.
- personal_token: Go & generate a personal token from here and add it to the secrets tab of your repository
Now, voila, every update/commit on the blog (master branch) triggers the deployment. Please get in touch if you have any more queries.