Jenkins Tutorial (以PHPUnit为例)

Jenkins(前身是Hudson),是一个开源(Java语言)的CI(Continuous Integration,持续集成)系统

安装、启动#####

本文以MAC OS X 10.10.4为环境

官方给的安装方式有3种(见 这里 ),我使用的是第二种:
… download jenkins.war directly and launch it by executing java -jar jenkins.war.

  1. 如果不在jenkins.war文件所在目录执行该命令,需带上全路径
  1. 启动时如遇到你机器所装Java版本过低的情况,需从 Java官网 下载适用于你系统的最新版本

启动后会有一系列log输出,待到下面这句出现,就ready了
INFO: Jenkins is fully up and running

访问一下 http://localhost:8080,看能不能打开一个页面。如果能,那就OK了。

安装xUnit插件#####

Jenkins的强大之处之一是可以把各种单元测试框架(JUnit、CppUnit、PHPUnit等)的结果以可视化的方式呈现在页面上。我们先安装xUnit(其中包含了对PHPUnit的支持),以便查看后续单元测试的结果。

首页(也就是http://localhost:8080)-> Manage Jenkins (Page) -> Manage Plugins (Link) -> Available (Tab) -> xUnit (Search Box),如下图:

《Jenkins Tutorial (以PHPUnit为例)》 QQ20151103-0.png

执行 Install without restart之后,坐等完成。完成后页面有提示,shell的日志中也有相应提示:
INFO: Installation successful: xUnit plugin

准备svn repo#####

如果你没有现成的php工程及单元测试,可以先自己弄一个“hello world”级别的 assert(true) “单元测试”:即只建一个测试案例,其中 assert(true),并传到svn中,我的FooTest.php是这样的:

<?php
class FooTest extends PHPUnit_Framework_TestCase
{
    public function test1()
    {
       $this->assertEquals(1, 1); 
    }
    
    /* 如果你觉得一个case太单薄,可以放开此处
    public function test2()
    {
       $this->assertEquals(1, 1); 
       $this->assertEquals(1, 1); 
    }
    */
}

(你需要先 安装PHPUnit)在本地执行 phpunit FooTest.php,可以看到执行结果

创建Job#####

对于一个job的一次执行,一般来说无外乎下面几个步骤:

  • Get code
  • Build
  • Run tests
  • Send notification(e.g. email)
    这里,因php是脚本语言,不需要编译;Send notification一步我们这里也暂略过。故,就只有Get code 和 Run tests两个步骤了

回到你的首页,点击create new jobs,开始创建job流程

  1. 给你的job取个名字(比如Foo),并选择Freestyle project,”OK” 完成
  2. 此时进入job的configure页面。此页有较多的配置项(后续都可以重新编辑),关于触发时机(周期性触发/检查到有更改触发)、svn/git等的配置我们就不赘述了,一是相信你比较熟悉,二是每个配置项旁边有个帮助按钮(”?”),可以帮助你理解。这里仅着重说一下 Build 部分(据我目前的了解,Build+Run tests都是在这里接入)
Add build step

理论上,这里需要加入自己的build+run tests的脚本(取代码的任务已由前面的配置完成,运行时会取到当前目录),因我们是php,无需build,直接run tests:

  • Add build step -> Execute Shell
    输入命令 phpunit –log-junit report.xml FooTest.php。此命令意在执行单元测试,并将测试结果输出到文件report.xml(供后续步骤用)
  • Add build step -> Process xUnit test result report
    Add -> PHPUnit-3.x(default),在Pattern输入框中输入 ./report.xml,即测试结果所在文件

Save,完成。至于更多的 build-step,post-build-actions,暂不添加,我也还未弄明白。现在这两项,已经足以将测试结果通过web页展示出来了。

此时,就可以手动启动一次build了。页面上有好多入口,很容易找。执行完后,具体执行过程可以通过左侧的Console Output点进入看。展示一下我的一次执行具体输出:


Started by user anonymous
Building in workspace /Users/yourname/.jenkins/jobs/Foo/workspace
No JDK named ‘null’ found
Cleaning local Directory .
Checking out svn://localhost at revision '2015-11-03T14:43:47.903 +0800'
A         test.php
A         run.sh
At revision 6
no change for svn://localhost since the previous build
No JDK named ‘null’ found
[workspace] $ /bin/sh -xe /var/folders/rb/tv9pnj2904589kck1z8yb7h00000gn/T/hudson8129901931154079888.sh
+ sh run.sh
PHPUnit 4.8.15 by Sebastian Bergmann and contributors.

..

Time: 88 ms, Memory: 11.75Mb

OK (2 tests, 5 assertions)
[xUnit] [INFO] - Starting to record.
[xUnit] [INFO] - Processing PHPUnit-3.x (default)
No JDK named ‘null’ found
[xUnit] [INFO] - [PHPUnit-3.x (default)] - 1 test report file(s) were found with the pattern 'report.xml' relative to '/Users/yourname/.jenkins/jobs/Foo/workspace' for the testing framework 'PHPUnit-3.x (default)'.
[xUnit] [INFO] - Converting '/Users/yourname/.jenkins/jobs/Foo/workspace/report.xml' .
[xUnit] [INFO] - Check 'Failed Tests' threshold.
[xUnit] [INFO] - Check 'Skipped Tests' threshold.
[xUnit] [INFO] - Setting the build status to SUCCESS
[xUnit] [INFO] - Stopping recording.
Build step 'Process xUnit test result report' changed build result to SUCCESS
Finished: SUCCESS
未解决的一点困惑:######

Jenkins判断build是否成功的标准为:the build is considered a failure if any of the commands exits with a non-zero exit code。 即你在Execute Shell输入框中执行的脚本,如果有返回值非0,即被Jenkins认为是失败的。这自然很好。
但是,在这里运行phpunit时,若有case失败,phpunit将返回非0值,不幸被认为失败,并且最终在页面看不到tests执行结果(成功多少、失败多少),这不是我们预期的。我们预期是:build成功了,tests也执行完了,我要看到执行结果(尽管cases未全部通过)。Jenkins的行为跟我的预期不一样,我还没搞明白。

Jenkins vs. Buildbot#####

Buildbot是python写的CI系统,也进行了简单tutorial。总体感觉是,Jenkins更易上手,直击目标;而Buildbot则有些“抽象”,弄了半天还没明白怎么跑一个Job。引用一个别人的说法:

**Generally I would say that buildbot is the most “general purpose” automatic builds tools. Jenkins however might be the best related to running tests, especially for parsing and presenting results in nice ways (results, details, charts.. some clicks away), things that buildbot does not do “out-of-the-box”. ** from stackoverflow

似乎可以说,Jenkins更像windows,Buildbot更像Linux。

高级:Master/Slave######

Buildbot Tutorial开门见山说的就是自己的Master/Slave架构,可以让一个任务同时在多个平台上跑。Jenkins开始并没着重说这个,我还以为没有。其实也是有的:参考 这里。感谢 这篇文章 给我的提示。此为高级主题,还没深入了解。

    原文作者:NoteCode
    原文地址: https://www.jianshu.com/p/05235277ed70
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