CSS

[Continuous Integration]使用CruiseControl.NET實現持續整合

Continuous Integration,CI,中文稱為持續整合
在Martin Flower的網站有著定義如下:
http://martinfowler.com/articles/continuousIntegration.html

Continuous Integration is a software development practice where members of a team integrate their work frequently,usually each person integrates at least daily - leading to multiple integrations per day. Each integration is verified by an automated build (including test) to detect integration errors as quickly as possible.Many teams find that this approach leads to significantly reduced integration problems and allows a team to develop cohesive
software more rapidly.

持續整合是一個軟體開發的實踐,讓開發團隊裡的每個成員頻繁的整合他們的工作,使每天都會有許多次的整合.
每次的整合都都由一台自動建置伺服器來驗證以及執行測試,來盡快的偵測到每次的整合錯誤.
許多團隊發現到這個方法能夠有效的減少整合的問題,並且讓團隊能夠更密切的合作並快速開發軟體.

JosephJ也有一篇很好的介紹文章:
http://www.josephj.com/entry.php?id=251

為什麼要持續整合呢?


每次有人commit code,功能作好了要佈署給使用者或Tester測試,
程式碼要執行測試與分析
要產生程式碼文件等等的時候
我們都是靠人工來執行整合的
每次都要把code checkout下來整合,執行一堆事情
更慘的是往往有人commit上了錯誤的程式碼,或漏掉一些元件
讓其他人update之後程式就不能跑了...然後花上時間去解決這些build problem
然後生命就浪費在這些無意義的事情上面

CI的意義就是利用srcipt的方式把這些工作整合起來
然後放在一個server上執行
只要有固定的script,這個整合步驟的執行頻率就可以提升到最多
可以每次有人commit就執行一次script,然後透過server,將產生的報告顯示在網頁上,或是以email的方式寄給大家,
最後自動將東西佈署到測試伺服器上
或是打包好可以使用,安裝

持續整合的例子 : Google Chrome 的Chromium BuildBot WaterFall
透過buildbot 執行多伺服器的Master-slave的持續整合架構
http://sites.google.com/a/chromium.org/dev/developers/testing/chromium-build-infrastructure/tour-of-the-chromium-buildbot

CruiseControl.NET


CruiseControl.NET是.NET平台一個open source的CI Server,
雖然在設定檔方面比較難以上手,需要用xml調整,
但是卻是適合沒錢和觀望的人嘗試CI的輕量級解決方案
Team City應該是比較好的選擇,只是要花錢
TFS則是很強大,和專案管理完全整合的重量級解決方案,不過要花錢
而且會與source control和其他MS的東西綁死

接下來會說明如何使用CCNET來持續整合一個簡單的專案 :
網路上也有很多說明文章:
http://omaralzabir.com/asp_net_website_continuous_integration_deployment_using_cruisecontrol_net__subversion__msbuild_and_robocopy/
這裡將會使用SVN,msbuild,mstest,msdeploy來作為持續建置的工具

1.安裝CCNet:


從SourceForge下載CCNET:http://sourceforge.net/projects/ccnet/ 並安裝

安裝好了之後在安裝目錄下(C:\Program Files\CruiseControl.NET\server)
會有ccnet.config檔案
這是設定所有持續整合工作的主要設定檔

於ccnet.config加入一個專案與工作:

    
        Hello CCNet
        
            
                c:\projects\Hello.cmd
            
        
    

建立c:\projects\Hello.cmd

echo 'Hello CCNet!!'

然後執行CCNet.exe開始整合
連到http://localhost/ccnet, 可以從DashBoard看到專案建置的狀況,
並可以點選forcebuild來執行工作,從buildlog看到執行訊息


2.設定ccnet.config

config檔的格式說明:http://confluence.public.thoughtworks.org/display/CCNET/Configuring+the+Server

設定專案:


    CITestProject
    c:\projects\CITestProject
    c:\projects\CITestProject\Log

workingDirectory:專案的所在目錄
artifactDirectory:儲存專案建置的Log目錄

之後所有的tag都是放在區塊內

設定Repository:


這裡使用的是SVN,現在也有對應git:

    https://@SVNHost/CITestProject/trunk/
    c:\projects\CITestProject
    BuildMaster
    ********
    36000000
    false
    Always

