我正在尝试为我们的代码部署创建一个构建脚本到多个环境。代码如下:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<PropertyGroup>
<TargetEnv>Production</TargetEnv>
</PropertyGroup>
<ItemGroup Condition="'$(TargetEnv)' == 'Integration'">
<Server Include="int1">
<ip>172.0.0.1</ip>
</Server>
</ItemGroup>
<ItemGroup Condition="'$(TargetEnv)' == 'Production'">
<Server Include="prod1">
<ip>172.0.2.1</ip>
</Server>
<Server Include="prod2">
<ip>172.0.2.2</ip>
</Server>
</ItemGroup>
<Target Name="Deploy">
<CallTarget Targets="DeployIntegration" />
<CallTarget Targets="DeployServers" />
</Target>
<Target Name="DeployIntegration" Condition="'$(TargetEnv)' == 'Integration'" Outputs="%(Server.Identity)">
<Message Text="= specific int server thing need access to variable %(Server.Identity) =" Importance="high" />
</Target>
<Target Name="DeployServers" Condition="'$(TargetEnv)' != 'Integration'" Outputs="%(Server.Identity)">
<Message Text="= specific prod thing here need access to variable %(Server.Identity) =" Importance="high" />
</Target>
<Target Name="RemoveServerFromLoadBalancer" AfterTargets="DeployServers" Condition="'$(TargetEnv)' != 'Integration'">
<Message Text="= removing %(Server.Identity) from load balancer =" Importance="high" />
</Target>
<Target Name="IgnoreRemoveServerFromLoadBalancer" AfterTargets="DeployServers" Condition="'$(TargetEnv)' == 'Integration'">
<Message Text="= ignore removing %(Server.Identity) from load balancer =" Importance="high" />
</Target>
<Target Name="CopyFilesAndCreateFolderLinks" AfterTargets="RemoveServerFromLoadBalancer;IgnoreRemoveServerFromLoadBalancer">
<Message Text=" = creating and copying files %(Server.Identity) =" Importance="high" />
</Target>
<Target Name="SetWebFarmServerName" AfterTargets="UpdateWebConfig" Condition="'$(TargetEnv)' != 'Integration'">
<Message Text=" = app setting CMSWebFarmServerName set to %(Server.Identity) =" Importance="high" />
</Target>
<Target Name="DisableWebFarmForIntegration" AfterTargets="UpdateWebConfig" Condition="'$(TargetEnv)' == 'Integration'">
<Message Text=" = Disabled webfarm setting for Integration - %(Server.Identity) =" Importance="high" />
</Target>
<Target Name="AddBackToLoadBalancer" AfterTargets="DisableWebFarmForIntegration" Condition="'$(TargetEnv)' != 'Integration'">
<Message Text=" = Putting server %(Server.Identity) back on load balancer =" Importance="high" />
</Target>
</Project>
此代码位于 xml(保存在 11.0 文件夹中)文件中,我使用 msbuild 命令运行它:
C:\Program Files (x86)\Microsoft Visual Studio 11.0>msbuild buildtest.xml /t:Deploy
当我为生产运行构建任务时,此代码将返回此代码:
DeployServers:
= specific prod thing here need access to variable prod1 =
DeployServers:
= specific prod thing here need access to variable prod2 =
RemoveServerFromLoadBalancer:
= removing prod1 from load balancer =
= removing prod2 from load balancer =
CopyFilesAndCreateFolderLinks:
= creating and copying files prod1 =
= creating and copying files prod2 =
我基本上想确保,如果我正在进行集成,它不会运行特定目标,例如负载均衡器相关任务,因为它只有一台机器。我在想,返回的值应该是这样的:
DeployServers:
= specific prod thing here need access to variable prod1 =
RemoveServerFromLoadBalancer:
= removing prod1 from load balancer =
CopyFilesAndCreateFolderLinks:
= creating and copying files prod1 =
DeployServers:
= specific prod thing here need access to variable prod2 =
RemoveServerFromLoadBalancer:
= removing prod2 from load balancer =
CopyFilesAndCreateFolderLinks:
= creating and copying files prod2 =
很抱歉,这篇文章很长,这个 msbuild 的东西有点棘手。感谢您的意见。