###背景
每次写完blog,都要在本地hexo环境编译,等编译完成后,再push到github,重复的动作,浪费的时间。
懒惰是互联网进步的第一动力!我能不能每次写完md,直接push到github,然后它自动编译,编译完成后,自己push到仓库主分支呢?肯定是可以的,答案就是使用Travis CI自动构建工具。
###关联Github和Travis CI
Github 有提供一个 Personal access tokens,这个 Token 与 账号密码 以及 SSH Keys 同样具有 Github 写入能力。
前往 Github 帐号 Settings 页面,在左侧选择 Personal Access Token,然后在右侧面板点击 “Generate new token” 来新建一个 Token。需要注意的是,创建完的 Token 只有第一次可见,之后再访问就无法看见(只能看见他的名称),因此要保存好这个值。

###登录Travis CI关联对应项目
首先打开官方网站 travis-ci.org,然后使用 Github 账号登入 Travis CI,然后 Travis 中会列出你 Github 上面所有的仓库,以及你所属于的组织。
然后,勾选你需要 Travis 帮你自动构建的仓库,打开仓库旁边的开关,打开以后,Travis 就会监听这个仓库的所有变化了。
![E1BF98EB2CB8D8E4E6E1F6E8E3BDC5EA.jpg][2]
###travis.yml配置
原理就是监控对应的文件,有push就开始构建。
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
| language: node_js # 声明环境为node node_js: stable
# Travis-CI Caching cache: directories: - node_modules # 缓存node_modules文件夹
# S: Build Lifecycle before_install: - export TZ='Asia/Shanghai' # 更改时区 - npm install hexo-cli -g
install: - npm install # 下载依赖
script: - hexo clean - hexo g
after_script: # 推送到github的部分 - 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 "xxxxxxx" - git config user.email "[email protected]" - git add . - git commit -m "Travis CI Auto Builder at `date +"%Y-%m-%d %H:%M"`" - git push --force --quiet "https://${GH_TOKEN}@${GH_REF}" master:master
# E: Build LifeCycle branches: only: - hexo # 只对hexo分支构建
|
###Build Config
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
| { "only": [ "hexo" ], "script": [ "hexo clean", "hexo g" ], "install": [ "npm install" ], "node_js": "stable", "language": "node_js", "directories": [ "node_modules" ], "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 \"xxxxxxxx\"", "git config user.email \"[email protected]\"", "git add .", "git commit -m \"Travis CI Auto Builder at `date +\"%Y-%m-%d %H:%M\"`\"", "git push --force --quiet \"https://${GH_TOKEN}@${GH_REF}\" master:master" ], "before_install": [ "export TZ='Asia/Shanghai'", "npm install hexo-cli -g" ] }
|