Setting up a website with Hugo and Github Actions

This article is more than two years old: versions and interfaces may have changed.

You’ll find in this article, all the element to setting up a website based on the following tools :

  • Hugo : Static website generator
  • Github : Web hosting and development source management service (using Git)
  • Github Actions : Github service to test and deploy developments
  • Gandi : Domain name provider

Objective

  • Create a Github repository to store the source code to generate the static website (documentation)
  • Create a Github repository to store the generated static website elements (pragmatias.github.io)
  • Configure Github Actions to trigger the static website generation in the repository pragmatias.github.io after a commit in the branch master from the repository documentation
  • Management of a personal domain name from Gandi to display the static website with url pragmatias.fr

Source code

All the source code of this website can be found on my dépôt github repository.

Create a Github repository

Create the repository documentation

Prerequisites : have an account at Github

1. On your repository listing page, click on New

step 1

2. Fill the name of your repository that will contain the source code to generate the website and click on Create repository

step 2

3. Github will show you the steps to initialize your repository

step 3

Create the repository pragmatias.github.io

You have to do the same step as for the creation of the repository documentation but this time with the name pragmatias.github.io.

You need to create at least one file in the repository pragmatias.github.io to configure the repository.

You will find more information about Github Pages system on the official website.

If you want to change the default configuration :

1. Go on the Settings page of the repository pragmatias.github.io

step 4

2. Go in the GitHub Pages section to configure the needed information (custom domain, enforces HTTPS, …)

step 5

Configure the actions on the Github repositories

Manage a Github personal token

First, you have to create a Github token allowing to give the right at Github Actions to do the commit action on the target repository.

1. Login on your Github account, go in settings and click on Developer settings

step 1

2. Click on Personal access tokens, then click on Generate new token

step 2

3. Fill the Token description field, then select the necessary rights (repo et gist) and click on Generate token

step 3

4. Copy the code displayed after the token generation (it will only be displayed once)

step 4

5. Go in the Settings of the repository, select the Actions option in the General > Security > Secrets menu

step 5

6. Click on the New repository secret button and fill the Name field with the value PERSONAL_TOKEN and the Value field with the token value retrieved at step 4

step 6

Create the configuration file for Github Actions

To be able to automatically deploy the website from the repository documentation to the repository pragmatias.github.io, you must create a file main.yml in the directory .github/workflows of the repository documentation

yaml
 1# This is a basic workflow to help you get started with Actions
 2
 3name: main
 4
 5# Controls when the workflow will run
 6on:
 7  # Triggers the workflow on push or pull request events but only for the master branch
 8  push:
 9    branches: [ master ]
10  pull_request:
11    branches: [ master ]
12  
13  # Allows you to run this workflow manually from the Actions tab
14  workflow_dispatch:
15  
16# A workflow run is made up of one or more jobs that can run sequentially or in parallel
17jobs:
18  # This workflow contains a single job called "build"
19  build:
20    # The type of runner that the job will run on
21    runs-on: ubuntu-latest
22
23    # Steps represent a sequence of tasks that will be executed as part of the job
24    steps:
25      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
26      - uses: actions/checkout@v3
27      # Runs a single command using the runners shell
28      - name: Setup Hugo
29        uses: peaceiris/actions-hugo@v2
30        with:
31          hugo-version: 'latest'
32
33      - name: Cleaning
34        run: rm -rf public 2> /dev/null
35
36      - name: Build
37        run: hugo --minify
38
39      - name: AfterBuild
40        run: echo "www.pragmatias.fr" > public/CNAME
41 
42      - name: Deploy
43        uses: peaceiris/actions-gh-pages@v3
44        with:
45          personal_token: ${{ secrets.PERSONAL_TOKEN }}
46          external_repository: pragmatias/pragmatias.github.io
47          publish_branch: master  # default: gh-pages
48          publish_dir: ./public

The content of this file is :

  • Use the Ubuntu distribution runs-on: ubuntu-latest
  • Download and installation of the Hugo tool for Linux name: Setup Hugo
  • delete the folder public which is the default generation folder for Hugo name: Cleaning
  • Run the Hugo tool name: Build
  • For the deployment part :
    • We define that we want to deploy the content of the directory public in the master branch of the repository pragmatias/pragmatias.github.io

Using a personal domain with Github Pages

Note : I use the domain name * pragmatias.fr * at Gandi

1. In the pragmatias.github.io repository parameters, fill then Custom domain section

github settings

Tip

to force the https, selection the Enforce HTTPS option.

2. Configure your domain to redirect users to the content of the Pages of the repository pragmatias.github.io

2.1. Go to Gandi’s website, click on Domain name, then on the desired domain name

Gandi 1

2.2. Go to Enregistrement DNS to modify the records

Gandi 1

2.3. Add the following DNS records (to redirect between the Github Pages service and your domain name)

plaintext
1@ 1800 IN A 185.199.108.153
2@ 1800 IN A 185.199.109.153
3@ 1800 IN A 185.199.110.153
4@ 1800 IN A 185.199.111.153
5www 10800 IN CNAME pragmatias.github.io
Warning

Be careful not to have any other lines starting with @ 1800 IN A or by www

It may take several hours for the modification of records to be propagated before they are taken into account.