CCNET 設定檔 筆記(daily build with MsBuild、Unit Test)

CCNET 設定檔 筆記(daily build with MsBuild、Unit Test)

ccnet (CruiseControl.Net)是一套 利用.net Framework 開發的 OpenSource CI Server

可以應用在 每日自動建置、自動測試、以及佈署等

優點:

  • 靈活(可設定的參數及支援的項目非常多)
  • Web Site 介面可以瀏覽執行狀況及查詢Log
  • Open Source

缺點:

  • 非常複雜的設定檔,幾乎都是以XML格式編輯,無自動化建置設定檔的程式(如果有,再請提供,謝謝)

目前我有應用在進行 daily build、unit test,並且將執行結果寄發Email出來

以下是以我目前使用的設定檔(ccnet.config)內容進行說明,如果有類似需求就可以直接參考寫法

(ccnet.config 可以在安裝完ccnet 目錄上找到,預設環境下是 C:\Program Files (x86)\CruiseControl.NET\server)

 

先就ccnet.config 的結構簡單描述

<crusisecontrol>
    <project>
        ....
    </project>
    <project>
        ....
    </project>
</crusisecontrol>

每一個Project區塊表示一個要在ccnet中執行的Project

所以重要的是 project內容

 

Project 結構

<project>
    <name>專案名稱</name>
    <triggers>
        用來存放觸發執行,常見的有
                    固定間隔時間執行(Interval Trigger)
                    固定指定時間執行(Schedule Trigger)
    </triggers>
    <sourcecontrol>
        版控相關資訊
    </sourcecontrol>
    <tasks>
        依序執行的項目,可以是
                   msbuild、NUnit、批次檔、PowerShell等等
    </tasks>
    <publishers>
        發佈執行結果
    </publishers
</project>

 

現在來看看每一個區塊的範例

Triggers

<triggers>
      <scheduleTrigger time="23:30" buildCondition="ForceBuild">
      </scheduleTrigger>
      <scheduleTrigger time="12:00" buildCondition="ForceBuild">
      </scheduleTrigger>
    </triggers>

表示 每天 12:00  23:30 執行

 

SourceControl

<sourcecontrol type="vss">
      <executable>C:\Program Files (x86)\Microsoft Visual SourceSafe\SS.EXE</executable>
      <project>$/NineYi</project>
      <username>VSS UserName</username>
      <password>Vss Password</password>
      <ssdir>D:\VSS</ssdir>
      <applyLabel>false</applyLabel>
      <alwaysGetLatest>true</alwaysGetLatest>
      <workingDirectory>D:\CCNET\VSS</workingDirectory>
      <culture>en-US</culture>
      <cleanCopy>true</cleanCopy>
      <timeout units="minutes">10</timeout>
    </sourcecontrol>

透過VSS 取得版控檔案

 

tasks

   1: <tasks>
   2:       <msbuild>
   3:         <executable>C:\Windows\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe</executable>
   4:         <workingDirectory>D:\CCNET\VSS</workingDirectory>
   5:         <projectFile>YourSolution(MySolution.sln)</projectFile>
   6:         <logger>C:\Program Files (x86)\CruiseControl.NET\server\ThoughtWorks.CruiseControl.MsBuild.dll</logger>
   7:       </msbuild>
   8:       <exec>
   9:         <executable>DeleteTestResult.bat</executable>
  10:         <baseDirectory>D:\CCNET\VSS</baseDirectory>
  11:       </exec>
  12:       <exec>
  13:         <executable>c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\mstest.exe</executable>
  14:         <baseDirectory>D:\CCNET\VSS</baseDirectory>
  15:         <buildArgs>/testcontainer:MyProduct.Test.dll /resultsfile:TestReport/MyProduct.Test.Results.trx</buildArgs>
  16:       </exec>
  17:     </tasks>

這邊進行三個task

第一個 msbuild ,表示要進行編譯

第二個 exec ,執行了 deleteTestResult.bat  ,裡面的內容是要刪除 unit test 產生的記錄檔,因為預設進行visual studio內建的unit test 時,如果上次執行的記錄檔還存在,會出現Error,無法繼續往下進行

第三個 exec ,執行了mstest.exe,這是 visual studio 內建的unit test 執行程式, 此步驟表示要去對指定的dll進行 unit test

 

deleteTestResult.bat 內容


del TestReport\*.* /q

@echo off 
for /f "delims=" %%a in ('dir /ad /b TestReport') do ( 
  dir /a-d /b /s "\TestReport\%%a" && rd /s/q "\TestReport\%%a" 
) >nul 2>nul

 

publishers

<publishers>
      <merge>
        <files>
          <file>D:\ccnet\VSS\TestReport\MyProduct.Test.Results.trx</file>
        </files>
      </merge>
      <statistics />
      <xmllogger />
      <email from="ccnet@test.com" mailhost="127.0.0.0" includeDetails="TRUE">
        <users>
          <user name="David" group="buildmaster" address="david@test.com"/>
          <user name="Dev" group="developers" address="dev@test.com"/>
        </users>
        <groups>
          <group name="buildmaster">
            <notifications>
              <notificationType>Always</notificationType>
            </notifications>
          </group>
          <group name="developers">
            <notifications>
              <notificationType>Change</notificationType>
              <notificationType>Failed</notificationType>
              <notificationType>Exception</notificationType>
            </notifications>
          </group>
        </groups>
        <xslFiles>
          <file>xsl\header.xsl</file>
          <file>xsl\compile.xsl</file>
          <file>xsl\modifications.xsl</file>
          <file>xsl\compile-msbuild.xsl</file>
          <file>xsl\MsTestReport2008.xsl</file>
          <file>xsl\MsTest9Summary.xsl</file>
        </xslFiles>
      </email>
    </publishers>

