利用 Travis CI 自动部署 Hexo 博客

Hexo 博客每次写完文章后都需要手动执行 hexo cleanhexo ghexo d 命令来上传与生成静态网页,这个过程对于博客的维护管理来说很是麻烦。于是想着怎么样简化这个过程,减少冗余的工作量。
Google 搜索 hexo 自动部署 后决定利用 Travis CI 来实现 hexo 博客的自动部署。

简述自动部署 hexo 博客实现过程

(一)在博客仓库下新建名为 hexo 的分支:

具体实现过程请自行 Google/Baidu 。

(二)添加 .travis.yml 在博客源文件根目录下并上传:

.travis.yml 内容:

language: node_js
node_js: stable

cache:
  directories:
    - node_modules
before_install:
  - npm install hexo-cli -g

install:
  - npm install
  - npm install hexo-deployer-git --save

script:
  - hexo clean
  - hexo generate

after_script:
  - cd ./public
  - git init
  - git config user.name "username"
  - git config user.email "useremail"
  - git add .
  - git commit -m "Update docs"
  - git push --force --quiet "https://${GH_TOKEN}@${GH_REF}" master:master

branches:
  only:
    - hexo
env:
 global:
   - GH_REF: github.com/username/username.github.io.git

(三)配置GitHub Access Token:

GitHub 主页 ——> Settings ——> Developer Settings ——> Personal access tokens ——> Generate new token

(四)Travis CI 设置:

   (1)登录 Travis 网站用 github 授权登录;
   (2)登录后在个人主页选择你需要 CI 的仓库;
   (3)点击你选择的 hexo 博客的仓库进行配置;
   (4)在 Travis 仓库配置界面 setting 里面对环境变量 Environment Variables 进行 token 配置;

(五)撰写文章并 push 到 github pages:

每次写完文章,只需要执行下面的命令,其余部分会自动完成部署。

git add .
git commit -m "updated docs"
git push origin hexo

参考文章:
(1)利用 CI 自动部署 hexo 博客、
(2)Travis CI 自动部署 hexo 到 GitHub/Coding


你可能感兴趣的:(利用 Travis CI 自动部署 Hexo 博客)