使用Travis CI自动生成Hexo Blog

Posted by Simon Dong on 2019-07-24

0x01 Travis CI服务申请

参考:

Hexo遇上Travis-CI:可能是最通俗易懂的自动发布博客图文教程

使用Travis CI自动部署Hexo博客

0x02 我的定制

由于我将hexo的source/_posts作为Git子模块使用,因此各分支的作用如下:

  • master: 博客生成的内容
  • develop: 博客源文件(md和image)
  • hexo: Hexo配置

因此,travis ci的配置:

develop:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
language: node_js
node_js: stable
branches:
only:
- develop
cache:
apt: true
yarn: true
directories:
- node_modules
before_install:
- git config --global user.name "Simon Dong"
- git config --global user.email "snovian@gmail.com"
- git clone --depth=5 --branch=hexo https://$REPO_TOKEN@github.com/snovian/snovian.github.io.git _deployer
- mkdir -p _publish
- cd _deployer
- git submodule update --init --remote --recursive
- mv ../node_modules ./
- npm install -g hexo-cli
- rm -rf source/images
- mv source/_posts/images source/
- rm -rf source/plantuml
- mv source/_posts/plantuml source/
- sed -i "s/^build:\ .*$/build:\ `echo \$TRAVIS_BUILD_NUMBER`/" _config.yml
install:
- npm install -g hexo-cli
- npm install
script:
- hexo clean
- hexo generate
after_success:
- rm -rf ../node_modules
- mv ./node_modules ../
- cd ../_publish
- rm -rf *
- mv ../_deployer/public/* ./
- git init
- git add -v --all .
- git commit -m "Travis CI Auto Builder@$TRAVIS_BUILD_NUMBER"
- git push -v --force "https://$REPO_TOKEN@github.com/snovian/snovian.github.io.git" master:master

hexo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
language: node_js
node_js: stable
branches:
only:
- hexo
cache:
apt: true
yarn: true
directories:
- node_modules
before_install:
- git config --global user.name "Simon Dong"
- git config --global user.email "snovian@gmail.com"
- git submodule update --init --recursive --remote
- mkdir -p _publish
- rm -rf source/images
- mv source/_posts/images source/
- sed -i "s/^build:\ .*$/build:\ `echo \$TRAVIS_BUILD_NUMBER`/" _config.yml
install:
- npm install -g hexo-cli
- npm install
script:
- hexo clean
- hexo generate
after_success:
- rm -rf _publish/*
- mv public/* _publish/
- cd _publish
- git init
- git add -v --all .
- git commit -m "Travis CI Auto Builder@$TRAVIS_BUILD_NUMBER"
- git push -v --force "https://$REPO_TOKEN@github.com/snovian/snovian.github.io.git" master:master