Travis CI 是一个非常好用持续集成工具。
集成 主要是用来将多个用户的开发模块构建成一个可运行版本;而 持续集成 则是在集成之上,尽量将每一次提交都进行一次构建,这个个过程就是 持续集成 。
Travis Ci 的自动构建周期分为两步:
但是我们可以根据这两步将相关的内容分成更细的步骤:
所以我们可以这样认为,持续集成就是把一系列的手工操作合并成一个脚本的过程。
我们可以这样实现我们的博客部署脚本
sudo: false
language: go
os: osx
install:
brew install hugo
script:
- hugo --config jane-config.toml
branches:
only:
- source
after_success:
- git add -A
- git commit -m "update blog"
- git push -u origin master
这个脚本中,我们主要工作是生成 hugo 博客这一步,如果成功了,我们就进行提交,也就完成了。
经过查阅之后,发现 Travis Ci 本身就支持直接部署到 GitHub Pages 上,并拥有单独的章节。
在 GitHub 中的 Setting 下的 Developer settings 中,有一个 Personal access tokens 中,可以生成,然后配置到 Travis Ci 对应的 My Repositories 中的项目中,一般的话,使用 public_repo 权限就足够了。
如果在 My Repositories 中看不到 Settings ,可以在 More options 中找到 Settings 然后在 Environment Variables 中配置对应的令牌即可。
在项目中新增 .travis.yml ,内容如下:
deploy:
provider: pages
skip-cleanup: true
github-token: $GITHUB_TOKEN
# Set in the settings page of your repository, as a secure variable
keep-history: true
on:
branch: master
git push -f
备注:
git submodule init
,但是需要使用 https 的路径,否则需要配置 ssh key 除了令牌之后,我们也可以将 name 、 email 等相关信息配置到这里,比如推送的分支,可以设置为 P_BRANCH 然后在推送的时候,使用 ${P_BRANCH} 来引用。