How to create a blog with Jekyll 4 and host it for free on GitHub
Today I want to note down how I created this very blog using GitHub Pages and Jekyll 4 – a static site generator.
Why? Because GitHub uses Jekyll 3.9.0, whereas the current version of Jekyll is 4.2.1, see all Jekyll releases.
To do this I had to go through various sources and here is the complete guide.
Contents
- Prerequisites
- Setting up GitHub repository
- Setting up local environment
- Setting up deployment using GitHub Actions
- (Optional) Setting up custom domain
- Example
Prerequisites
- GitHub account
- Installed Git locally
- Basic knowledge of HTML and CSS – later for styling your blog
- (Optional) Money for the custom domain
Setting up GitHub repository
First, create a public GitHub repository. The name of the repository should be username.github.io
.
For example, if your username is octocat
, repository should be named octocat.github.io
.
Clone your repo locally
Setting up local environment
- Install Ruby. MacOS has Ruby installed by default but let’s get a new one with Homebrew.
Find out where brew ruby and gems are
Here’s what I have here:
Add both of them to your PATH
Relaunch terminal and verify you use the brew ruby now
Now install jekyll and bundler
Add .gem
to your PATH
Initiate a new Jekyll site (--force
option is here in case you already have some files in your directory that you don’t need and wish to overwrite)
Build and run the site locally
Open http://127.0.0.1:4000 in your browser – you should see the website there.
Setting up deployment using GitHub Actions
Jekyll has almost working instructions on how to do that.
Here’s what to do to make it work correctly.
- Create in your project file
.github/workflows/github-pages.yml
- Copy the following contents into that file
NOTE: target_branch
is missing from Jekyll docs and token name can’t start with GITHUB
,
so I choose the name ACTIONS
.
Setup the token in GitHub
- Go to Personal access tokens on GitHub.
- Generate a new token:
- give it
public_repo
permissions - setup expiration date
- save the generated value locally
- give it
- Go to your repository’s settings on GitHub and then to the
Secrets
tab. - Create a token with the name
ACTIONS
(name should be the same as in the yaml file) and provide it the saved value from the generated token.
Add all generated files and commit them to GitHub
Check what is tracked by git
If there’s anything you don’t want to add to GitHub, add those files to .gitignore
.
Add, commit and push your files
Verify your changes
Go to the Actions
tab of the repository and see that the workflow is completed successfully.
Once it is completed, navigate to https://username.github.io,
where username
is your GitHub username. Success, the blog is ready and is published for everyone to see!
(Optional) Setting up custom domain
If you want your blog to run at some address other than username.github.io
, do the following:
- Register a domain you like
- Follow official GitHub instructions
- Add
CNAME
file to the root of your project and add there your domain (according to the documentation, it should be created automatically, but it did not happen ¯\_(ツ)_/¯)
Example
If you need a specific example, you can visit the source code of this blog.