I’m gonna describe how this website is hosted and deployed using GitHub and Hugo, with the help of the embedded CI/CD tools to automate the site generation and deployment.
- The first step is to create a new empty repository on GitHub, with the name following the format
mysite.github.io
. That’s how GitHub knows that this repository is going to be a static website:
- Then, visit the repository you just created. From the main menu choose Settings > Pages. In the center of your screen you will see this:
The default option is “Deploy from a branch”. Change it to “GitHub Actions”.
You can also choose a custom domain if you don’t want to use the default github.io domain:
- Now, clone the repository locally:
|
|
Now is time to populate your repository with the workflow yaml. Create a folder called .github and a subfolder called workflows:
|
|
Then, create this yaml file inside. It can have any name, let’s use hugo.yaml:
|
|
This yaml is a template provided by the Hugo team, and can be found here. What is does is configure the required permissions and settings, download and install hugo’s binaries on the runner, run hugo to generate the website and then deploy it to GitHub Pages. This saves you all the hassle of having to do it locally, and you can simply push your markdown files to the repository and everything else will be handled by the Workflow.
The next step is to copy the hugo files. There are many different ways to do that, and I prefer to use a simple method that doesn’t involve git submodules or having to install go dependencies locally. That might be necessary depending on the theme you choose, but for my case the basic is enough. The theme I use currently is the Stack, that can be found here. First, clone the theme repository:
|
|
Almost every theme will have a exampleSite folder inside, which you should copy first:
|
|
Next, copy the entire theme to the themes folder:
|
|
It might look a bit counterintuitive or confusing, but the theme must be inside the themes folder. There can be multiple different themes, and hugo will know which one to use through the hugo.yaml
file on the root folder. The problem is that each theme uses its own set of parameters within hugo’s capabilities, and that makes it a bit difficult to seamlessly change themes. I changed the theme of my website a couple of times already, and I had to start fresh every time, bringing just the content and even then having to adjust some things.
The last step is to push everything to GitHub, after configuring and testing your website locally, which I will not get into details here. But basically you have to set all the parameters and then test your pages to verify syntax, image fitment and so on. To push it:
|
|
If everything worked correctly, you can check the workflow by clicking in the Actions menu:
Then it’s just a matter of opening the URL and verifying if everything went on correctly. Most of the times when the site is correctly rendered locally but not after publishing, is due to a misconfigured hugo.yaml
file, usually at the baseurl parameter.