Roman Böhm


Meta Militia, or: Using a Custom Domain with GitHub Pages

2021-06-16

Last edit: 2025-04-08

dns blogmeta

How to: Using a custom domain (apex domain and www subdomain) for a Hugo-generated page hosted on GitHub Pages.

Introduction

What better first post than a meta post detailing the experience of getting my page running under my own domain in the first place. On the true developer-scale probably right below writing your own custom static site generator (in Rust or Kotlin, of course). If you’re too cheap for the all-inclusive Wordpress experience, too vain to just use the <username>.github.io domain, or too lazy to read the actual (very decent docs), this one’s for you!

Body

Hint: The instructions assume you already have your domain registered and a Hugo page lying around on your disk somewhere. In my examples, I will describe the setup with the domains (www.) romanboehm.com and rmnbhm.github.io. If you want to follow along, exchange those with your own domain and your desired additional .github.io domain.

Those are the steps needed to make it work:

  1. In your DNS provider’s (or registrar’s) settings add the following A records, with the record’s name being the apex domain. The IP addresses belong to GitHub Pages (cf. the GitHub Pages docs).
    romanboehm.com.	1	IN	A	185.199.111.153
    romanboehm.com.	1	IN	A	185.199.110.153
    romanboehm.com.	1	IN	A	185.199.109.153
    romanboehm.com.	1	IN	A	185.199.108.153
    
  2. Then, add the following CNAME record.
    www.romanboehm.com.	1	IN	CNAME	rmnbhm.github.io.
    
    This will make your page available under the www subdomain, too.
  3. Create a repository on GitHub called rmnbhm.github.io. Now, sure you can actually name it however you want, I just follow GitHub Pages’ convention here of using <username>.github.io. Added benefit: Your page will be available under that domain as well!
  4. Navigate to your local git repository for the page’s source code and add the remote repository as origin:
    git remote add origin [email protected]:rmnbhm/rmnbhm.github.io.git
    
  5. To allow for automatic builds and deployments create a folder in your repository’s root called .github which itself contains a folder called workflows. The latter then contains a file called gh-pages.yml providing all the GitHub Actions steps to build and deploy the page:
    # .github/workflows/gh-pages.yml
    name: github pages
    
    on:
      push:
        branches:
          - main  # Set a branch to deploy
      pull_request:
    
    jobs:
      deploy:
        runs-on: ubuntu-20.04
        steps:
          - uses: actions/checkout@v2
            with:
              submodules: true  # Fetch Hugo themes (true OR recursive)
              fetch-depth: 0    # Fetch all history for .GitInfo and .Lastmod
    
          - name: Setup Hugo
            uses: peaceiris/actions-hugo@v2
            with:
              hugo-version: 'latest'
              # extended: true
    
          - name: Build
            run: hugo --minify
    
          - name: Deploy
            uses: peaceiris/actions-gh-pages@v3
            if: github.ref == 'refs/heads/main'
            with:
              github_token: ${{ secrets.GITHUB_TOKEN }}
              publish_dir: ./public
    
  6. Add a file called CNAME to your static folder. The file contains just the apex domain, like so:
    romanboehm.com
    
    This file will get included in your published files upon building the page, as described here.
  7. Push your page’s source to GitHub:
    git push origin main
    
  8. Navigate to your repository’s Pages settings on GitHub. If the initial actions run was succesful you should be able to pick gh-pages as the Source branch. Use / (root) as the desired folder from which GitHub is supposed to source the page’s content after a push.
  9. Make sure the domain provided through the CNAME file appears under the Custom domain section. In my case that’s romanboehm.com

Conclusion

That’s the whole thing. You should verify your site is available under the desired apex, www, and github.io (sub-) domains. Don’t forget to update your config.toml to make use of the new base URL if you have any settings referencing the URL there.


© Roman Böhm 2021 - 2025 – GithubBlueskyMastodonLinkedIn