This page looks best with JavaScript enabled

How to Migrate a Blog from Ghost to Hexo

 ·  ☕ 3 min read

1. Introduction to Hexo

Hexo is a concise, efficient, open-source static blog generator built with Node.js. Hexo can render Markdown into static HTML files extremely quickly. Like Jekyll, Ghost, and Octopress, it can be used to create a blog.

Hexo has the following advantages:

  • It uses Node.js, so setting up the environment is simple. Setting up a Jekyll environment is a real pain.
  • It is easy to host on GitHub Pages. Since Hexo generates HTML, deployment is very convenient.
  • It supports Markdown.

2. Installing Hexo

After installing Node.js and NPM, it is recommended to switch to a domestic NPM mirror to speed up dependency installation.

1
2
npm install -g cnpm --registry=https://registry.npm.taobao.org
cnpm install install

Install Hexo.

1
cnpm install -g hexo

Initialize Hexo.

1
hexo init myblog

This generates files such as scaffolds, drafts, public, db.json, _config.yml, package.json, and theme.

Start the service locally; the default port is 4000.

1
2
3
4
cd myblog
hexo s
# Set the service port to 80
hexo s -P 80

3. Migrating Posts from Ghost

Since the blog was previously built with Ghost, you need to export the blog data in JSON format from the Ghost admin panel. Then, with a Hexo plugin, convert the JSON data into Markdown files.

Install hexo-migrator-ghost.

1
npm install -g hexo-migrator-ghost

Import the data and convert it to Markdown files.

1
hexo migrate ghost <source.json>

Once it finishes, a Markdown file for each post is generated automatically under the source directory.

4. Publishing to GitHub Pages

Step one: create a new project named your_name.github.io on GitHub.

Step two: configure the repo in _config.yml.

1
2
3
4
5
deploy:
  type: git
  repo:
    github: git@github.com:your_name/your_name.github.io.git
  branch: master

Step three: generate the HTML.

1
hexo generate

Step four: deploy to GitHub Pages.

1
hexo deploy

Enter your account and password when prompted to complete the deployment. Visit the blog at http://your_name.github.io.

5. Automated Publishing with CI

Since GitHub Pages is used only as the publishing address for the blog, you still need another repository to host the Hexo project and the Markdown source files.

Step one: on GitHub’s https://github.com/settings/keys page, generate a pair of SSH keys, then click [Add SSH key] and paste in the public key.

Step two: on GitLab’s [settings/ci_cd] page, under [Secret variables], add an environment variable named DEPLOY_KEY whose value is the private key.

Step three: configure the CI pipeline. Use GitLab to host the Hexo source repository and publish automatically to GitHub Pages through CI.

In the root directory, create a new file named .gitlab-ci.yml with the following content:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
image: node:4.2.2
pages:
  cache:
    paths:
    - node_modules/
  before_script:
  - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
  - eval $(ssh-agent -s)
  - ssh-add <(echo "$DEPLOY_KEY")
  - mkdir -p ~/.ssh
  - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
  script:
  - npm install hexo-cli -g
  - npm install
  - hexo deploy
  artifacts:
    paths:
    - public
  only:
  - master

Without a CI configuration, you have to generate the HTML files locally and then deploy them to GitHub Pages. With CI configured, all you need to do now is commit the Markdown files to GitLab, and GitLab CI completes the deployment automatically.


WeChat Official Account
WRITTEN BY
WeChat Official Account