Travis CI 是一個已經建置好而且免費的持續整合系統,並且支援多種語言。主要是用來與 github 上的 repo 結合,只要有push code上去,Travis CI 就會自動幫你測試

CI 的全名是 Continous Integration。軟體開發會常遇到當專案的程式一多,測試過程也會變的複雜。例如一個專案測試需要經過十個unit test測試,但每次送一次commit就要人工跑一次這十個測試會很麻煩,因此可以透過 CI Server 來幫助你完成。

Travis CI Build Lifecycle
在使用Travis CI前,必須先瞭解它幫你佈署的流程(取自 官網)

  • 1. Clone project repository from GitHub
  • 2. cd to clone directory
  • 3. Checkout commit for this build
  • 4. Run "before_install" commands
  • 5. Run "install" commands
  • 6. Run "before_script" commands
  • 7. Run test "script" commands
  • 8. Run "after_success" or "after_failure" commands
  • 9. Run "after_script" commands

建立.travis.yml
Travis CI 會按照專案根目錄的 .travis.yml 檔案的描述來執行單元測試

以下為PHP版本為例

1# 選擇程式語言
2language: php
3 
4# 選擇版本
5php:
6  - 5.4
7  - 5.5
8  - 5.6
9  - hhvm
10 
11# 設定參數(Travis CI 會按照參數分別執行)
12env:
13  - DB=mysql
14  - DB=pgsql
15 
16# 在 install 前執行的指令
17before_install:
18  - if [[ "$DB" == "pgsql" ]]; then psql -c "DROP DATABASE IF EXISTS hello_world_test;" -U postgres; fi
19  - if [[ "$DB" == "pgsql" ]]; then psql -c "create database hello_world_test;" -U postgres; fi
20  - if [[ "$DB" == "mysql" ]]; then mysql -e "create database IF NOT EXISTS hello_world_test;" -uroot; fi
21 
22# install 指令
23install:
24  - ./install.sh
25 
26# 在執行測試之前的指令
27before_script:
28  - composer self-update
29  - composer install --prefer-source --no-interaction --dev
30 
31# 執行
32script: phpunit
33 
34# 特殊的判斷
35# allow_failures: 在特定版本發生錯誤可以略過.
36# fast_finish: 當這個參數為 true 時,如發生錯誤就立即中止
37matrix:
38  allow_failures:
39    - php: 5.6
40    - php: hhvm
41  fast_finish: true
42 
43# 自訂通知
44notifications:
45  email:
46    recipients:
47      - xx@gmail.com
48    # [always|never|change]
49    on_success: always # default: change
50    on_failure: always # default: always
51  irc: "chat.freenode.net#my-channel"