对于.net方向,做过自动化的,应该没有人不熟悉msbuild吧,非常强大的代码编译工具,.net平台的编译工作都是交给他来完成的,包括.net core的命令,本质上都是调用msbuild来执行的
对于发布到window平台,linux平台,或者用jenkins管理,核心基础还是绕不开msbuild
Msbuild目录结构
msbuild.bat为引导脚本,效果就是让用户选择启动哪个编译脚本,集成到jinkens的话,可以在jinkens里面选择,代码如下:
@echo off echo "欢迎使用东宸科技自动化脚本" echo "1.生成租户Sql脚本" echo "2.初始化数据库Sql脚本" echo "3.生成租户MySql脚本" echo "4.初始化数据库MySql脚本" echo "5.生成QA初始化Sql数据库" echo "6.生成QA Sql租户" echo "--------------------------" echo "51.生成QA工作流服务" echo "52.生成QA工作流管理工具" echo "53.生成QA管理服务" echo "54.生成QA Ng-Alain服务" set /p selected=请选择: IF "%selected%"=="1" Sql-New-Tenant.bat & goto end IF "%selected%"=="2" Sql-New-Database.bat & goto end IF "%selected%"=="3" MySql-New-Tenant.bat & goto end IF "%selected%"=="4" MySql-New-Database.bat & goto end IF "%selected%"=="5" QA-New-Database.bat & goto end IF "%selected%"=="6" QA-New-TenantExe.bat & goto end IF "%selected%"=="51" QA-Workflow-ServiceHost.bat & goto end IF "%selected%"=="52" QA-Workflow-WorkflowManagement.bat & goto end IF "%selected%"=="53" QA-Sprite-SpriteService.bat & goto end IF "%selected%"=="54" QA-Sprite-Portal.bat & goto end echo "自动化脚本已完成!" :end @echo on
00_MsBuild文件夹下其他文件为具体启动编辑脚本,主要是设置不同编译环境的参数以及第一个执行的编译target目标,比如Sql-New-Tenant.bat,代码如下:
@echo off set /p tenantName=请输入英文标识名称: set /p tenantDisplayName=请输入租户中文名称: set /p tenantAdminUserName=请输入租户管理员UserName: set /p yes="确认租户信息【英文标识名称:%tenantName%,租户中文名称:%tenantDisplayName%,租户管理员UserName:%tenantAdminUserName%】,是否继续(y/n):" if not "%yes%"=="y" ( echo 生成租户脚本已取消 goto end ) cd /d %~dp0msbuild if not exist .\output\log\Sql\sql-new-tenant\ mkdir .\output\log\Sql\sql-new-tenant\ cmd /c "build.bat /l:FileLogger,Microsoft.Build.Engine;logfile=.\output\log\Sql\sql-new-tenant\sql-new-tenant.log /target:Sql-New-Tenant /property:TenancyName=%tenantName% /property:TenancyDisplayName=%tenantDisplayName% /property:AdminUserName=%tenantAdminUserName%" @if %errorlevel% NEQ 0 (pause) else (if "%1%" NEQ "--batch" pause) :end @echo on
msbuild文件夹里面的内容,就是正真的编译逻辑。
build.bat为msbuild执行路径
My.proj为编译总架构,定义了一系列参数,包括输入输出目录、模版路径、引用的编译依赖的第三方库dll路径,还有就是加载所有项目的target,以及定义公共的一些帮助task,代码如下:
"1.0" encoding="utf-8" ?>"Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0"> "Echo"> "Hello from $(MSBuildProjectDirectory)" /> $(MSBuildProjectDirectory)\..\.. $(MSBuildProjectDirectory)\output\$(Environment) "'$(Configuration)' == ''">Release "'$(Platform)' == ''">Any CPU "'$(Platform)' == 'MCD'">Any CPU "'$(Environment)' == ''">DEV "Exists('$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v12.0\Web\Microsoft.Web.Publishing.Tasks.dll')">12.0 "Exists('$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v14.0\Web\Microsoft.Web.Publishing.Tasks.dll')">14.0 $(MSBuildProjectDirectory)\lib\ $(MSBuildProjectDirectory)\template\ echo 1 "Workflow-ServiceHost.targets" /> "Workflow-WorkflowManagement.targets" /> "Sprite-SpriteService.targets" /> "Sprite-Portal.targets" /> "Sql-New-Tenant.targets" /> "Sql-New-Database.targets" /> "Sql-New-DatabaseExe.targets" /> "Sql-New-TenantExe.targets" /> "MySql-New-Tenant.targets" /> "MySql-New-Database.targets" /> "TransformXml" AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Web\Microsoft.Web.Publishing.Tasks.dll" /> "Build-Web"> "Start building everything" /> "VisualStudioVersion:$(VisualStudioVersion)"/> "MSBuildReplaceFile" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll"> "System.String" Required="true" /> "System.String" Required="true" /> "System.String" Required="true" /> "System.String" Required="true" /> "System.Core" /> "System" /> "System.IO" /> "System.Net" /> "System.Linq" /> "Microsoft.Build.Framework" /> "Microsoft.Build.Utilities" /> "Fragment" Language="cs"> <![CDATA[ string strContent = File.ReadAllText(TemplateFileFullName, System.Text.Encoding.UTF8); var strReplaceTags = ReplaceTags.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); var strReplaceValues = ReplaceValues.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); for(int i=0;i< strReplaceTags.Length;i++) { strContent = strContent.Replace(strReplaceTags[i], strReplaceValues[i]); } File.WriteAllText(DestinationFileFullName, strContent, System.Text.Encoding.UTF8); return true; ]]>
新增租户编译脚本Sql-New-Tenant.targets和MySql-New-Tenant.targets,这个稍微简单一些,输入租户名称、租户显示名称、租户超级管理员名称,脚本读取模版文件,替换模版文件里面定义的内容标识,输出sql或者mysql脚本,脚本如下:
"1.0" encoding="utf-8" ?>"http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0"> "MySql-New-Tenant" DependsOnTargets="Build-Web"> $(ReleaseFolder)\mysql\mysql-new-tenant "$(ReleaseWebHost)" /> "$(ReleaseWebHost)" /> "#TenancyName#;#Name#;#AdminUser#" ReplaceValues="$(TenancyName);$(TenancyDisplayName);$(AdminUserName)" DestinationFileFullName="$(ReleaseWebHost)\sql-new-tenant_mysql.sql" TemplateFileFullName="$(TemplatesPath)\mysql-new-tenant.sql.template">
初始化数据库脚本Sql-New-Tenant.targets和MySql-New-Database.targets,主要用.net frameworkcore的migrations实现dotnet ef migrations script,代码如下:
"1.0" encoding="utf-8" ?>"http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0"> "MySql-New-Database" DependsOnTargets="Build-Web"> $(RootFolder)\01_Framework\CK.Sprite.MigrationToolMysql $(RootFolder)\03_Workflow\CK.Workflow.MigrationToolMysql $(ReleaseFolder)\mysql\mysql-new-database "$(ReleaseWebHost)" /> "$(ReleaseWebHost)" /> dotnet ef migrations script --output "$(ReleaseWebHost)\authcenterscript_mysql.sql" -c AuthCenterDbContext dotnet ef migrations script --output "$(ReleaseWebHost)\commonscript_mysql.sql" -c CommonDbContext dotnet ef migrations script --output "$(ReleaseWebHost)\oaservicescript_mysql.sql" -c OAServiceDbContext dotnet ef migrations script --output "$(ReleaseWebHost)\workflow_mysql.sql" -c WorkflowDbContext "$(SpriteProjectRoot)" Command="$(CmdSprite)" StdOutEncoding="UTF-8" StdErrEncoding="UTF-8" /> "$(WorkflowProjectRoot)" Command="$(CmdWorkflow)" StdOutEncoding="UTF-8" StdErrEncoding="UTF-8" />
流程引擎服务编译Workflow-ServiceHost.targets,核心思想:msbuild编译项目,根据不同的环境变量,替换默认配置文件内容,删除其他配置文件,配置文件严格按照标准格式来定义,如Web.config、Web.QA.config,如果编译传递的是QA环境变量,则用Web.QA.config内容替换Web.config指定内容,代码如下:
"1.0" encoding="utf-8" ?>"http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0"> "Workflow-ServiceHost-BuildSolution" DependsOnTargets="Build-Web"> $(RootFolder)\03_Workflow\CK.Workflow.ServiceHost $(WebProjectRoot)\CK.Workflow.ServiceHost.csproj $(ReleaseFolder)\workflow-servicehost workflow-servicehost.zip "$(ReleaseWebHost)" /> "$(ReleaseWebHost)" /> "$(WebProjectPathOfWebHost)" Targets="ResolveReferences;Compile;_CopyWebApplication" Properties=" Configuration=$(Configuration); WebProjectOutputDir=$(ReleaseWebHost); OutDir=$(ReleaseWebHost)\bin" /> "Exists('$(WebProjectRoot)\Configuration\AppSettings\App.$(Environment).config')" Source="$(WebProjectRoot)\Configuration\AppSettings\App.config" Transform="$(WebProjectRoot)\Configuration\AppSettings\App.$(Environment).config" Destination="$(ReleaseWebHost)\Configuration\AppSettings\App.config" /> "Exists('$(WebProjectRoot)\log4net.$(Environment).config')" Source="$(WebProjectRoot)\log4net.config" Transform="$(WebProjectRoot)\log4net.$(Environment).config" Destination="$(ReleaseWebHost)\log4net.config" /> "Exists('$(WebProjectRoot)\Configuration\Models\WorkflowForm.$(Environment).xml')" Source="$(WebProjectRoot)\Configuration\Models\WorkflowForm.xml" Transform="$(WebProjectRoot)\Configuration\Models\WorkflowForm.$(Environment).xml" Destination="$(ReleaseWebHost)\Configuration\Models\WorkflowForm.xml" /> "Exists('$(WebProjectRoot)\Configuration\XmlConfig\CoreConfiguration.$(Environment).xml')" Source="$(WebProjectRoot)\Configuration\XmlConfig\CoreConfiguration.xml" Transform="$(WebProjectRoot)\Configuration\XmlConfig\CoreConfiguration.$(Environment).xml" Destination="$(ReleaseWebHost)\Configuration\XmlConfig\CoreConfiguration.xml" /> "Exists('$(WebProjectRoot)\Configuration\XmlConfig\SocketClientConfiguration.$(Environment).xml')" Source="$(WebProjectRoot)\Configuration\XmlConfig\SocketClientConfiguration.xml" Transform="$(WebProjectRoot)\Configuration\XmlConfig\SocketClientConfiguration.$(Environment).xml" Destination="$(ReleaseWebHost)\Configuration\XmlConfig\SocketClientConfiguration.xml" /> "Exists('$(WebProjectRoot)\Configuration\XmlConfig\SocketServiceConfiguration.$(Environment).xml')" Source="$(WebProjectRoot)\Configuration\XmlConfig\SocketServiceConfiguration.xml" Transform="$(WebProjectRoot)\Configuration\XmlConfig\SocketServiceConfiguration.$(Environment).xml" Destination="$(ReleaseWebHost)\Configuration\XmlConfig\SocketServiceConfiguration.xml" /> "Workflow-ServiceHost" DependsOnTargets="Workflow-ServiceHost-BuildSolution"> "$(ReleaseWebHost)\Configuration\AppSettings\App.QA.config;"/> "$(ReleaseWebHost)\log4net.QA.config;"/> "$(ReleaseWebHost)\Configuration\Models\WorkflowForm.QA.xml;"/> "$(ReleaseWebHost)\Configuration\XmlConfig\CoreConfiguration.QA.xml;"/> "$(ReleaseWebHost)\Configuration\XmlConfig\SocketClientConfiguration.QA.xml;"/> "$(ReleaseWebHost)\Configuration\XmlConfig\SocketServiceConfiguration.QA.xml;"/> "$(ReleaseWebHost)\Configuration\AppSettings\App.PRD.config;"/> "$(ReleaseWebHost)\log4net.PRD.config;"/> "$(ReleaseWebHost)\Configuration\Models\WorkflowForm.PRD.xml;"/> "$(ReleaseWebHost)\Configuration\XmlConfig\CoreConfiguration.PRD.xml;"/> "$(ReleaseWebHost)\Configuration\XmlConfig\SocketClientConfiguration.PRD.xml;"/> "$(ReleaseWebHost)\Configuration\XmlConfig\SocketServiceConfiguration.PRD.xml;"/>
其他项目的编译也是参照这样的方式实现
补充说明:对于发布到IIS,可以通过webdeploy实现,也是用脚本,把编译后的代码推送到IIS服务器,对于linux可以推送到docker容器,还有其他方式,比如把代码发送到git服务器,调用客户端接口,让客户端从git上拉取代码到本地等等。