merge 區段,表示將unit test的結果檔整合到執行結果中

email 區段,表示將執行結果透過email寄出

email / users 區段,表示有多少個user要寄送, 其中 group 可以將user歸類,並指定寄送的條件

email / group 區段,用來定義 group 、以及那些情境下需要寄送email

email / xslFiles 區段,表示寄送的Email 結構 (這邊不多描述,可以直接參考拿來使用,另外要講的是 xsl\MsTestReport2009.xsl  、 xsl\MsTest9Summart.sxl 是參考 此編文章 取得的,用來呈現unit test結果)

 

 

完整的內容如下

   1: <cruisecontrol xmlns:cb="urn:ccnet.config.builder">
   2:   <project>
   3:     <name>ProductName</name>
   4:     <triggers>
   5:       <scheduleTrigger time="23:30" buildCondition="ForceBuild">
   6:       </scheduleTrigger>
   7:       <scheduleTrigger time="12:00" buildCondition="ForceBuild">
   8:       </scheduleTrigger>
   9:     </triggers>
  10:     <sourcecontrol type="vss">
  11:       <executable>C:\Program Files (x86)\Microsoft Visual SourceSafe\SS.EXE</executable>
  12:       <project>$/NineYi</project>
  13:       <username>VSS UserName</username>
  14:       <password>Vss Password</password>
  15:       <ssdir>D:\VSS</ssdir>
  16:       <applyLabel>false</applyLabel>
  17:       <alwaysGetLatest>true</alwaysGetLatest>
  18:       <workingDirectory>D:\CCNET\VSS</workingDirectory>
  19:       <culture>en-US</culture>
  20:       <cleanCopy>true</cleanCopy>
  21:       <timeout units="minutes">10</timeout>
  22:     </sourcecontrol>
  23:     <tasks>
  24:       <msbuild>
  25:         <executable>C:\Windows\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe</executable>
  26:         <workingDirectory>D:\CCNET\VSS</workingDirectory>
  27:         <projectFile>YourSolution(MySolution.sln)</projectFile>
  28:         <logger>C:\Program Files (x86)\CruiseControl.NET\server\ThoughtWorks.CruiseControl.MsBuild.dll</logger>
  29:       </msbuild>
  30:       <exec>
  31:         <executable>DeleteTestResult.bat</executable>
  32:         <baseDirectory>D:\CCNET\VSS</baseDirectory>
  33:       </exec>
  34:       <exec>
  35:         <executable>c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\mstest.exe</executable>
  36:         <baseDirectory>D:\CCNET\VSS</baseDirectory>
  37:         <buildArgs>/testcontainer:MyProduct.Test.dll /resultsfile:TestReport/MyProduct.Test.Results.trx</buildArgs>
  38:       </exec>
  39:     </tasks>
  40:     <publishers>
  41:       <merge>
  42:         <files>
  43:           <file>D:\ccnet\VSS\TestReport\MyProduct.Test.Results.trx</file>
  44:         </files>
  45:       </merge>
  46:       <statistics />
  47:       <xmllogger />
  48:       <email from="ccnet@test.com" mailhost="127.0.0.0" includeDetails="TRUE">
  49:         <users>
  50:           <user name="David" group="buildmaster" address="david@test.com"/>
  51:           <user name="Dev" group="developers" address="dev@test.com"/>
  52:         </users>
  53:         <groups>
  54:           <group name="buildmaster">
  55:             <notifications>
  56:               <notificationType>Always</notificationType>
  57:             </notifications>
  58:           </group>
  59:           <group name="developers">
  60:             <notifications>
  61:               <notificationType>Change</notificationType>
  62:               <notificationType>Failed</notificationType>
  63:               <notificationType>Exception</notificationType>
  64:             </notifications>
  65:           </group>
  66:         </groups>
  67:         <xslFiles>
  68:           <file>xsl\header.xsl</file>
  69:           <file>xsl\compile.xsl</file>
  70:           <file>xsl\modifications.xsl</file>
  71:           <file>xsl\compile-msbuild.xsl</file>
  72:           <file>xsl\MsTestReport2008.xsl</file>
  73:           <file>xsl\MsTest9Summary.xsl</file>
  74:         </xslFiles>
  75:       </email>
  76:     </publishers>
  77:   </project>
  78: </cruisecontrol>

 

 

在了解整個結構後,會發現其實ccnet的設定檔沒有這麼難,只是因為沒有工具輔助所以使用上學習門檻比較高,以上資料可以給大家參考

 

ps. 好一段時間沒寫blog了

時間不多,沒法介紹更多,花時間排版

不過 希望之後可以繼續提供一些使用上的心得與大家分享

謝謝

 

 

參考資訊

CCNET 官網

CCNET 設定檔參數設定

如何用CruiseControl.Net来进行持续化集成

Integrating Visual Studio Team System 2008 Unit Tests with CruiseControl.NET




 


 

  • 如果您覺得這篇文章有幫助,請您幫忙推薦一下或按上方的""給予支持,非常感激
  • 歡迎轉載,但請註明出處
  • 文章內容多是自己找資料學習到的心得,如有不詳盡或錯誤的地方,請多多指教,謝謝