Wednesday, March 12, 2014

transformation in TSF build

Add a publish folder in solution. Add sub folders under publish folder to hold transformed web.config. Put a blank web.config in the sub folders as a placeholder. Make sure the placeholder web.config is set "Do not copy" and "Content" in the property window and is used to pushed to TFS. The build parameters (TransformDEVConfigFile='true' or 'falsedetermine which transformed will be copied to the root. 

In project file, Add following section. First add build project from 2010.  
<!-- ***** SECTION BELOW ADDED IN ORDER TO USE WEB.CONFIG TRANSFORMS ***** -->
<UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll" />
  <!-- TRANSFORMS WEB.CONFIG FROM WEB.DEV.CONFIG -->
  <Target Name="TransformDEVConfigFile" AfterTargets="AfterBuild" Condition="'$(TransformDEVConfigFile)'=='true'">
    <ItemGroup>
      <DeleteAfterBuild Include="$(WebProjectOutputDir)\Publish\DEV\Web.*.config" />
    </ItemGroup>
    <!-- ProjectConfigTransformDEVFileName IS A MSBUILD PARAMETER IN THE BUILD DEFINITION -->
    <TransformXml Source="Web.config" Transform="$(ProjectConfigTransformDEVFileName)" Destination="$(WebProjectOutputDir)\Publish\DEV\Web.config" />
    <Copy SourceFiles="$(WebProjectOutputDir)\Publish\DEV\Web.config" DestinationFolder="$(WebProjectOutputDir)"></Copy>
    <Delete Files="@(DeleteAfterBuild)" />
  </Target>
  <!-- TRANSFORMS WEB.CONFIG FROM WEB.QA.CONFIG -->
  <Target Name="TransformQAConfigFile" AfterTargets="AfterBuild" Condition="'$(TransformQAConfigFile)'=='true'">
    <ItemGroup>
      <DeleteAfterBuild Include="$(WebProjectOutputDir)\Publish\QA\Web.*.config" />
    </ItemGroup>
    <!-- ProjectConfigTransformQAFileName IS A MSBUILD PARAMETER IN THE BUILD DEFINITION -->
    <TransformXml Source="Web.config" Transform="$(ProjectConfigTransformQAFileName)" Destination="$(WebProjectOutputDir)\Publish\QA\Web.config" />
    <Delete Files="@(DeleteAfterBuild)" />
  </Target>
  <!-- TRANSFORMS WEB.CONFIG FROM WEB.PROD.CONFIG -->
  <Target Name="TransformPRODConfigFile" AfterTargets="AfterBuild" Condition="'$(TransformPRODConfigFile)'=='true'">
    <ItemGroup>
      <DeleteAfterBuild Include="$(WebProjectOutputDir)\Publish\PROD\Web.*.config" />
    </ItemGroup>
    <!-- ProjectConfigTransformPRODFileName IS A MSBUILD PARAMETER IN THE BUILD DEFINITION -->
    <TransformXml Source="Web.config" Transform="$(ProjectConfigTransformPRODFileName)" Destination="$(WebProjectOutputDir)\Publish\PROD\Web.config" />
    <Delete Files="@(DeleteAfterBuild)" />
  </Target>

In Web.config, the connectionStrings section will be replace in the transformation web config  
<connectionStrings> <add name="VentureEntities" connectionString="metadata=res://*/Models.VentureModel.csdl|res://*/Models.VentureModel.ssdl|res://*/Models.VentureModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=yoursqlserver; initial catalog=yourdatabase; integrated security=True;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" /> </connectionStrings>

In Web.DEV.config, the connectionStrings will replace the one in web.config
<connectionStrings xdt:Transform="Replace"> <add name="VentureEntities"     connectionString="metadata=res://*/Models.VentureModel.csdl|res://*/Models.VentureModel.ssdl|res://*/Models.VentureModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=yourDEVsqlserver; initial catalog=yourDEVdatabase; integrated security=True;multipleactiveresultsets=True;application name=EntityFramework&quot;" 
providerName="System.Data.EntityClient" /></connectionStrings>

In Process->Build process paraemters->Advanced
Add /p:TransformDEVConfigFile=true /p:TransformQAConfigFile=true /p:TransformPRODConfigFile=false /p:ProjectConfigTransformDEVFileName="Web.DEV.config" /p:ProjectConfigTransformQAFileName="Web.QA.config" /p:ProjectConfigTransformPRODFileName="Web.PROD.config"







Retention Policy is set default.



No comments:

Post a Comment