GitHub Pages is a static site hosting service that takes HTML, CSS, and JavaScript files straight from a repository on GitHub, optionally runs the files through a build process, and publishes a website.
2. pre-work
2.1 create a repository
Create a repository named your_github_username.github.io, where your_github_username is your GitHub username. For example, if your GitHub username is octocat, the repository name should be octocat.github.io.
# create directorymkdir your_github_username.github.io
# cd to directorycd your_github_username.github.io
# init sitehugo new site .
# git init, make sure it's a git repositorygit init
# add a theme, here we use LoveIt theme.git submodule add https://github.com/dillonzq/LoveIt.git themes/LoveIt
# now the git is main branch which is not stable, we need to checkout to the latest stable version.cd themes/LoveIt
git checkout v0.3.0
cd ../..
# now, there should be a .gitmodules file in your directory. if not, you need to run `git init` first.# copy the exampleSite config file to the root directorycp themes/LoveIt/exampleSite/hugo.toml .
3.3 modify the config file
modify the config file hugo.toml
3.3.1 bashURL
1
baseURL="https://gooddayday.github.io"
3.3.2 themes directory
1
2
3
# themes directory# 主题目录themesDir="./themes"
3.3.3 website title
1
2
3
# website title# 网站标题title="GoodyHao's Blog"
3.3.4 website images
1
2
3
# website images for Open Graph and Twitter Cards# 网站图片, 用于 Open Graph 和 Twitter Cardsimages=["/logo.jpg"]
3.3.5 website icon
put icon file in the static directory
3.3.6 gitRepo
modify the gitRepo to your public git repo url
1
2
3
# public git repo url only then enableGitInfo is true# 公共 git 仓库路径,仅在 enableGitInfo 设为 true 时有效gitRepo="https://github.com/GOODDAYDAY/GOODDAYDAY.github.io"
4. github deploy
4.1 create a workflow file
create a file .github/workflows/deploy.yaml, and add the following content:
github repository settings -> pages -> source -> select gh-pages branch and / (root) folder -> save
if gh-pages branch not exist, need to push code to github first
need to set token
generate new token with repo and workflow permissions
add token to github secrets with name TOKEN_GITHUB
4.2 push code to github
1
2
3
4
5
6
# add all filesgit add .
# commitgit commit -m "first commit"# push to githubgit push -u origin master
4.3 check the workflow
check the workflow in github actions
5. access the blog site
access the blog site with https://your_github_username.github.io, for example, https://gooddayday.github.io
6. others
6.1 add new post
1
2
3
4
5
6
7
8
9
10
# create a new posthugo new posts/first-post.md
# edit the postvim content/posts/first-post.md
# after edit, need to set the post as published# set draft = false# then commit and push to githubgit add .
git commit -m "add first post"git push
if you want to add images to the post, need to put the images in the static directory, for example, static/images/first-post-image.png, then you can access the image with /images/first-post-image.png in the post.
6.2 gitignore
create a .gitignore file in the root directory, and add the following content:
1
public/*
we don’t need to push the public directory to github, because it will be generated by hugo in the workflow.
6.3 tag & category generate
tag and category will be generated automatically by hugo, no need to create them manually.
But if no index.html shown below, you need to add templates.
just copy the themes/LoveIt/layouts/taxonomy/list.html to the different path and rename it to layouts/taxonomy/tag.html and layouts/taxonomy/category.html
and then, run hugo server to check if the result has index.html like the picture below:
6.4 setup comment system with giscus
By default, the LoveIt theme uses Valine comment system, but we recommend using Giscus which is based on GitHub Discussions. Giscus is free, stable, and stores comment data in your own GitHub repository.
6.4.1 disable other comment systems
First, make sure other comment systems are disabled in hugo.toml:
Fill in the repository field: your-username/your-username.github.io
Click The giscus app is installed, otherwise visitors will not be able to comment and react. and install giscus to your repository.
6.4.4 update hugo.toml
Add the giscus configuration to your hugo.toml:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[params.page.comment.giscus]enable=truerepo="your-username/your-username.github.io"repoId="your-repo-id-from-giscus"category="General"# or your chosen categorycategoryId="your-category-id-from-giscus"lang=""# empty for auto-detectionmapping="pathname"reactionsEnabled="1"emitMetadata="0"inputPosition="bottom"lazyLoading=falselightTheme="light"darkTheme="dark"
data like repoId and categoryId can be found in the giscus configuration you generated earlier.