原文连接:blog.sjfkai.com/2017/11/04/…html
转载请注明出处前端
若是你曾了解过Hexo, 相信你已经能够经过hexo deploy
部署本身的博客到github pages
了。node
可是,这样咱们仅仅能够经过当前电脑来发布博客,若是电脑不在身边,或者不当心把文件删除了,就会很麻烦。git
本文教你如何实现把本身写的博客和生成的静态文件同时托管在github
:github
细心的人能够发现,经过hexo init
生成的文件中,已经包含了.gitignore
文件。说明hexo
开发者也是但愿你们把博客源码托管到git
上的。npm
咱们能够经过分支来实现,将源码放在master
(看我的喜爱)分支、将hexo deploy
的分支指定为gh-pages
分支。具体实现以下:hexo
若是没有github repository
首先须要在github
新建一个仓库,点击new repository
。分布式
hexo init
生成一个新的博客项目测试
根据hexo 文档修改配置,使博客能够经过git server
预览。ui
git init
git remote add origin git@github.com:account/blog_repo.git
请将git
地址改成1
中说到的你本身的仓库地址
git add .
git commit -m "init"
git push origin master -f
将代码推到github
master
分支
这时候,咱们就把项目托管到了github
,当你换了一台电脑,或者不当心把文件删除了的时候,只要从新git clone
就能够了。
可是这仅仅只是将代码托管到了github
。当咱们新完成一篇博客,并把代码push
到github
上的时候,并不会自动deploy
。
Travis CI是在软件开发领域中的一个在线的,分布式的持续集成服务,用来构建及测试在GitHub托管的代码。
使用 github
受权登陆 Travis CI
在 Travis CI
中打开博客项目仓库的开关。并在配置中打开Build only if .travis.yml is present
选项
在 github
中建立access token
,详细教程:Creating a personal access token for the command line
在travis
博客仓库的配置中将刚刚生成的token
添加到Environment Variables
中,name为REPO_TOKEN
.travis.yml
文件。配置以下language: node_js
node_js: stable
branches:
only:
- master
cache:
directories:
- node_modules
before_install:
- git config --global user.name "sjfkai"
- git config --global user.email "sjfkai@163.com"
- npm install -g hexo-cli
- export HEXO_DEPLOYER_REPO=https://$REPO_TOKEN@github.com/sjfkai/blog.git
install:
- npm i
script:
- hexo clean
- hexo generate
- hexo deploy
复制代码
_config.yml
的deploy
:# 注意,这里注释掉了repo, 由于咱们须要在ci中经过环境变量 HEXO_DEPLOYER_REPO 配置
deploy:
type: git
# repo:
branch: gh-pages
复制代码
git add .
git commit -m "add travis ci"
git push origin master
这时候你会发现travis ci
显示该项目处于running
状态。 等最后变为 passed
状态后。github pages
就已经自动部署成功了。
欢迎关注公众号 “大前端开发者”。给你带来更多的前端技术与资讯