設定url,使用者名稱與密碼,要checkout的目錄等等,建置伺服器上要安裝svn
tips : 如果有裝Cygwin, commend line的svn 版本可能會是舊版,要先確認版本: svn --version
如果有憑證的話也要先手動接收一次,紀錄憑證訊息

設定trigger:


    

trigger是指在什麼時候執行整合,有分固定時間執行或定期檢查Source有沒有更改兩種Trigger
這裡設定了檢查頻率為60秒,如果發現SVN上有變更就會update並執行整合

設定build task:


    
    
        C:\Windows\Microsoft.NET\Framework\v3.5\MSBuild.exe
        C:\projects\CITestProject
        CITestProject.sln
        Clean;Build
        300
        C:\Program Files\CruiseControl.NET\server\ThoughtWorks.CruiseControl.MsBuild.dll
        

artifactcleanup:清除專案建置的log

msbuild:
這裡使用visual studio的msbuild建置專案,msbuild的位置為
C:\Windows\Microsoft.NET\Framework\v3.5\MSBuild.exe
如使用.Net 4.0則要使用
C:\Windows\Microsoft.NET\Framework\v4.0\MSBuild.exe
並指定專案的.sln定義檔以執行建置

設定publishers: 如何以email或dashboard發佈專案結果


        
        
        
            
        
        
        
            
                Failed
                Fixed
            
        
        

        
            Failed
            Fixed
        

        
            xsl\header.xsl
            xsl\compile.xsl
            xsl\unittests.xsl
            xsl\modifications.xsl
        
    

email:設定寄信的host
users:設定接收email的使用者
groups:設定接受通知的群組與原則:這裡設定只有在build failed與fixed才會收到通知
modifierNotificationTypes: commit的人在什麼狀況下會收到的email訊息:設定為failed與fixed
xslFiles:email中的訊息會由buildlog + .xsl轉換而來,這裡設定email buildreport要包含的訊息

3.Test and Deploy

以下為我用到的其他整合tasks,放在標籤下
設定Test會讓每次的Build收到測試案例的檢核,如果有TestCase失敗,build也會定義為失敗
deploy則是放在最後一個task,讓每個通過testcase檢核的commit會即時deploy到伺服器上

Mstest:

        deleteTestResult.bat
        C:\projects\ESurvey
        
        300


        C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\mstest.exe
        C:\projects\ESurvey
        /testcontainer:CITestProject.Test\bin\Debug\CITestProject.Test.dll 
     /runconfig:LocalTestRun.testrunconfig 
     /resultsfile:testResults.trx
        300



    
       
           testResults.trx
       
    
    


deleteTestResult.bat:
del testResults.trx

mstest必須要在build server安裝Visual Studio才會有
要指定測試專案中的\bin\debug\@TestProjectName.dll 執行
第一個task是為了刪除mstest的記錄檔testResults.trx而執行的script,不然mstest會告訴你因為已經有了log檔而無法測試
一般mstest的記錄檔名稱是自動產生,但這裡因為要將檔案merge到buildReport所以要指定檔名
詳細說明 : http://blogs.blackmarble.co.uk/blogs/bm-bloggers/archive/2006/06/14/5255.aspx

RoboCopy & msdeploy:

在區域網路的其他機器進行WebApp的deploy時,可以使用robocopy與檔案分享來deploy:
 
    robocopy.exe
    C:\Windows\System32
    c:\projects\CITestProject\Web \\@DeployServerName\CITestProject /XD .svn /E /NDL /NC /NP /XO

或是使用msdeploy來佈署: http://www.iis.net/download/webdeploy

    msdeploy.exe
    C:\Program Files\IIS\Microsoft Web Deploy
    -verb:sync -source:contentPath="c:\projects\CITestProject\Web"
-dest:contentPath="C:\project\CITestProject\Web",computername=@DeployServerName,username=administrator,password=******* 
-skip:objectName=dirPath,absolutePath=.*\.svn
-skip:objectName=FilePath,absolutePath=.*\web.config


在確定整合工作沒有問題後,可以進入控制台=>服務 ,設定ccnet成為自動開啟的系統服務
於client端可以安裝CCTray,會於工作列新增一個小圖示,並隨時監控CI Server上的狀況

No comments:

Post a Comment