Previously in Setup Blog using Hexo on GitHub, I talked about how to setup Hexo on GitHub. Today, I’m going to do the same but on GitLab. One of the main reasons for GitLab is because that Continuous Integration (CI) is built-in to GitLab. Therefore, the only thing I need to do for deployment is just push the code to GitLab, and then everything else will be magically done by GitLab and CI. The rest of the post will show how I setup Hexo and CI on GitLab.
References:
StaticGen: https://www.staticgen.com
Hexo: https://hexo.io
Hexo Docs: https://hexo.io/docs/
NexT theme: https://github.com/theme-next/hexo-theme-next
Introduction | Hexo - Static Site Generator | Tutorial
GitLab: https://about.gitlab.com/
GitLab Pages: https://about.gitlab.com/product/pages/
部署 HEXO 到 GitLab Page
HEXO 搭配主題 next 基礎配置教學
Requirements
My Environment
- MacBook Pro (13”, 2017) - macOS High Sierra 10.13.6
- Git - 2.17.1 (Apple Git-112)
- Node Version Manager (NVM) - 0.34.0
- Node.js - v8.16.0
- Hexo - 3.9.0
- hexo-cli - 2.0.0
- NexT theme - v7.2.0
Setup Node.js
Install Node Version Manager (NVM)
The way I installed Node.js on MacBook is to use Node Version Manager (NVM).1
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
To verify that nvm has been installed, do:
1
command -v nvm
Install Node.js and NPM
npm
(originally short for Node Package Manager) is a package manager for the JavaScript programming language. It is the default package manager for the JavaScript runtime environment Node.js. It consists of a command line client, also called npm, and an online database of public and paid-for private packages, called the npm registry. The registry is accessed via the client, and the available packages can be browsed and searched via the npm website. The package manager and the registry are managed by npm, Inc.
In order to install
hexo-cli
, we will neednpm
.1
2nvm install --lts=carbon
nvm install-latest-npm
References:
- Node Version Manager(NVM):
https://github.com/creationix/nvm/blob/master/README.md#installation
GitLab Page
It should be fairly easy to setup your GitLab Pages by following their instructions. They also have an example for setting up Hexo on GitLab.
Hexo
Setup Hexo
Install Hexo
1 | npm install -g hexo-cli |
Setup Hexo and Your Site
1 | cd <YOUR_WORKSPACE> |
Setup Git
After Hexo site is initialized locally, let’s setup the git for the Hexo site. In my case, the URL of Hexo repo is https://gitlab.com/jocodoma/site-hexo.
1 | cd <YOUR_HEXO_SITE> |
Setup NexT Theme
We will checkout the latest version (v7.2.0 as of 2019/06/05) of NexT theme as a git submodule. The reason we do this way as a git submodule is because that by doing this we don’t have to do version control of the NexT source code and also it will be easier for us to update the version of NexT later. In case if there’s any change we want to make in NexT source, we can easily do so by creating git patch files and apply the custom changes to the NexT source. Therefore, we don’t have to check in the entire source of NexT to the repository, but just git submodule with the patch files.
1 | cd <YOUR_HEXO_SITE> |
To update NexT theme later, please refer to Update NexT Theme.
After installed NexT, you may add NexT options in site’s _config.yml.
References:
- NexT installation:
https://github.com/theme-next/hexo-theme-next/blob/master/docs/INSTALLATION.md - NexT Data Files:
https://github.com/theme-next/hexo-theme-next/blob/master/docs/DATA-FILES.md
Changes for Google Adsense
In order to have Google Adsense working properly, some of the files need to be modified in NexT source code. I created a git patch file for the changes made for Google Adsense as here. This will be useful later when setting up .gitlab-ci.yml
.
To create a git patch, stage all the files (do git add
), and then run git diff --cached
.
1 | git diff --cached > 0001-google-adsense-files-NexT-v7.2.x.patch |
To apply a git patach, do
1 | git apply 0001-google-adsense-files-NexT-v7.2.x.patch |
Reference: https://nithinbekal.com/posts/git-patch/
Modify Config File
You can find my full Hexo config settings with NexT options here. Below is just an example:
1 | title: Jocodoma |
Reference:
Hexo doc - Configuration: https://hexo.io/docs/configuration.html
Hexo Server
You can start a Hexo server on local machine to review your changes before publishing to the server.
1 | cd <YOUR_HEXO_SITE> |
By default, your website will run at http://localhost:4000
. The -p
option can be used to set a different port.
1 | hexo server -p 5432 |
Setup CI for Hexo Deployment
Unlike Hexo on GitHub, we do not need to install hexo-deployer-git mentioned in Setup Hexo Deployment. Instead, the Hexo deployment will be done by CI on GitLab.
Here’s my .gitlab-ci.yml
. You can also find it here.
1 | image: node:8.16.0 |
Below is what happens in the apply_ads_patch.sh
script.
1 |
|
The final step is to make sure you commit the code and push back to remote repo. After few minutes, you should be able to see your Hexo blog on your GitLab page, which in my case is https://tech.jocodoma.com. Yay!!
Plugins
Here are some plugins that I use for Hexo blog.
Symbols count time: https://github.com/theme-next/hexo-symbols-count-time
1
2cd <YOUR_HEXO_SITE>
npm install hexo-symbols-count-time --saveGenerate sitemap: https://github.com/hexojs/hexo-generator-sitemap
1
2cd <YOUR_HEXO_SITE>
npm install hexo-generator-sitemap --saveAdd following lines to
_config.yml
:1
2
3
4sitemap:
path: sitemap.xml
template: ./sitemap_template.xml
rel: falseGenerate search data for Hexo: https://github.com/theme-next/hexo-generator-searchdb
1
2cd <YOUR_HEXO_SITE>
npm install hexo-generator-searchdb --saveAdd following lines to Hexo
_config.yml
:1
2
3
4
5search:
path: search.xml
field: post
format: html
limit: 10000Add following lines to NexT
_config.yml
:1
2
3
4
5
6local_search:
enable: true
trigger: auto
top_n_per_article: 1
unescape: false
preload: falseReference: https://theme-next.org/docs/third-party-services/search-services
A JavaScript library for zooming images like Medium (Supported by NexT theme)
https://github.com/francoischalifour/medium-zoom1
2cd <YOUR_HEXO_SITE>
npm install medium-zoom --saveAdd following lines to NexT
_config.yml
:1
mediumzoom: true
Vanilla JavaScript plugin for lazyloading images (Supported by NexT theme)
https://github.com/ApoorvSaxena/lozad.js1
2cd <YOUR_HEXO_SITE>
npm install lozad --saveAdd following lines to NexT
_config.yml
:1
lazyload: true
Update
Update NVM
In case if you need to upgrade your NVM, you can do:
1 | pushd "$NVM_DIR" |
References:
- Node Version Manager(NVM):
https://github.com/creationix/nvm/blob/master/README.md#installation
Update node
1 | nvm ls # check the installed version |
1 | # Some other options |
Update Hexo Dependencies
1 | cd <YOUR_HEXO_SITE> |
Update NexT Theme
- List all available updates:
1
2
3cd <YOUR_HEXO_SITE>/themes/next/
git fetch
git log --no-walk --pretty="%h %d %s" --tags --abbrev-commit --decorate=full - Select the version you want to update:Here’s my script to check and update NexT theme.
1
git checkout tags/v6.x.x