This document ilustrates how to setup GitHub Pages using Hugo as website generator and GitHub’s Actions to automate the deployment process.
There are different alternatives to setup GitHub pages, the one used
in here is a project pages using gh-pages
branch, the advantages
are:
So basically this project’s repository has the following branches:
docs/
folder.# Docs folder
mkdir docs && cd docs
hugo new site .
git add .
git commit -m "Adds initial hugo site"
git push origin master
# gh-pages branch
git checkout --orphan gh-pages
git reset --hard
git commit --allow-empty -m "Initializing gh-pages branch"
git push origin gh-pages
ssh-keygen -t rsa -f hugo -q -N ""
This will generate the files: hugo
and hugo.pub
which will be
needed for the next steps.
Navigate to your GitHub’s repository settings and under Deploy keys
add a new one using the content of the hugo.pub
SSH key generated in
the previous step.
Make sure the Allow write access is checked, otherwise the GitHub’s Action won’t be able to push changes.
Navigate to you GitHub’s repository settings and under Secrets add a
new one using the content of the hugo
SSH private key generated in
the step 2.
Create the needed directory in the hugo branch:
mkdir -p .github/workflows
Add a new file in the path .github/workflows/gh_pages.yml
with the
following content:
on:
push:
paths:
- 'docs/**'
- '.github/workflows/hugo.yml'
name: Hugo
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Installs hugo
run: |
cd /tmp
wget https://github.com/gohugoio/hugo/releases/download/v0.57.0/hugo_0.57.0_Linux-64bit.deb
sudo dpkg -i hugo_0.57.0_Linux-64bit.deb
hugo version
- name: Build hugo site
run: |
cd docs
rm -rf public
git worktree add -b gh-pages public origin/gh-pages
hugo
- name: Configure git and deployment key
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_DEPLOY_KEY: ${{ secrets.WORKDAY_GH }}
run: |
mkdir /home/runner/.ssh
ssh-keyscan -t rsa github.com >/home/runner/.ssh/known_hosts
echo "${GITHUB_DEPLOY_KEY}" > /home/runner/.ssh/id_rsa && \
chmod 400 /home/runner/.ssh/id_rsa
git remote set-url origin git@github.com:iris-garcia/workday.git
git config --global user.name "GitHub Action"
git config --global user.email "action@github.com"
- name: Commit and push changes to gh-pages
run: |
cd docs/public
git add .
git commit -m "Publishing to gh-pages branch"
git push origin gh-pages
Replace the origin’s remote with your repository.
Finally commit and push the changes (which should trigger already the Action).
git add .github/workflows/gh_pages.yml
git commit -m "Adds GitHub's Action to build hugo site."
git push origin master
If everything went well you should already have your site updated and a
new commit to the gh-pages
branch.
You can also see the output of the Action navigating to the Actions section of your repository.