Alex Dai Playground

[Travis CI] Install Client

| Comments

1. 必須先安裝 ruby & RubyGems (https://github.com/travis-ci/travis.rb#installation)

2. gem install travis

P.S windows 7在安裝時發生錯誤:

bad response Not Found 404 (https://rubygems.global.ssl.fastly.net/quick/Marshal.4.8/pry-0.9.12.6-x86-mingw32.gemspec.rz),

似乎是 gem 版本的問題 (http://stackoverflow.com/questions/28835959/ruby-shoes-app-gem-chef-bad-response-not-found-404)
解決辦法: $ gem update --system 2.4.4 (退回2.4.4版)

3. repo 目錄 下執行

先建立 .travis.yml 檔案

$ touch .travis.yml

設定 git

.git/config 加入以下

[travis]
    slug = <user/org>/<repo>
設定 travis 可以 push 回 github
方法1.

#取得 GitHub Personal access tokens
Personal settings -> Personal access tokens

# 登入到 Travis

$ travis login --auto --github-token <Personal Access Token>

# Token 加密

$ travis encrypt 'GIT_NAME="your_name" GIT_EMAIL="your_email" GH_TOKEN=<Personal Access Token>' --add

P.S --add 此參數會將結果直接寫入.travis.yml

此時 .travis.yml 會出現:

env:
       global:
         secure: .... 一大串英數與符號 ....

在其他地方需使用變數時,例如 GtHub Personal Access Token

只需 $GH_TOKEN 即為 上面3-1拿到到的 Access Token

設定
script:
- echo -e "Host github.com\n\tStrictHostKeyChecking no\n" >> ~/.ssh/config # 避免在透過 SSH 連到 github.com 的時候需要確認 
- git config --global user.name "$GIT_NAME"
- git config --global user.email "$GIT_EMAIL"
- git clone --depth 1 --quiet https://${GH_TOKEN}@github.com/<Repository Path> > /dev/null

add remote
git remote add origin https://${GH_TOKEN}@github.com/your_account/project_name.git

push
git push -u --quiet origin

P.S 記得加 --quiet 否則 GH_TOKEN的值會顯示出來,那之前的加密就白費了

方法2.

#使用 SSH Key

產生 ssh key

ssh-keygen -t rsa -C "your-email"

設定 name 為 travis 將產生2個檔案: travis_key & travis_key.pub

將 travis_key.pub 貼到 github repo Deploy key

$ travis encrypt-file travis_key --add (必須先登入travis)

Travis 會自動解析目前的 repo 並把 Private key 上傳到相對應的 repo,

將會產生 travis_key 加密 過後的檔案 travis_key.enc, 並寫入相關設定到.travis.yml

完成後記得移除在repo目錄下的 travis_key 檔案, 保留 travis_key.enc

結果如下:

  $ - openssl aes-256-cbc -K $encrypted_fc0df..._key -iv $encrypted_fc0df..._iv -in travis_key.enc -out travis_key -d

察看環境變數

travis env list

問題

# travis 執行時,無法開啟加密的檔案 openssl failed and exited with 1 during
Windows 7 x64 的問題 https://github.com/travis-ci/travis-ci/issues/4746
解決辦法 使用其他作業系統,如linux (ruby version install 2.2.3)

  1. 檢查環境變數 $ travis env list
  2. 移除環境變數 $ travis env unset encrypted_fc0df..._key $ travis env unset encrypted_fc0df_...._iv
  3. 檢查是否已登入 travis $ travis whoami
  4. 重新將檔案加密,並上傳至對應的repo travis encrypt-file -r github_account/project_name your_file

repository not known to https://api.travis-ci.org/: account/repos

problem is to edit the file at .git/

[travis]
    slug = <user/org>/<repo>

Encrypting multiple files

$ tar cvf secrets.tar foo bar
$ travis encrypt-file secrets.tar
----------------------------------------

.travis.yml

before_install:
  - openssl aes-256-cbc -K $encrypted_5880cf525281_key -iv $encrypted_5880cf525281_iv -in secrets.tar.enc  -out secrets.tar -d
  - tar xvf secrets.tar

驗證 .travis.yml 設定

1.http://lint.travis-ci.org/

2.travis-lint(requires Ruby 1.8.7+ and RubyGems install)

#install:
gem install travis-lint

#Usage:
inside a repository with .travis.yml
travis-lint

#from any directory
travis-lint [path to your .travis.yml]


travis-ci 生命週期

Build Lifecycle

1.Clone project repository from GitHub
2.cd to clone directory
3.Checkout commit for this build
4.Run before_install commands
Use this to prepare the system to install prerequisites or dependencies
e.g. sudo apt-get update
5.Run install commands
Use this to install any prerequisites or dependencies necessary to run your build
6.Run before_script commands
Use this to prepare your build for testing
e.g. copy database configurations, environment variables, etc.
7.Run test script commands
Default is specific to project language
All commands must exit with code 0 on success. Anything else is considered failure.
8.Run after_success or after_failure commands
9.Run after_script commands

指定分支

黑名單
branches:
  except:
    - legacy
    - experimental

白名單
 branches:
   only:
     - master
     - stable
     - /^deploy-.*$/

如何略過觸發 travis 的 build

commit 的文字中任何地方 加入 [ci skip] or [skip ci]

travis-ci environment-variables

http://docs.travis-ci.com/user/environment-variables/

Trigger a Travis-CI rebuild without pushing a commit

$ git commit --allow-empty -m "Trigger"

Trigger a Travis-CI rebuild via cli

$ travis show master
$ travis restart 48 #use Job number without .1
$ travis logs master

Comments

comments powered by Disqus