Overview
- About how to use Hexo build up my blog and deploy on GitHub Pages.
- How to use Travis CI to set up automatically deployment.
Why use Hexo
- run on Node.js
- fast build up
- many plugins and big community
Start
Prerequisites
- npm & node.js
- git
Installing
Install Hexo
1 | npm install -g hexo-cli |
Add hexo folder, cd into it and install packages through npm
1 | hexo init <folder> |
Notes: the default theme ‘Landscape’ has an unknown variable at README.md line 77. It would lead to build failed. So need to delete the README.md file of the ‘Landscape’ theme. Or comment out that line. (2019.10.04)
Setting
Edit the _config.yml
file
1 | ... |
Notes: It is essensial to set
root
to/<repo-name>/
otherwise it could led to CSS file generating failed.
More details: Hexo document Configuration
Deployment
In official document tutorial, it recommends to use Travis CI
to deploy Github Pages
. It is free for open source repository, meaning your repository’s master branch has to be public.
Two options of GitHub Pages Url Choosing
Create a repo named
username.github.io
, where username is your username on GitHub.Create a repo named anything. for example:
myblog
.
The difference between these two options is that a user has only one site of url https://<username>.github.io
, while you can have mutiple sites with the url https://<username>.github.io/<repo-name>/
.
Push hexo files to Github
- Push the files of your Hexo folder to the repository.
Notes: The public/ folder is not (and should not be) uploaded by default, make sure the
.gitignore
file containspublic/
line.
Add Travis CI to GitHub account
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.yml
file to your repo (alongside_config.yml
&package.json
) with the following content:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18sudo: false
language: node_js
node_js:
- 10 # use nodejs v10 LTS
cache: npm
branches:
only:
- master # build master branch only
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
Deploy
To create a new post, youn can add
<title>.md
file in thesource/_posts
folder by running following command:hexo new [layout] <title>
Notes:
post
is the default layout. To change the default layout by editing the default_layout setting in_config.yml
.You can monitor the building process at Travis CI dashboard. And add the image of build/passing by click it to copy the markdown script and paste into your
README.md
file in hexo folder.Once Travis CI finish the deployment, the generated pages can be found in the
gh-pages
branch of your repositoryIn your GitHub repo’s setting, navigate to “GitHub Pages” section and make sure that the Source is pointed to
gh-pages
branch.Now you can check the webpage at the url you set.