CruiseControl.NET+MsBuild+MsTest+SVN进行自动构建。
CruiseControl.NET由C#编写,需要.NET Framework运行时的支持(或者Mono),至少需要.NET Framework2.0
使用CruiseControl.NET可以自动化一下工作
从SVN获取代码
编译代码
代码检查
单元测试
发送邮件
等等
CruiseControl.NET下载地址:http://sourceforge.net/projects/ccnet/files/CruiseControl.NET%20Releases/CruiseControl.NET安装
1.CruiseControl.NET需要安装,安装非常简单,点下一步就行。
安装之后会生成一个windows服务(CruiseControl.NET Server 需要手动启动)
在安装目录下会一个webdashboard网站,需要把这个网站部署到IIS中(如果安装的时候没有部署好的话)
在开始菜单中可以打开CCNET.config文件,CruiseControl.NET Server就是根据这个配置文件来进行自动构建的
CCNET.config默认有如下配置项
<triggers> <!-- 定时检查SVN代码是否更改,如果有更改就执行下面的任务--> <intervalTrigger name="continuous" seconds="30" buildCondition="IfModificationExists" initialSeconds="5"/> </triggers>
2.从SVN获取代码(当然CruiseControl.NET还支持其他很多源码管理工具)
首先需要安装SVN客户端(使用SVN在本机进行迁入迁出)
svn客户端必须使用svn.exe,不能使用TortoiseSVN客户端。
svn下载地址:http://sourceforge.net/projects/ccnet/files/latest/download?source=files
在CCNET.Config中进行配置
<!--SVN配置--> <sourcecontrol type="svn" > <trunkUrl>file:///D:/chenb/SVN/Trunk/CCNET2</trunkUrl>
<executable>C:\Program Files\svn-win32-1.8.3\bin\svn.exe</executable>
<workingDirectory>D:\CCNET2\CCNET2</workingDirectory> </sourcecontrol>
trunkUrl是svn的工作库,如果是使用svn服务端,配置就有点不一样
workingDirectory是本机放源码的文件夹
3.配置msbuild,编译代码(当然可以使用其他的,比如VS自带的编译)
安装.NET之后默认安装了msbuild
配置如下:
<!--编译项目--> <msbuild> <executable>C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe</executable> <workingDirectory>D:\CCNET2\CCNET2</workingDirectory> <projectFile>CCNET2.sln</projectFile> <logger>C:\Program Files\CruiseControl.NET\server\ThoughtWorks.CruiseControl.MSBuild.dll</logger> <targets>Build</targets> <timeout>900</timeout> <buildArgs>/p:Configuration=Debug /v:diag</buildArgs> </msbuild>
executable:指定msbuild的路径
workingDirectory:项目的路径
projectFile:项目名称
BuildArgs:编译参数(如Debug、Release)
4.使用Fxcop进行代码检测
安装VS之后,在开始菜单中会看到Fxcop,点击之后会提示安装。
打开Fxcop如下:
可以在My Fxcop Project上右键添加要进行代码检查了程序集。在Rules可以选择要应用的规则。点击保存之后会生成一个.FxCop后缀的文件。
编写一个批处理,来执行Fxcop文件
cd C:\Program Files\Microsoft FxCop 1.36 FxCopCmd /project:D:\CCNET2\1.FxCop /out:D:\CCNET2\DailyBuild.Fxcop.xml /q /gac
CCNET.config中配置如下:
<!--执行代码检查--> <exec executable="D:\CCNET2\fxcop.bat" />
5.使用mstest进行单元测试
CCNET.config配置如下:
<!--执行单元测试--> <exec> <executable>c:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\mstest.exe</executable> <baseDirectory>D:\CCNET2\CCNET2</baseDirectory> <buildArgs>/testcontainer:TestProject\bin\Debug\TestProject.dll /resultsfile:TestResults\mstest-results.xml</buildArgs> <buildTimeoutSeconds>300</buildTimeoutSeconds> </exec>
executable:mstest.exe路径(安装VS自带)
buildArgs:参数,/testcontainer:单元测试项目程序集(可以包含多个) /resultsfile:测试结果文件
6.发送编译、测试结果邮件
CCNET.config配置如下:
<!--发送邮件--> <email mailport="25" includeDetails="true" mailhostUsername="cheng@126.com" mailhostPassword="12345678" useSSL="FALSE"> <from>cheng@126.com</from> <mailhost>smtp.126.com</mailhost> <users> <user name="cheng" group="buildmaster" address="cheng@gmail.com" /> </users> <groups> <group name="developers"> <notifications> <notificationType>Failed</notificationType> <notificationType>Fixed</notificationType> </notifications> </group> <group name="buildmaster"> <notifications> <notificationType>Always</notificationType> </notifications> </group> </groups> <!--<converters> <regexConverter find="$" replace="@gmail.com" /> </converters>--> <xslFiles> <file>xsl\header.xsl</file> <file>xsl\compile.xsl</file> <file>xsl\unittests.xsl</file> <file>xsl\modifications.xsl</file> <file>xsl\MsTestReport2010.xsl</file> <file>xsl\fxcop-summary_1_36.xsl</file> </xslFiles> <!--失败的邮件标题--> <subjectSettings> <subject buildResult="StillBroken" value="Build is still broken for {CCNetProject}" /> </subjectSettings> <attachments> <file>D:\CCNET2\CCNET2\TestResults\mstest-results.xml</file> </attachments> </email> </publishers>
上面配置的是使用126邮箱发送邮件
xslFiles:配置发送哪些测试结果。
subjectSettings:配置但编译成功、失败等,邮件的标题。buildResult有固定的值。
attachments:给邮件添加附件
完整CCNET.config如下:
<cruisecontrol xmlns:cb="urn:ccnet.config.builder"> <!-- This is your CruiseControl.NET Server Configuration file. Add your projects below! --> <!--项目名称、项目描述--> <project name="CCNET2" description="demoproject showing a small config"> <triggers> <!-- 定时检查SVN代码是否更改,如果有更改就执行下面的任务--> <intervalTrigger name="continuous" seconds="30" buildCondition="IfModificationExists" initialSeconds="5"/> </triggers> <!--SVN配置--> <sourcecontrol type="svn" > <trunkUrl>file:///D:/chenb/SVN/Trunk/CCNET2</trunkUrl> <workingDirectory>D:\CCNET2\CCNET2</workingDirectory> </sourcecontrol> <!--任务列表--> <tasks> <!-- 如果想让任务失败,请ping未知的主机 --> <exec> <executable>ping.exe</executable> <buildArgs>localhost</buildArgs> <buildTimeoutSeconds>15</buildTimeoutSeconds> <description>Pinging a server</description> </exec> <!--编译项目--> <msbuild> <executable>C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe</executable> <workingDirectory>D:\CCNET2\CCNET2</workingDirectory> <projectFile>CCNET2.sln</projectFile> <logger>C:\Program Files\CruiseControl.NET\server\ThoughtWorks.CruiseControl.MSBuild.dll</logger> <targets>Build</targets> <timeout>900</timeout> <buildArgs>/p:Configuration=Debug /v:diag</buildArgs> </msbuild> <!--执行代码检查--> <exec executable="D:\CCNET2\fxcop.bat" /> <!--删除单元测试结果文件,否则不会创建新的结果文件--> <exec executable="D:\CCNET2\delmstestresult.bat" /> <!--执行单元测试--> <exec> <executable>c:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\mstest.exe</executable> <baseDirectory>D:\CCNET2\CCNET2</baseDirectory> <buildArgs>/testcontainer:TestProject\bin\Debug\TestProject.dll /resultsfile:TestResults\mstest-results.xml</buildArgs> <buildTimeoutSeconds>300</buildTimeoutSeconds> </exec> <!--合并xml文件--> <merge> <files> <file>D:\CCNET2\*.xml</file> <file>D:\CCNET2\CCNET2\TestResults\mstest-results.xml</file> </files> </merge> </tasks> <publishers> <!--记录日志--> <xmllogger /> <!--保留多少日志文件--> <artifactcleanup cleanUpMethod="KeepLastXBuilds" cleanUpValue="50" /> <!--发送邮件--> <email mailport="25" includeDetails="true" mailhostUsername="cheng@126.com" mailhostPassword="12345678" useSSL="FALSE"> <from>cheng@126.com</from> <mailhost>smtp.126.com</mailhost> <users> <user name="cheng" group="buildmaster" address="cheng@gmail.com" /> </users> <groups> <group name="developers"> <notifications> <notificationType>Failed</notificationType> <notificationType>Fixed</notificationType> </notifications> </group> <group name="buildmaster"> <notifications> <notificationType>Always</notificationType> </notifications> </group> </groups> <!--<converters> <regexConverter find="$" replace="@gmail.com" /> </converters>--> <xslFiles> <file>xsl\header.xsl</file> <file>xsl\compile.xsl</file> <file>xsl\unittests.xsl</file> <file>xsl\modifications.xsl</file> <file>xsl\MsTestReport2010.xsl</file> <file>xsl\fxcop-summary_1_36.xsl</file> </xslFiles> <!--失败的邮件标题--> <subjectSettings> <subject buildResult="StillBroken" value="Build is still broken for {CCNetProject}" /> </subjectSettings> <attachments> <file>D:\CCNET2\CCNET2\TestResults\mstest-results.xml</file> </attachments> </email> </publishers> </project> </cruisecontrol>
delmstestresult.bat内容如下:d: cd d:\CCNET2\CCNET2\TestResults if exist mstest-results.xml (del d:\CCNET2\CCNET2\TestResults\mstest-results.xml)
问题:1、svn: E230001: Server SSL certificate verification failed: certificate issued for a different hostname
解决方法:
运行certmgr.msc,把证书添加到个人证书。
让CruiseControl.NET Server服务使用当前用户运行