Let’s say you’re cheap like me and don’t want to pay for hosting some html, css, and js. There are a few choices for free hosting, the best of which are using Github’s or Gitlab’s pages features. This site is hosted through Gitlab which has the added benefit of auto-updating the blog every time I push without any further setup.
Here’s how you do it:
- Setup your repo with Gitlab.
- When you’re setting it up make the name / path of the project equal to:
*note: It does not matter if the repo is public or private.
Create a runner to build your blog every time you push. It will download the hugo docker container image and run the script
hugoin order to build your static files. If you want to build the files yourself every time before you push then just remove the image and script portion of the below.
.gitlab-ci.ymlfile to be added to your repo:
image: publysher/hugo pages: script: - hugo artifacts: paths: - public
Push to Gitlab. You can then check the ‘build’ tab on the left to see your current build process.
If Gitlab successfully built the blog using the
hugocommand then you’re all set!
Go to username.gitlab.io and see your site!
*Notes on the URL:
You can create `username.gitlab.io/projectname’ if you’d rather.
Adding Your Custom Domain
In order to add your custom domain, Gitlab is going to instruct you to use a CNAME DNS record. While this may work for the root of the domain on some hosting providers, it does not work in my case (Google domains). If you find that this does not work in your setup either, simply set the Gitlab domain as a subdomain and redirect your root domain to your subdomain.
- Go to the settings for the project.
- Go to the ‘pages’ tab on the left.
- Add a new domain (blog.ilangitter.com for me).
*note: You can even add a ssl certificate here if you’d like.
- Save and look at the details for your newly added domain. It should say something like:
blog.ilangitter.com CNAME gitter.gitlab.io.
- Go to your domain host’s preferences and add the CNAME record mentioned above
- If needed redirect your domain to your subdomain
This process above (with minor tweaks) will let you host any site created by a static site generator, not just Hugo. This has made my life so simple. After I’m done writing this all I have to do is push it to Gitlab and the rest is synced for me. If you want you could set up some sanity tests and add that to the script section of the yml file to make sure all is well.