Configuring Github Pages With a Custom Domain
Today I setup my TIL website and as its a static generated site using Hugo I decided to use github pages to host it and use a custom domain.
To setup my custom domain I went through the following steps:
- Verify Domain: I followed these steps to verify. Note that this was done using my GitHub accounts settings and not the repositories settings.
- Configuring my subdomain: Next I configured my subdomain to be used as my custom domain with GitHub pages by following these steps which involved adding a CNAME record to my DNS settings with my domain registrar. Note that as I was using a subdomain adding a CNAME record was all that was needed (however if I was to use an apex domain, I would have needed to add A and AAAA records too pointing to GitHub’s ip addresses).
- Add CNAME file: You need to add a file called
CNAMEto the root of your generated static site that contains your host name only (i.e. for this site
til.softinio). As I use Hugo, I put this in the
staticdirectory as when the site is created it will be put in the root. Here is an example.
- Configuring GitHub Pages: Go to your repositories settings and select pages. Enter your domain name, save it and click check. Tick the box to enforce
- This TIL does not cover setting up GitHub actions to build and deploy your code. Here is a sample.
- You need to have your site deployed at least once so that the
gh-pagesbranch is created so that you can set the site source branch in your repositories pages settings.
- To make sure your GitHub actions is able to create the
gh-pagesbranch, go to your repositories settings, then click
Generaland under the
workflow permissionssection make sure the
read and write permissionsis selected and saved.