DD4T: Configuring multiple environments

DD4T, Tridion, Continuous Integration, MSBuild, MSDeploy

When deploying a .NET application on multiple environments you have to deal with multiple configuration settings for each server. This can be fixed by creating multiple packages, publishing from Tridion or creating parameter files.

MSDeploy parameter files

Multiple packages takes too long to finish and there are multiple files you might want to change. That will take multiple transform files.
Publishing from Tridion is not really wanted, because the content porter will mess up the configuration again (or database restore will too).

When using parameter files you can change any xml file in your project in any folder. First you will have to define a parameter definition file. This definition file will tell msdeploy which variables you have, which variables are in which file and define defaults for those variables.

Creating a parameter definition file

Create an xml file in the root of your web project in Visual Studio called parameters.xml. The base of this file will look like this:

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

Defining a parameter

To define a parameter that locates a variable in the web.config check the following example. This example will define the logfilelocation for the rollingfile appender for the log4net config.

<parameter name="LogFileLocation" description="Path where the logfiles are being kept." defaultValue="App_Data/dd4t.log">
    <parameterEntry kind="XmlFile" scope="\\web.config$" match="//configuration/log4net/appender[@name='RollingFile']/file/@value" />
</parameter>

Publishing your project

Once you've created the parameters.xml file and defined your first parameter definition, you can configure your publish profile to publish your website.
Right click your web project in Visual studio and select Publish from the options.
Configure it to your liking (reference: http://msdn.microsoft.com/en-us/library/aa983453(v=vs.90).aspx). Select to publish it to the file system and choose a folder of your liking.

After you've configured the publishing, visit the folder you've chosen to publish to. In this folder you can find a file called Set-Parameters.xml. If you open that file, you can see that in that file are your defined parameters. This file is compatible with MS Deploy and allows you to send this file along with the zip package that can be created with the web publishing functionality too.

I've created two MSBuild files, which will clean, compile and package these files and also grab this parameter file as part of a deployment package. The second MSBuild file will use XML Transformations to alter these set parameter files to allow changes per environment. In a next post I'll show you how to combine this blog post and my previous posts to create a complete continuous integration solution and to enable continuous deployment for all of your DD4T projects.

Setting parameters for different environments

So we now have a package and a parameter file filled with our defaults. The next step is to change the parameter file to reflect one of our environments. First create a directory structure that resembles those environments:

--Root
    -- Configuration
        -- Environments
            -- Development
            -- Test
            -- Acceptance
            -- Production

We need a folder for our development environment. The default values are for our localhost.

We're going to use the same magic as visual studio uses for the web.config files. When generating a deployment package a parameters.xml file is also being generated. This file we'll use as a baseline. For each of the nodes we're deploying to, we'll need to override those parameters.
We'll need a script to generate the correct parameters file for deployment.

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="MergeParameters" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
    <PropertyGroup>
        <Platform Condition="'$(Platform)'==''">Development</Platform>
        <Node Condition="'$(Node)'==''">Staging</Node>
    </PropertyGroup>

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

    <Target Name="MergeParameters">
        <Message Text="Parsing parameter files" Importance="normal" />
        <TransformXml Source="$(MSBuildProjectDirectory)\default-parameters.xml"
            Transform="$(MSBuildProjectDirectory)\$(Platform)\parameters.xml"
            Destination="$(MSBuildProjectDirectory)\parameters.xml"
            StackTrace="true" />
        <TransformXml Source="$(MSBuildProjectDirectory)\parameters.xml"
            Transform="$(MSBuildProjectDirectory)\$(Platform)\parameters.$(Node).xml"
            Destination="$(MSBuildProjectDirectory)\parameters.xml"
            StackTrace="true" />
    </Target>
</Project>

The build script will copy the default-parameters.xml into the Package directory. The overrides will also be copied here. This parameters script will combine the parameter files according to platform and node provided (e.g. Development and Staging).

Tridion Parameters

Tridion uses xml files as well, so these parameters can be used to change those file too. For example:

  • Logfile location in the logback.xml
  • Broker database in the storageconf.xml
  • Session preview & Experience Manager in the storage_conf.xml
  • Caching in the storage_conf.xml
  • License file location in all the tridion xml configuration files at once.
  • etc...

Deployment

The scripts will create all the files needed for deployment. You'll need to install Webdeploy on the target server. After this we can run the msdeploy command to deploy our application:

msdeploy.exe -verb:sync -source:package=MSDeploy-DD4T.Package.Sample.zip -dest:auto,computerName='https://[HOST]:8172/msdeploy.axd?site=[IIS SITE NAME]',userName='[USERNAME]',password='[PASSWORD]',authtype='basic' -disableLink:AppPoolExtension -disableLink:ContentExtension -disableLink:CertificateExtension -allowuntrusted -setParamFile:ParameterFiles\parameters.xml

To deploy a DD4T website, you'll probably have to stop the website before the deployment can go through. This is because the xmogrt.dll will be in use sometimes. This can also be handled by msdeploy. It allows us to stop and start the site.

Stop the site

msdeploy.exe -source:recycleApp -dest:recycleApp='[IIS SITE NAME]',recycleMode='StopAppPool',computerName='https://[HOST]:8172/msdeploy.axd?site=[IIS SITE NAME]',userName='[USERNAME]',password='[PASSWORD]',authtype='basic' -verb:sync -disableLink:AppPoolExtension -disableLink:ContentExtension -disableLink:CertificateExtension -allowuntrusted

Start the site

msdeploy.exe -source:recycleApp -dest:recycleApp='[IIS SITE NAME]',recycleMode='StartAppPool',computerName='https://[HOST]:8172/msdeploy.axd?site=[IIS SITE NAME]',userName='[USERNAME]',password='[PASSWORD]',authtype='basic' -verb:sync -disableLink:AppPoolExtension -disableLink:ContentExtension -disableLink:CertificateExtension -allowuntrusted

Code example

The parameter file for .NET DD4T, with a basic configuration, can be downloaded from my github project.

References