使用Travis CI自动部署Hexo博客

wshunli
2017-11-09 / 0 评论 / 239 阅读 / 正在检测是否收录...

随着 Hexo 文章的增多,加上压缩文件的时间,部署时间越来越长。

我一般都是将博客的源文件保存到私有仓库中,虽然几个简单地命令就能部署,但是过程还是很繁琐。最近申请了 Github Student Developer Pack ,就想利用软件开发中的持续集成工具 Travis CI 来帮助完成 Hexo 博客的部署过程。这样就只需要把源代码 push 到仓库就能自动部署好了。

CI 是 Continuous Integration 的缩写,持续集成之意。持续集成是一种软件开发实践,每次集成都通过自动化的构建(包括编译,发布,自动化测试)来验证,从而尽早地发现集成错误。

Hexo 搭建

关于 Hexo 博客的搭建请参考文章:https://www.wshunli.com/posts/9642fffa.html

Travis CI 配置

1.使用 GitHub 账户登录 Travis CI 官网,然后用 Github 账号登陆,就可以看到可以持续集成的仓库。

还有个地址 https://travis-ci.com 可以部署 Github 私有仓库。

选择要启用的项目,打开

然后选择一些通用的设置

2.登陆 GitHub –Settings -Developer Settings 选项,找到 Personal access tokens 页面。

点击右上角的 Generate new token 按钮会生成新的token,点击后提示输入密码后继续,然后来到如下界面。
取个名字,勾选相应权限,这里只需要 repo 下全部和 user 下的 user:email 即可。

生成完成后,将该token拷贝下来。

如果需要将代码同时部署到 Coding 类似
3.登陆 Coding ,账户 -访问令牌,新建访问令牌:

勾选相应权限:

4.将上面获取到的token添加到 Environment Variables 部分,值为该 token ,而名称为 GH_TOKEN、CD_TOKEN 。

创建 .travis.yml

要想自动部署还需在博客源码新增加 .travis.yml 配置文件。

# 设置语言
language: node_js
# 设置相应的版本
node_js:
  - '6'
# 设置只监听哪个分支
branches:
  only:
    - master
# 缓存,可以节省集成的时间
cache:
  directories:
    - node_modules
before_install:
  - npm install -g hexo-cli
install:
  - npm install
script:
  - hexo clean
  - hexo g
after_script:
    - cd ./public
    - git init
    - git config user.name "yourname" # 修改name
    - git config user.email "youremail" # 修改email
    - git add .
    - git commit -m "Travis CI Auto Builder"
    - git push --force --quiet "https://${GH_TOKEN}@${GH_REF}" master:master 
    # GH_TOKEN 是在 Travis 中配置 token 的名称
branches:
    only:
        - master #只监测 master 分支,master是我的分支的名称,可根据自己情况设置
env:
    global:
        - GH_REF: github.com/yourname/yourname.github.io.git 
        # 设置 GH_REF,注意更改 yourname

实现自动部署

将原代码 push 到 Github 即可,如果配置没问题就该自动构建了。

问题

虽然实现了基本的自动部署,还是有问题的。

master commit 树被清空

自动部署会把原来的 commit 清空,显然不是我们想要的结果。

为了解决这个问题,将配置文件改为了如下的内容:

after_script:
    - git clone https://${GH_REF} .deploy_git
    - cd .deploy_git
    - git checkout master
    - cd ../
    - mv .deploy_git/.git/ ./public/
    - cd ./public
    - git config user.name "yourname"
    - git config user.email "your email"
    - git add .
    - git commit -m "Travis CI Auto Builder"
    - git push --force --quiet "https://${GH_TOKEN}@${GH_REF}" master:master

添加 commit 时间戳

commit 的描述一直是 Travis CI Auto Builder

Travis CI Auto Builder
Travis CI Auto Builder
Travis CI Auto Builder
// 之前
Site updated: 2017-11-07 22:05:16
Site updated: 2017-11-07 17:19:32
Site updated: 2017-11-06 18:04:53

最终 .travis.yml 配置文件:

# 设置语言
language: node_js
# 设置相应的版本
node_js:
  - '6'
# 设置只监听哪个分支
branches:
  only:
    - master
# 缓存,可以节省集成的时间
cache:
  directories:
    - node_modules
before_install:
  - export TZ='Asia/Shanghai'
  - npm install -g hexo-cli
  - chmod +x ./publish-to-gh-pages.sh
install:
  - npm install
script:
  - hexo clean
  - hexo g

after_script:
  - ./publish-to-gh-pages.sh
env:
    global:
        - GH_REF: github.com/yourname/yourname.github.io.git
        # 设置GH_REF,注意更改成自己的仓库地址
        - CD_REF: git.coding.net/yourname/repository.git
        # 同时部署到 Coding,repository 为仓库名称

其中 publish-to-gh-pages.sh 文件:

#!/bin/bash
set -ev
git clone https://${GH_REF} .deploy_git
cd .deploy_git
git checkout master
cd ../
mv .deploy_git/.git/ ./public/
cd ./public
git config user.name  "yourname"
git config user.email "youremail"
git add .
git commit -m "Travis CI Auto Builder at `date +"%Y-%m-%d %H:%M"`"
git push --force --quiet "https://${TravisCIToken}@${GH_REF}" master:master
git push --force --quiet "https://yourname:${CD_TOKEN}@${CD_REF}" master:master
# 同时部署到 Coding,注意修改 yourname

参考资料
1、使用Travis CI自动部署Hexo博客
http://www.itfanr.cc/2017/08/09/using-travis-ci-automatic-deploy-hexo-blogs/
2、使用 Travis CI 自动部署 Hexo
http://www.jianshu.com/p/5e74046e7a0f
3、使用 Travis 自动部署 Hexo 到 Github 与 自己的服务器
https://segmentfault.com/a/1190000009054888

0

评论 (0)

取消