0%

GitHub MarketPlace 安装 TravisCI 自动部署 gitbook

GitHub 集成 Travis CI 用于自动部署 GitHub Pages

GitHub Market Place

GitHub Market Place 选择 Travis CI 进行安装,Open Source 工程是免费的。

输入密码完成安装

Environment Variables

Environment Variables

Travis CI 中定义环境变量有如下几种方式

  • Defining Public Variables in .travis.yml
  • Defining encrypted variables in .travis.yml
  • Defining Variables in Repository Settings
  • Convenience Variables

GitHub Token

Repository -> Settings -> Developer settings -> Personal access tokens

Make sure to copy your new personal access token now. You won’t be able to see it again!

  • https://travis-ci.com/travis login --pro – 付费版 – GitHub
  • https://travis-ci.org/travis login --org – 免费版

在 Travis CI 中添加环境变量

官方文档在这里

每个仓库的设置页面里,有一个 Environment Variables 的配置项,给我们的 Token 起一个名字 GITHUB_TOKEN 添加进去。这样以来,脚本内部就可以使用这个环境变量了。

git push -f "https://${GITHUB_TOKEN}@${GH_REF}" gh-pages:gh-pages

By default, the value of these new environment variables is hidden from the export line in the logs. This corresponds to the behavior of encrypted variables in your .travis.yml. The variables are stored encrypted in our systems, and get decrypted when the build script is generated.

加密 Token

https://travis-ci.com, see Encryption keys – Usage.

$ sudo apt install ruby
$ sudo apt install gem
$ sudo apt install ruby-all-dev
$ sudo gem install travis

输入 GitHub 用户名和密码?

$ travis login --pro --github-token xxxxxxxxx
$ travis login --pro
We need your GitHub login to identify you.
This information will not be sent to Travis CI, only to api.github.com.
The password will not be displayed.

Try running with --github-token or --auto if you don't want to enter your password anyway.

Username: xxxxxxxx
Password for xxxxxxxx: **********
Successfully logged in as xxxxxxxxx!
$ travis encrypt --pro GITHUB_TOKEN=<YOUR_GITHUB_TOKEN> --add

GitHub 使用 https://travis-ci.org/,加密时使用 --pro 保证正确

CI Log

Settings 中获取

Setting environment variables from repository settings
$ export GITHUB_TOKEN=[secure]
Setting environment variables from .travis.yml
$ export GH_REF=github.com/breezetemple/how-to-use-gitbook.git

.travis.yml 加密字符串中获取

Setting environment variables from .travis.yml
$ export GH_REF=github.com/breezetemple/how-to-use-gitbook.git
$ export GITHUB_TOKEN=[secure]

Travis CI

Repository -> Settings -> Applications -> Travis CI -> Configure -> Only select repositories

需要如下权限

  • Read access to code
  • Read access to metadata and pull requests
  • Read and write access to checks, commit statuses, deployments, and repository hooks

website

使用部署

GitHub Pages Deployment

language: generic
before_install:
- npm install -g gitbook-cli
install:
- gitbook install
- gitbook build
deploy:
  provider: pages
  local_dir: _book
  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
env:
  global:
  - GH_REF: github.com/breezetemple/how-to-use-gitbook.git

日志

Installing deploy dependencies
Logged in as @breezetemple ()
dpl.2
Preparing deploy
dpl.3
Deploying application
cd /tmp/d20191005-4465-c01j28/work
commit f8ff418258f8f1f8dd5320c2db79c46ce628d742
Author: Deployment Bot (from Travis CI) <deploy@travis-ci.org>
Date:   Sat Oct 5 04:04:35 2019 +0000
    Deploy breezetemple/how-to-use-gitbook to github.com/breezetemple/how-to-use-gitbook.git:gh-pages
 .travis.yml                                   |   1 +
 README.md                                     |   5 --
 SUMMARY.md                                    |  18 ----
 _book/.gitignore                              |  16 ----
 _book/.travis.yml                             |  16 ----
 basic-usage/README.md                         | 122 --------------------------
 {_book/basic-usage => basic-usage}/index.html |   2 +-
 book.json                                     |  50 -----------
 customize/README.md                           |   4 -
 {_book/customize => customize}/book.json.html |   2 +-
 ...
 4759 files changed, 13 insertions(+), 610832 deletions(-)
cd -

使用脚本

language: generic
before_install:
- npm install -g gitbook-cli
- rm -rf _build
install:
- gitbook install
- gitbook build
script:
- git checkout --orphan gh-pages
- git rm --cached -r .
- git clean -df
- rm -rf *~
- echo "*~" > .gitignore
- echo "_book" >> .gitignore
- echo "node_modules" >> .gitignore
- git add .gitignore
- git commit -m "Ignore some files"
- cp -r _book/* .
- git add .
- git commit -m "Publish book"
- git push --force --quiet "https://${GITHUB_TOKEN}@${GH_REF}" gh-pages:gh-pages
branches:
  only:
  - master
env:
  global:
  - GH_REF: github.com/breezetemple/how-to-use-gitbook.git
  - secure: encrypted data

Ref

  1. 使用 Travis CI 自动更新 GitHub Pages
  2. 用 Travis CI 自动部署 Hexo 博客
  3. 如何简单入门使用 Travis-CI 持续集成
  4. 使用 Travis CI 自动更新 GitHub Pages
  5. 用 Travis CI 自动部署 Github Pages
  6. Adding GitHub token to Travis CI configuration