持续集成(continuous integration)是一种软件开发实践,即团队开发成员经常集成他们的工作,通过每个成员每天至少集成一次,也就意味着每天可能会发生多次集成。每次集成都通过自动化的构建(包括编译,发布,自动化测试)来验证,从而尽早地发现集成错误。

 

Travis CI 是一个开源软件测试网站 ,它和Github无缝对接,主要用于对提交的代码进行自动化测试,支持各种常见开发语言(开发语言支持列表)。在Github项目中编写好配置文件后,每次提交代码,Travis CI都会按照配置,自动生成对应的运行环境,并对代码进行测试。

Travis CI官网上宣传的几大优点:

  • Watch your tests as they run
  • Keep your config with your code
  • Slack, HipChat, Emails and more
  • A clean VM for every build
  • Run your tests in parallel
  • Linux and Mac (and iOS) supported
  • Great API and command line tool
  • Did we say free for Open Source already?

下面是使用Travis CI 的步骤:

1.使用github账号登录Travis CI,同步github项目列表到Travis CI

2.在Travis CI项目列表中打开需要持续集成项目的开关

3.在Github上提交Travis配置文件到对应的项目

4.在Github上提交对应项目的代码(像平时一样)

5.在Travis CI上查看对应项目测试日志及测试结果

 

下面以我在Github上面的项目laravel-starter-kit为例,介绍Travis CI配置文件编写方法

首先需要介绍一下Travis CI构建项目时的生命周期:

before_install -> install -> before_script -> script -> after_success(after_failure) ->  before_deploy(可选) -> deploy(可选) -> after_deploy(可选) -> after_script

 

下面是laravel-starter-kit的Travis CI配置文件(.travis.yml):

配置文件的主要目的是让Travis CI执行以下操作:配置php版本,添加hosts,运行mysql,使用composer安装依赖类库,修改laravel配置文件.env,初始化laravel项目,安装apache和php-fpm,配置vhost,执行phpunit测试

language: php

php:
  - 5.5
  - 5.6

addons:
  hosts:
    - laravelstarterkit

services:
  - mysql

mysql:
  database: laravel
  username: root
  encoding: utf8

install:
  - composer install --prefer-dist --optimize-autoloader

before_script:
  #make .env
  - cp .env.example .env
  - sed -i 's/APP_URL=http:\/\/localhost/APP_URL=http:\/\/laravelstarterkit/g' .env
  - sed -i 's/DB_DATABASE=homestead/DB_DATABASE=laravel/g' .env
  - sed -i 's/DB_USERNAME=homestead/DB_USERNAME=root/g' .env
  - sed -i 's/DB_PASSWORD=secret/DB_PASSWORD=/g' .env
  #create database
  - mysql -uroot -e 'create database laravel;'
  #setup laravel app
  - php artisan key:generate
  - php artisan make:auth
  - php artisan migrate
  - php artisan db:seed
  #insall apache
  - sudo apt-get update
  - sudo apt-get install apache2 libapache2-mod-fastcgi
  #enable php-fpm
  - sudo cp ~/.phpenv/versions/$(phpenv version-name)/etc/php-fpm.conf.default ~/.phpenv/versions/$(phpenv version-name)/etc/php-fpm.conf
  - sudo a2enmod rewrite actions fastcgi alias
  - echo "cgi.fix_pathinfo = 1" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
  - ~/.phpenv/versions/$(phpenv version-name)/sbin/php-fpm
  #configure vhosts
  - sudo cp -f build/travis-ci-apache /etc/apache2/sites-available/default
  - sudo sed -e "s?%TRAVIS_BUILD_DIR%?$(pwd)?g" --in-place /etc/apache2/sites-available/default
  - sudo service apache2 restart

script: phpunit --configuration phpunit.xml

vhost配置文件(build/travis-ci-apache):

<VirtualHost *:80>
  DocumentRoot %TRAVIS_BUILD_DIR%/public
  ServerName laravelstarterkit
  ErrorLog /var/log/apache2/travis_error.log
  CustomLog /var/log/apache2/travis_access.log common

  <Directory "%TRAVIS_BUILD_DIR%/public">
    Options FollowSymLinks MultiViews ExecCGI
    AllowOverride All
    Order deny,allow
    Allow from all
  </Directory>

  # Wire up Apache to use Travis CI's php-fpm.
  <IfModule mod_fastcgi.c>
    AddHandler php5-fcgi .php
    Action php5-fcgi /php5-fcgi
    Alias /php5-fcgi /usr/lib/cgi-bin/php5-fcgi
    FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi -host 127.0.0.1:9000 -pass-header Authorization
  </IfModule>
</VirtualHost>

 

下面是Github提交代码后,Travis CI的测试结果(查看详情):

laravel-starter-kit-Travis-CI

laravel-starter-kit-Travis-CI

Post Navigation