6.8 KiB
| title | subtitle | date | tags | |
|---|---|---|---|---|
| How to create a Hexo blog on GitHub Pages | Host your Hexo blog for free on GitHub Pages. | 2018-11-10 |
|
In {% post_link how-to-create-a-hexo-blog 'previous post' %}, I showed you how to create a blog using Hexo and host it on GitLab Pages. Here's how to host it on GitHub Pages:
GitHub repository
- Register a free GitHub account or use your current one.
- Create a repo named username.github.io, where username is your username on GitHub.
- Clone this repo.
- Install Travis CI. It's free for open source repo.
- Go to Applications settings, configure Travis CI to have access to the repo.
- You'll be redirected to Travis page.
- On a new tab, generate a new token with repo scopes. Note down the token value.
- On the Travis page, go to your repo's setting. Under Environment Variables, put GH_TOKEN as name and paste the token onto value. Click Add to save it.
- Add
.travis.ymlfile to your repo with the following config:
language: node_js
node_js:
- node # use latest version of nodejs
cache: npm
branches:
only:
- master # build master branch only
before_install:
- npm install # install node packages and dependencies
script:
- hexo generate # generate static files
deploy:
provider: pages
skip-cleanup: true
github-token: $GH_TOKEN
keep-history: true
on:
branch: master
local-dir: public
-
You can start writing a new post straightaway without installing Hexo. You still need to change the blog's name and favicon though (how-to).
- To create a new post (through GitLab.com), create a new
<post-title>.mdfile insource/_postsfolder. - Start with the following header/front-matter:
--- title: Test page date: yyyy-mm-dd hh:mm:ss tags: categories: --- - To create a new post (through GitLab.com), create a new
-
After you create a new post, the website can be accessed on username.github.io. Check your repo settings, under the GitHub Pages, make sure the Source is
gh-pagesbranch.
Installation
- Having Hexo means you can debug locally, rather than waiting for Travis. You can even run a local server to preview your blog (see step 6 below).
- Clone your repo to your workstation.
- Install Node.js 10 (current active LTS). Other distro, see this guide or here.
# Installing npm will also install nodejs as dependency.
# Ubuntu 16.04 or newer
$ sudo snap install node --classic --channel=10
# Debian
$ curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
$ sudo apt-get install -y nodejs
# Fedora 29 or newer
$ sudo dnf install npm
# Fedora 28 or older
$ curl --silent --location https://rpm.nodesource.com/setup_10.x | sudo bash -
$ sudo yum -y install nodejs
# Arch Linux
$ sudo pacman -S npm
- Install Hexo and its dependencies (defined in
package.json). Re-launch the terminal program before continue. After installation, appendnode_modules/.binto $PATH (skip theechostep if you've already {% post_link running-locally-installed-node-packages 'done so' %}).
$ cd <project>
$ npm install --only=prod
$ echo 'PATH="$PATH:./node_modules/.bin"' >> ~/.profile
- Generate static files to check for any error. You should always do this before pushing/merging commits to the
masterbranch.
$ hexo generate
- (Optional) Start Hexo server on
http://localhost:4000to preview the blog. (more info)
$ hexo server
- Check the build status by going to your project in Travis. Due to the limitation of
hexo, the build will always pass even when there is error. Check the Jobs log, look for any error after$ hexo deploy. - If there is no error, the generated website can be accessed on username.github.io
Writing
- Create a new post (using Hexo)
$ hexo new "My New Post"
My-New-Post.mdis created to thesource/_postsfolder, with the following header/front-matter:
---
title: My New Post
date: yyyy-mm-dd hh:mm:ss
tags:
categories:
---
More info: Writing
Configuration
Naming
Change the website's author and name
_config.yml:
title:
subtitle:
description:
author:
themes/typing/_config.yml:
menu:
GitLab: <your-gitlab-project-link>
# Customize /about page
nickname:
description:
Favicon
RealFaviconGenerator provides a web-based tool to generate favicons with wide compatibility.
- Upload your favicon (at least 260x260) and configure however you want.
- Install the generated package to
themes/typing/sourcefolder. Make you replace all existing files. - Edit
themes/typing/layout/_partial/head.ejs. Change thecolorvalues ofmask-iconandmsapplication-TileColorto the values you configured on the generator. - Check for any error using
hexo generate(you should do this before you push any commit). - Commit and push.
- Check your favicon with the favicon checker.
Project page
If you prefer to have a project page on GitLab:
- Navigate to your repo on GitHub. Go to the Settings tab. Change the Repository name so your blog is available at username.github.io/repository, repository can be any name, like blog or hexo.
- Edit _config.yml, change the
root:value to the name. - Commit and push.
Useful links:
Configuration files for this blog deployment:
- .gitlab-ci.yml for GitLab Pages deployment only
- _config.yml
- package.json
- netlify.toml for Netlify deployment only
More info: