MSDeploy all configuration [.config files] in one

2019-02-10 03:02发布

问题:

We have 4 different environments at the moment (Lab, Test, Stage, LIVE) and we have implemented automatic deployment using Nant/CC.Net. I am investigating and doing some research as to what can be done more efficiently with new MSDeploy tool.

What I want to achieve is to create a package with a Configuration folder inside which we will have all the different configuration files for all the possible environments (basically adding all config transform files)

What I want to achieve is automatic deployment in our enterprise environment where development team hasn't got any access to the server where it is going to be deployed. We just need to hand over the deployment package with predefined instruction to how to install the package.

What's the best approach you can think of. WE ARE NOT USING TFS and don't want the automatic build process to be dependent on any process as such except MSDeploy or something which is easy to replace. Thinking of using MSBuild too.

回答1:

You can achieve it using below solution. In between, I got the guidance from ONE OF THE BEST BOOK I HAVE READ IN LONG LONG TIME. Book is "Inside the Microsoft Build Engine" written by Sayed Ibrahim Hashimi and William Bartholomew. Book goes through the detail in excellent way.

Create a msbuild proj file as shown below

<?xml version="1.0" encoding="utf-8"?>

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0" DefaultTargets="TransformAll">

  <UsingTask TaskName="TransformXml"  AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll"/>

  <PropertyGroup>
    <DestDirectory>C:\Temp\DeployPackage\TRANSFORM_CONFIGS\</DestDirectory>
  </PropertyGroup>

  <ItemGroup>
    <TransformFiles Include="$(FilesToTransform)"/>
  </ItemGroup>

  <Target Name="TransformAll" DependsOnTargets="ValidateSettings">
    <MakeDir Directories="$(DestDirectory)"/>
    <TransformXml Source="..\web.config"
                  Transform="%(TransformFiles.Identity)"
                  Destination="@(TransformFiles->'$(DestDirectory)%(Filename).transformed.config')" />
  </Target>

  <Target Name="ValidateSettings">
    <Error Text="FilesToTransform cannot be empty"
           Condition=" '$(FilesToTransform)'=='' "/>
    <Error Text="Couldn't find transform file at [%(TransformFiles.Fullpath)]"
           Condition =" !Exists('%(TransformFiles.Fullpath)') "/>
  </Target>

</Project>

After adding the above proj file and adding your environment specific files in a folder, simply run the below through msbuild accessible command line

msbuild transform.proj /t:TransformAll /p:FilesToTransform="Lab.config;Test.config;Live.config"

Hope this helps. Don't forget to buy/refer "Inside teh Microsoft Build Engine" book.



回答2:

One possibility would be to customize the installer so the first dialog it displays lets the user choose the environment they are installing to. Based on that decision, the appropriate file would be copied to the install directory and renamed.



回答3:

I am not 100% sure what you are looking for.

Do you want to have a root config file, that is updated from a "master" file containing all the environment specific settings? You should take a look at XmlPreProcessor http://xmlpreprocess.sourceforge.net/

In addition to Pedro's comment, you could also put a registry key on all your servers, stating what environment, the server belongs to. If you do a fully scripted deployment, the package will pick up the environment from that registry key, and produce the correct configuration for that environment. Saves the human step of choosing environment.