Hexo


  • Startseite

  • Archiv

  • Tags
Hexo

Hello all

Veröffentlicht am 2018-03-11

Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub.

Quick Start

Create a new post

1
$ hexo new "My New Post"

More info: Writing

Run server

1
$ hexo server

More info: Server

Generate static files

1
$ hexo generate

More info: Generating

Deploy to remote sites

1
$ hexo deploy

More info: Deployment

Hexo

hexo+Travis-ci+github构建自动化博客

Veröffentlicht am 2018-03-11

在mac上 用hexo+Travis-ci+github构建自动化博客。

Travis CI:是目前新兴的开源持续集成构建项目,它与jenkins,GO的很明显的特别在于采用yaml格式,简洁清新独树一帜。目前大多数的github项目都已经移入到Travis CI的构建队列中,据说Travis CI每天运行超过4000次完整构建。

说一下我对Travis 的理解。tarvis-ci是远端服务器构建,跟jenkins 不一样,不用自己搭建一个jenkins服务器。tarvis-ci是用的远端官方提供的服务器。用hexo 搭建博客,然后将博客源码提交到 git上,用travis-ci 编译git 上的源码,然后将编译后的文件,推送到Github.io仓库下,达到自动化编译构建。


##构建自动化博客的步骤

由于需要搭建hexo 和本地搭建Travis 所以本地需要有ruby 和 node 环境。本地搭建Travis 是为了和Travis-ci 服务器进行加密解密获得密匙,获得一次即可。

###1.安装rubu 和node.js

###2.搭建hexo

###3.安装 travis

###4.配置git 的ssh key

###5.配置Travis

###6.验证


#1.安装ruby和node

我是通过brew安装 ruby 和node 的,(如果brew 没有安装请参考mac安装brew)在终端执行命令

1
2
brew install ruby 
brew install node


#2.搭建hexo
此处参考博客
hexo+github 分分钟搭建博客

博客搭建成功后在github 的527515025.github.io 仓库下新建travis分支,并将博客源码(未编译过的代码)提交到该分支,用于以后Travis-ci编译使用。

#3.安装 travis
brew和node安装成功后执行下面命令安装travis

1
gem install travis


#4.配置git 的ssh key
参考
SSH连接GitHub、GitHub配置ssh key

#5.配置Travis
travis安装成功后就需要配置travis了,配置travis比较麻烦主要有如下步骤

(1):进入travis官网Travis 官网传送门用github账号登陆Travis CI

这里写图片描述

(2 ):在右上角你的账户名点击进入 account,在Repositories tab页点击Sync now同步你的github项目。选中项目将默认的off改变为on开启项目的持续集成。

这里写图片描述

(3):在你项目的根目录建立一个.travis文件夹

1
2
$ cd 博客项目文件夹根目录
$ mkdir .travis

(4)复制id_rsa 和 创建 ssh_config文件

1
2
$ cd 博客项目文件夹根目录/.travis
$ cp ~/.ssh/id_rsa ./

(5)创建 ssh_config 文件

1
2
$ cd 博客项目文件夹根目录/.travis
$ touch ssh_config

这里写图片描述

编辑 ssh_config ,输入以下信息

1
2
3
4
5
6
7
8
$ vi ssh_config 

//输入以下信息
Host github.com
User git
StrictHostKeyChecking no
IdentityFile ~/.ssh/id_rsa
IdentitiesOnly yes

这里写图片描述

(6)travis 登陆

1
2
3
4
5
6
7
8
9
10
11
$ cd 博客项目文件夹根目录/.travis
$ travis login --auto
We need your GitHub login to identify you.
This information will not be sent to Travis CI, only to api.github.com.
The password will not be displayed.

Try running with --github-token or --auto if you don't want to enter your password anyway.

Username: 527515025
Password for 527515025: ************
Successfully logged in as 527515025!

(7)加密操作

加密操作
在博客项目文件夹下对ssh的私钥进行加密

1
2
$ cd 博客项目文件夹根目录/.travis
$ travis encrypt-file id_rsa --add

如图
这里写图片描述
此操作会生成加密之后的秘钥文件 id_rsa.enc,删除id_rsa密钥文件(私钥不能随便泄露)。

你的travis 目前上已经出现了下面加密解密key :
(点击第2步图中的仓库名可进入下图界面,点击setting可看到加密)

这里写图片描述

(8)在你项目的根目录建立一个.travis.yml文件,内容为:
此步是在我527515025.github.io仓库的travis 分支下编译源码。
(用第7步中Travis 生成的 encrypted_xxxxxxxxx_key -iv 和encrypted_xxxxxxxxx_iv 替换travis.yml 中openssl的 key 和 -iv)

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
language: node_js
node_js:
- '4'
branches:
only:
- travis
cache:
directories:
- node_modules
before_install:
- openssl aes-256-cbc -K $encrypted_xxxxxxxxx_key -iv $encrypted_xxxxxxxxx_iv
-in .travis/id_rsa.enc -out ~/.ssh/id_rsa -d
- chmod 600 ~/.ssh/id_rsa
- eval $(ssh-agent)
- ssh-add ~/.ssh/id_rsa
- cp .travis/ssh_config ~/.ssh/config
- git config --global user.name "527515025"
- git config --global user.email "527515025@qq.com"
install:
- npm install hexo-cli -g
- npm install hexo-deployer-git --save
- npm install
script:
- hexo clean
- hexo g

after_success:
- hexo deploy

(9)_config.yml 配置如下:
这是将编译好的代码提交527515025.github.io 仓库的master分支

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


#6.验证
此时部署已经成功,在项目源码中也就是travis 分支上提交一个新的博客(也就是一个.md文件)我门在travis-ci 网站上看到正在自动化构建

(黄色为正在构建,绿色为构建完成)

这里写图片描述

构建完成后就可以访问 xxxx.github.io 查看是否自动发布成功

这里写图片描述

John Doe

John Doe

2 Artikel
© 2018 John Doe
Erstellt mit Hexo
Theme - NexT.Muse