国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > PHP > 正文

基于Jenkins實現(xiàn)php項目的自動化測試 打包和自動部

2020-03-24 18:54:26
字體:
供稿:網(wǎng)友
  • 本篇博文宅鳥將在上篇: 基于Jenkins 搭建持續(xù)集成環(huán)境 的基礎(chǔ)上,繼續(xù)介紹Jenkins結(jié)合php項目實現(xiàn)自動化測試和自動部署。廢話不再多說,直接上干活。

    宅鳥所使用的server為Ubuntu

    要實現(xiàn)在jenkins中實現(xiàn)php的自動化測試,首先需要Jenkins服務(wù)器上安裝php測試框架,php的測試框架很多,在這里我們選擇 PHPUnit Framework.

    PHPUnit的安裝很簡單:

    sudo apt-get install phpunit

    如果出現(xiàn)如下錯誤:

    PHP Warning: require_once(PHP/CodeCoverage/Filter.php): failed to open stream: No such file or directory in /usr/bin/phpunit on line 39PHP Fatal error: require_once(): Failed opening required 'PHP/CodeCoverage/Filter.php' (include_path='.:/usr/share/php:/usr/share/pear') in /usr/bin/phpunit on line 39

    可以通過下面方法安裝:

    sudo pear channel-discover pear.phpunit.desudo pear channel-discover pear.symfony-project.comsudo pear channel-discover components.ez.nosudo pear channel-discover pear.symfony.comsudo pear update-channelssudo pear upgrade-allsudo pear install pear.symfony.com/Yamlsudo pear install --alldeps phpunit/PHPUnitsudo pear install --force --alldeps phpunit/PHPUnit

    安裝后執(zhí)行phpunit --version 返回版本信息。表示安裝成功。

    root@dop-kvm-2:# phpunit --versionPHPUnit 3.7.28 by Sebastian Bergmann.

    下面我們開始給Jenkins一些插件:

    Subversion/Git:用于集成項目版本控制軟件,根據(jù)需要選擇(在上篇博文已安裝使用)

    Phing/Ant:使用Phing或Apache Ant 對PHP項目做自動化構(gòu)建

    CheckStyle:使用PHP CodeSniffer進(jìn)行代碼風(fēng)格檢查的工具。用于檢查PHP代碼是否有違反一組預(yù)先設(shè)置好的編碼標(biāo)準(zhǔn)的一個PEAR包,內(nèi)置了ZEND,PEAR的編碼風(fēng)格規(guī)則

    Clover PHP:使用phpunit進(jìn)行單元測試的工具,可以被xdebug擴展用來生成代碼覆蓋率報告,并且可以與phing集成來自動測試,還可以和Selenium整合來完成大型自動化集成測試

    DRY:使用PHPCPD(php copy paste detector)來發(fā)現(xiàn)項目中的重復(fù)代碼

    HTML Publisher:用來發(fā)布phpunit代碼覆蓋率報告

    JDepend:使用PHP Depend分析php中靜態(tài)代碼,用來檢查項目中的代碼規(guī)模和復(fù)雜程度

    Plot:使用phploc來統(tǒng)計php項目規(guī)模大小的工具,可以統(tǒng)計php的項目代碼行數(shù)

    PMD:使用phpmd(php mess dector),對基于pdepend的結(jié)果進(jìn)行分析,一旦項目超過了pdepend中各具體指標(biāo)的規(guī)定,將發(fā)出警告信息.

    Violations:按照代碼缺陷嚴(yán)重性集中顯示pwd靜態(tài)代碼分析的結(jié)果

    xUnit:使用JUnit的格式來輸出phpunit的日志文件

    注意這些插件是jenkins為php項目所提供的一些插件,但并不是必須的,所以宅鳥只把最值得大家關(guān)注的怎么自動化測試、打包和發(fā)布來給大家講解。

    先給出項目的目錄結(jié)構(gòu):

    root@dop-kvm-2:/home/jenkins/api# tree.├── aa.php├── build.xml├── create.php└── test    ├── DemoTest.php    └── FunctionTest.php1 directory, 5 files

    注意:

    aa.php、create.php是項目的程序文件

    test目錄下的DemoTest.php和FunxtionTest.php是項目的測試文件

    build.xml是jenkins持續(xù)集成測試打包部署的調(diào)用文件

    首先給出項目需要的build.xml文件:

    <?xml version="1.0" encoding="UTF-8"?><project name="api" default="build">        <target name="build" depends="make_runtime,phpcs-ci,phploc,pdepend,phpcb,phpunit,phpdox,phpcpd"/>        <property name="version-m"  html' target='_blank'>value="1.1" />        <property name="version"    value="1.1.0" />        <property name="stability"  value="stable" />        <property name="releasenotes" value="" />        <property name="tarfile"     value="${phing.project.name}.${buildnumber}.${buildid}.tar.gz" />        <property name="pkgfile"     value="${phing.project.name}.${version}.tgz" />        <property name="distfile"    value="dist/${tarfile}" />        <property name="tests.dir" value="test" />        <fileset id="api.tar.gz" dir=".">            <include name="test/**"/>            <include name="*.php"/>            <include name="*.xml"/>        </fileset>        <target name="make_runtime">                <mkdir dir="${project.basedir}/Runtime" />                <mkdir dir="${project.basedir}/build/logs" />                <mkdir dir="${project.basedir}/build/pdepend" />                <mkdir dir="${project.basedir}/build/code-browser" />        </target>        <target name="phpcs" description="Find coding standard violations using PHP_CodeSniffer">                <exec executable="phpcs">                        <arg value="--standard=${project.basedir}/build/phpcs.xml" />                        <arg value="--ignore=autoload.php" />                        <arg path="${project.basedir}/" />                </exec>        </target>        <target name="phpcs-ci" description="Find coding standard violations using PHP_CodeSniffer">                <exec executable="phpcs" output="${project.basedir}/build/build.log">                        <arg value="--report=checkstyle" />                        <arg value="--report-file=${project.basedir}/build/logs/checkstyle.xml" />                        <arg value="--standard=${project.basedir}/build/phpcs.xml" />                        <arg value="--ignore=" />                        <arg path="${project.basedir}/" />                </exec>        </target>        <target name="phploc" description="Measure project size using PHPLOC">                <exec executable="phploc">                        <arg value="--log-csv" />                        <arg value="${project.basedir}/build/logs/phploc.csv"/>                        <arg path="${project.basedir}/"/>                </exec>        </target>        <target name="pdepend" description="Calculate software metrics using PHP_Depend">                <exec executable="pdepend">                        <arg value="--jdepend-xml=${project.basedir}/build/logs/jdepend.xml"/>                        <arg value="--jdepend-chart=${project.basedir}/build/pdepend/dependencies.svg"/>                        <arg value="--overview-pyramid=${project.basedir}/build/pdepend/overview-pyramid.svg"/>                        <arg path="${project.basedir}/"/>                </exec>        </target>                                                                                                                                                                                                              <target name="phpmd" description="Perform project mess detection using PHPMD">                <exec executable="phpmd">                        <arg path="${project.basedir}/"/>                        <arg value="text"/>                        <arg value="${project.basedir}/build/phpmd.xml"/>                </exec>        </target>                                                                <target name="phpmd-ci" description="Perform project mess detection using PHPMD">                <exec executable="phpmd">                        <arg path="${project.basedir}/"/>                        <arg value="xml"/>                        <arg value="${project.basedir}/build/phpmd.xml"/>                        <arg value="--reportfile"/>                        <arg value="${project.basedir}/build/logs/pmd.xml"/>                </exec>        </target>                                                                                  <target name="phpcpd" description="Find duplicate code using PHPCPD">                <exec executable="phpcpd">                        <arg value="--log-pmd"/>                        <arg value="${project.basedir}/build/logs/pmd-cpd.xml"/>                        <arg path="${project.basedir}/"/>                </exec>        </target>        <target name="phpdox" description="Generate API documentation using phpDox">                <exec executable="phpdox"/>        </target>        <target name="phpunit" description="Run unit tests with PHPUnit">                <exec executable="phpunit" />        </target>        <target name="test" description="Run PHPUnit tests">            <phpunit haltonerror="true" haltonfailure="true" printsummary="true">            <batchtest>            <fileset dir="${tests.dir}">                <include name="**/*Test.php" />            </fileset>            </batchtest>            </phpunit>        </target>        <target name="phpcb" description="Aggregate tool output with PHP_CodeBrowser">                <exec executable="phpcb">                        <arg value="--log"/>                        <arg path="${project.basedir}/build/logs"/>                        <arg value="--source"/>                        <arg path="${project.basedir}/"/>                        <arg value="--output"/>                        <arg path="${project.basedir}/build/code-browser"/>                </exec>        </target>        <target name="check" description="Check variables" >            <fail unless="version" message="Version not defined!" />            <fail unless="buildnumber" message="buildnumber not defined!" />            <fail unless="buildid" message="buildid not defined!" />            <delete dir="dist" failonerror="false" />            <mkdir dir="dist" />        </target>                                                                                         <target name="tar" depends="check" description="Create tar file for release">            <echo msg="Creating distribution tar for ${phing.project.name} ${version}"/>            <delete file="${distfile}" failonerror="false"/>            <tar destfile="${distfile}" compression="gzip">                <fileset refid="api.tar.gz"/>            </tar>        </target></project>

    閱讀build.xml后,大家可以了解一下內(nèi)容:

    項目名稱、版本、打后的包名稱:

    <project name="api" default="build">        <target name="build" depends="make_runtime,phpcs-ci,phploc,pdepend,phpcb,phpunit,phpdox,phpcpd"/>        <property name="version-m"  value="1.1" />        <property name="version"    value="1.1.0" />        <property name="stability"  value="stable" />        <property name="releasenotes" value="" />        <property name="tarfile"     value="${phing.project.name}.${buildnumber}.${buildid}.tar.gz" />        <property name="pkgfile"     value="${phing.project.name}.${version}.tgz" />        <property name="distfile"    value="dist/${tarfile}" />        <property name="tests.dir" value="test" />

    打包時包括的文件和文件夾:這里還可以使用exclude排除文件和文件夾:

    <fileset id="api.tar.gz" dir=".">           <include name="test/**"/>           <include name="*.php"/>           <include name="*.xml"/>       </fileset>

    測試文件所在地址:

    <target name="phpunit" description="Run unit tests with PHPUnit">                <exec executable="phpunit" />        </target>        <target name="test" description="Run PHPUnit tests">            <phpunit haltonerror="true" haltonfailure="true" printsummary="true">            <batchtest>            <fileset dir="${tests.dir}">                <include name="**/*Test.php" />            </fileset>            </batchtest>            </phpunit>        </target>

    了解這些后,我們開始在jenkins中新建autoTestTarAndPublish項目,選擇:構(gòu)建一個自由風(fēng)格的軟件項目:

    并且指定好代碼庫:如圖所示

    172124575.jpg

    然后再 增加構(gòu)建步驟->Invoke Phing targets:

    增加兩個 target: test,tar 分別與build.xml中的test,tar名稱相對應(yīng)

    172650946.jpg

    給tar加上參數(shù):

    172622637.jpg

    然后在左邊主菜單: 系統(tǒng)管理->系統(tǒng)設(shè)置->Publish over SSH 下添加主機:(這里宅鳥設(shè)置使用ssh免密碼登陸)需要設(shè)置成從jenkins到要發(fā)布的web服務(wù)器的無密碼登陸

    如圖設(shè)置:

    175130605.jpg

    這里添加設(shè)置的主機名是:134

    接下來我們就可以設(shè)置部署工作了:

    在添加構(gòu)建步驟下來表中選擇:Send files or execute commands over SSh,如果該選項未出現(xiàn)需要在插件管理中安裝插件:Publish Over SSH 然后重啟jenkins即可.

    174009766.jpg

    然后在出現(xiàn)的SSH Publishers中選擇要發(fā)布的主機:

    并填寫打包文件地址,發(fā)布到遠(yuǎn)程server地址信息,并在Exec command文本框中填寫解壓等shell腳本:

    詳情見圖:

    175817788.jpg

    此項設(shè)置完畢后,就可以發(fā)布php項目到134服務(wù)器上了:

    最后文件發(fā)布包的存檔工作:

    增加構(gòu)建后操作步驟:

    180033526.jpg

    填寫dist/*.tar.gz

    180210215.jpg

    至此配置完畢后,點擊 保存 按鈕.我們就可以發(fā)布程序到指定服務(wù)器134上了.

    來看一下發(fā)布結(jié)果:

    回到項目左側(cè)點擊:立即構(gòu)建:可以看到構(gòu)建進(jìn)度條,結(jié)束后可以在控制臺看到輸出結(jié)果:

    180656573.jpg

    我們來到134上看:

    180805428.jpg

    至此發(fā)布完畢.

    此時我們查看一下test/DemoTest.php文件內(nèi)容:

    <?phpclass DemoTest extends PHPUnit_Framework_TestCase {  public function testPass() {      $this->assertTrue(true);    }  public function testFail() {      $this->assertFalse(false);    }}?>

    我們把 testFail()改成下面:

    <?phpclass DemoTest extends PHPUnit_Framework_TestCase {  public function testPass() {      $this->assertTrue(true);    }  public function testFail() {     $this->assertTrue(false);    }}?>

    $this->assertTrue(false);

    這個是錯誤的斷定:

    提交文件后再次構(gòu)建:

    我們可以看到本次構(gòu)建失敗,查看輸出結(jié)果如下:

    181704835.jpg

    當(dāng)把測試用例修改回正確后,執(zhí)行構(gòu)建,發(fā)布正確。

    <?phpclass DemoTest extends PHPUnit_Framework_TestCase {  public function testPass() {      $this->assertTrue(true);    }  public function testFail() {      $this->assertFalse(false);    }}?>

    182504533.jpg

    ok,到此介紹結(jié)束.

    總結(jié)一下:

    jenkins根據(jù)項目根目錄下的build.xml文件,并根據(jù)jenkins中targets的配置,首先自動執(zhí)行test,當(dāng)測試通過后,開始執(zhí)行tar,打包完成后,開始鏈接遠(yuǎn)程webserver把程序包上傳到遠(yuǎn)程webserver指定目錄下,然后再根據(jù)jenkins下的command 執(zhí)行解壓操作,然后就可以根據(jù)自己的業(yè)務(wù)通過shell腳本進(jìn)行自動處理自動發(fā)布的各項操作.

    如果在執(zhí)行test過程中,出現(xiàn)發(fā)現(xiàn)測試用例不通過,則就發(fā)出錯誤報告,終止本次構(gòu)建。

    下一篇博文,將介紹jenkins結(jié)合ansible、shell和mysql版本遷移實現(xiàn)多服務(wù)器批量發(fā)布.敬請關(guān)注

    干貨吐槽完畢,由于時間倉儲,有不足之處歡迎拍磚.

    PHP編程

    鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

  • 發(fā)表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發(fā)表
    主站蜘蛛池模板: 屏东县| 深圳市| 康平县| 四会市| 中宁县| 读书| 独山县| 密山市| 凤阳县| 山西省| 会理县| 馆陶县| 太仓市| 响水县| 鹤岗市| 彭山县| 广汉市| 永兴县| 勃利县| 赞皇县| 体育| 镇宁| 日喀则市| 澄江县| 新蔡县| 桑植县| 富民县| 娄底市| 太仆寺旗| 当雄县| 志丹县| 南投市| 宁城县| 陵川县| 永清县| 青神县| 贵定县| 霍州市| 漳浦县| 玉环县| 鹤山市|