I’m sure many of you have your own blog. If not, check out Hexo: Deploy to Github, build a beautiful and convenient blog using Hexo and Github Pages services. Hexo can parse and render your MD documents into HTML pages, and finally push them to Github via Git to form a website.

Hexo publishes a blog process

  1. Build hexo environment (series of software installation, configuration)
  2. Hexo New Post “Article name”
  3. Writing MD documentation
  4. hexo clean
  5. hexo generate
  6. hexo deploy

The preceding step is only required for the first establishment, and subsequent steps 2 to 6 are required. But what if you need to blog on different computers? What if you reinstalled the system? Do you need a do-over? Remember that the most tedious step is the first one, and there are a lot of steps that can be broken down into, as those of you who have read the above blog post or are manually blogging using Hexo + GH know how painful it can be.

Today, I will introduce the use of Travis – CI continuous integration platform to simplify the release process.

  1. Writing MD documentation
  2. git push

Using Travis is a huge efficiency boost! Isn’t it tempting?

Travis – CI introduction

Travis CI is an open source continuous integration build project that builds code hosted on GitHub. It provides support for a variety of programming languages, including JavaScript, Java, Scala, Ruby, PHP, Haskell and Erlang.

Travis CI automatically detects our commit every time we do a Git push, etc., and automatically generates and deploys a static web page based on the configuration file.travis.

Prepare in advance

Push the hexo blog source to Github

  1. The CD goes into its own local Hexo blogs folder, the same folder you go into when you want to publish a blog.
  2. Push all of your local Hexo blog source files to Github. How to Submit Code to Github

Note: Not hexo Deploy update blog repo! Instead, you host your local blogs directly to Github.

I directly host the local hexo blog source code to xiong-it. Github. IO hexo branch, the blog site is on the default master branch. If you don’t want to branch, you can also host your local Hexo blog to a separate repository, but there is a difference when configuring Travis synchronization. Branch management is adopted in this paper.

Configuration making token

Since you need to automate updates to your blog using Travis, Travis naturally needs to read and write your Repo on Github. Github provides a token mechanism for external access to your repository.

Enter the github.com/settings/to… Create a token that Travis can use to read and write your Github. As for the permission of the token, I have directly selected all of them, but do not recommend this, because it is risky, keep the token confidential, and use it later.

Enter a description of the picture here

Travis – CI

If you don’t have a Github account, you can set up a Github account by referring to How to Submit code to Github.

Sync management in Travis into the warehouse

Go to travis-ci.org/profile and turn on the sync switch for the hexo blog repository that you just hosted, not necessarily the blog site repo. Because the author directly hosted the local hexo blog source code to xiong-it. Github. IO hexo branch, so that is open the website repo. If you are not taking branch management and instead hosting the Hexo blog source code as a standalone repO, turn on the corresponding Github repo switch. As shown in figure:

Enter a description of the picture here

Travis set

Click the red circle in the figure above to enter the Settings page to set the automatic compilation time and variables needed in the automatic compilation process.

Enter a description of the picture here

Enter a description of the picture here

The meanings of the above Settings are as follows:

  • Compile only if the.travis. Yml file exists, mandatory!
  • Compilation when the repository/branch is updated, which means compilation after push, is usually selected to facilitate automated builds.
  • GH_TOEKN and other variables are added, and the value is the token string prepared in the preparatory work

Write a.travis. Yml file

.travis. Yml is a configuration file that Travis builds for automation. Travis generates a shell automation script from the configuration file.

Enter the hexo blog source local repo

cd hexo
touch .travis.yml
vim .travis.ymlCopy the code

The following is an example of.travis. Yml:

Specify locale
language: node_js
Sudo permission is required
sudo: required
# specify the node_js version
node_js: 
  - 7.9. 0
# Specify cache module, optional. Caching speeds up compilation.
cache:
  directories:
    - node_modules

# Specify the source branch of the blog. The hexo blog source code hosted on a standalone REPO does not have to be set to this
branches:
  only:
    - hexo 

before_install:
  - npm install -g hexo-cli

# Start: Build Lifecycle
install:
  - npm install
  - npm install hexo-deployer-git --save

# Execute clear cache, generate web page operation
script:
  - hexo clean
  - hexo generate

Git commit name: Replace the real token with the _config.yml file, and finally depoy deploys
after_script:
  - git config user.name "yourName"
  - git config user.email "yourEmail"
  # replace the gh_token string in the _config.yml file in the same directory with the variable Travis just configured in the background. Note that the sed command uses double quotation marks. Single quotation marks are invalid!
  - sed -i "s/gh_token/${GH_TOKEN}/g" ./_config.yml
  - hexo deploy
# End: Build LifeCycleCopy the code

Modify the deploy node of the _config.yml file.

# change before
deploy:
  - type: git
    repo: [email protected]:xiong-it/xiong-it.github.io.git
    branch: masterCopy the code
# modified
deploy:
- type: git
  # gh_token will be replaced by sed in.travis. Yml
  repo: https://[email protected]/xiong-it/xiong-it.github.io.git
  branch: masterCopy the code

Yml sample portal

Finally, push the two YML files to the Hexo blog source branch or standalone REPO, and you will see the first build successfully in the background of Travis.

Welcome to my personal Hexo blog: Xiong -it. Github. IO

You’re done

In the future, all you need to do is write an MD file and put it in hexo/source/ _POST/folder, git add, commit, and push. Then you can use the token you just applied for for push without having to configure SSH common key on different computers.

git push https://<your_token>@github.com/xiong-it/xiong-it.github.io.git hexo:hexoCopy the code

Travis reads the.travis. Yml file in the Source branch of the Hexo blog and automatically generates and deploys the site for us.