由于需要自动化构建集成测试,所以需要一整套环境。
关键 1根据源代码的改动自动下载代码,2编译3发布网站
1第一步只需要通过CruiseControl.NET就可以做到
2编译其实就是在ccnet.config中添加一个任务,关键代码
<tasks> <msbuild> <executable>C:\Windows\Microsoft.NET\Framework\v4.0.30319\Msbuild.exe</executable> <workingDirectory>$(baseDirectory)$(workingDirectory)$(projectName)\src\PollutantMonitorPlatform\PollutantMonitor</workingDirectory> <projectFile>$(baseDirectory)$(workingDirectory)$(projectName)\src\PollutantMonitorPlatform\PollutantMonitor\PollutantMonitor\PollutantMonitor.csproj</projectFile> <buildArgs>/noconsolelogger /p:OutDir=C:\SourceControl\publish\PollutantMonitor\ /v:n</buildArgs> <targets>Clean;Build;_CopyWebApplication</targets> <timeout>900</timeout> <logger>C:\Program Files\CruiseControl.NET\server\ThoughtWorks.CruiseControl.MsBuild.dll</logger> </msbuild> <exec> <executable>AddApp.bat</executable> <baseDirectory>C:\Program Files\CruiseControl.NET\server</baseDirectory> <buildArgs>PollutantMonitor 82 $(baseDirectory)TestWeb\PollutantMonitor "ASP.NET v4.0"</buildArgs> <buildTimeoutSeconds>10</buildTimeoutSeconds> </exec> </tasks> <publishers> <xmllogger/> <buildpublisher> <sourceDir>$(baseDirectory)$(publishDirectory)PollutantMonitor\_PublishedWebsites</sourceDir> <publishDir>C:\SourceControl\Test\</publishDir> <useLabelSubDirectory>false</useLabelSubDirectory> <alwaysPublish>false</alwaysPublish> </buildpublisher> </publishers>
通过应用msbuild.exe命令行就可以编译,不需要vs的开发环境。
网站项目的命令行如下:
C:\Windows\Microsoft.NET\Framework\v3.5\Msbuild.exe F:\download\MSBuildDemo1\MSBuildDemo.sln /t:Clean;Build /p:OutDir=H:\testweb\
含义就是通过3.5版的Msbuild.exe编译路径F:\download\MSBuildDemo1下的solution文件MSBuildDemo.sln,首先清理,再生成,最后把生成的所有文件复制到目录下H:\testweb\
web应用程序项目的命令行如下:
C:\Windows\Microsoft.NET\Framework\v3.5\Msbuild.exe F:\download\MSBuildDemo\BFL.MSBuildDemo.WebApp\BFL.MSBuildDemo.WebApp.csproj /t:Clean;Build;_CopyWebApplication /p:OutDir=H:\testweb\
含义就是通过3.5版的Msbuild.exe编译路径F:\download\MSBuildDemo\BFL.MSBuildDemo.WebApp\下的项目文件BFL.MSBuildDemo.WebApp.csproj ,首先清理,再生成,最后把生成的所有文件复制到目录下H:\testweb\
两种方式都在OutDir下生成了目录_PublishedWebsites,和通过vs右键发布生成的一样。
如何出现如下错误,需要修改csproj项目文件中的如下一行,需要改变版本,例如把v10.0改为v8.0或者v9.0
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" />
C:\Program Files\MSBuild\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets(133,11): error MSB4064: The "Retries" parameter is not supported by the "Copy" task. Verify the parameter exists on the task, and it is a settable public instance property. C:\Program Files\MSBuild\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets(131,5): error MSB4063: The "Copy" task could not be initialized with its input parameters. Done Building Project "F:\download\MSBuildDemo\BFL.MSBuildDemo.WebApp\BFL.MSBuildDemo.WebApp.csproj" (Clean;Build;_CopyWebApplication target(s)) -- FAILED. Build FAILED. "F:\download\MSBuildDemo\BFL.MSBuildDemo.WebApp\BFL.MSBuildDemo.WebApp.csproj" (Clean;Build;_CopyWebApplication target) (1) -> (_CopyWebApplicationLegacy target) -> C:\Program Files\MSBuild\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets(133,11): error MSB4064: The "Retries" parameter is not supported by the "Copy" task. Verify the parameter exists on the task, and it is a settable public instance property. C:\Program Files\MSBuild\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets(131,5): error MSB4063: The "Copy" task could not be initialized with its input parameters. 0 Warning(s) 2 Error(s) Time Elapsed 00:00:00.423发布网站到IIS下
%windir%\system32\inetsrv\appcmd add app /site.name:"Default Web Site" /path:/TestWeb /physicalPath:C:\SourceControl\Test\TestWeb\PollutantMonitor
含义调用appcmd在Default Web Site下添加一个web应用程序,虚拟路径为TestWeb,物理路径为C:\SourceControl\Test\TestWeb\PollutantMonitor
%systemroot%\system32\inetsrv\APPCMD set app "Default Web Site/TestWeb" /applicationPool:"ASP.NET v4.0"
含义调用appcmd设置Default Web Site/TestWeb web应用程序的运行环境为ASP.NET v4.0
%windir%\system32\inetsrv\appcmd delete app "Default Web Site/TestWeb"含义调用appcmd删除iis下的Default Web Site/TestWeb web应用程序
通过一个bat文件自动添加应用程序和设置运行版本,文件内容如下:
重复添加会有错误,目前就是忽略,还没有找到好的方法
if exist %windir%\system32\inetsrv\appcmd.exe (goto AddWeb) else (goto NOappcmd) :AddWeb echo 添加网站 %windir%\system32\inetsrv\appcmd add site /name:%1 /bindings:http/*:%2: /physicalPath:%3 if %errorlevel% equ 0 goto success if %errorlevel% gtr 0 goto failed :failed echo 获得错误返回码为%errorlevel% echo 添加网站failed goto end :success echo 添加网站successed echo 获得返回码为%errorlevel% echo 设置dotnet版本,启动网站 %systemroot%\system32\inetsrv\APPCMD set app %1"/" /applicationPool:%4 %systemroot%\system32\inetsrv\APPCMD start site %1 goto end :NOappcmd echo %windir%\system32\inetsrv\appcmd.exe is not exist :end
需要注意的是以上命令需要有管理员的权限,否则就会出错
参考文章
使用 MSBuild 以特定的 NET Framework 为目标
MSBuild Task Reference
使用MSBuild自动编译发布你的ASP.NET应用程序
重启IIS命令行iisreset
如何使用 Appcmd.exe 命令行工具启用和配置 Internet Information Services 7.0 中的 HTTP 日志记录和其他功能
IIS(IISReset.exe)命令行
Create a Web Site (IIS 7)
Configuring-IIS-7-command-line-Appcmdexe
Getting Started with AppCmd.exe