Environment specific configuration transformation using Octopus Deploy

configuration transformation

Introduction

In this article I will try to describe how to perform web.config transformation based on environment using octopus deploy. App config transformation is similar to web config transformation so I am skipping it in my article.

configuration transformation web.config transformation web.config transform

Problem/ Scenario

We want to deploy a web application using octopus but want to use different connection string in different environment. Consider we have two different environment one is DEV and another is QA. For Dev we will use connection string = “Dev.txt” and for QA we will use connection string = “QA.txt”.

Background

Here we have used two different tool one is TeamCity and another is Octopus. TeamCity is used to build the project and Octopus is used to deploy the project. Octopus is a deployment tool. We will use this tool to transform configuration file. Here we will create OctoPack package to deploy our project.

Creating OctoPack

OctoPack is a nuget package used by Octopus deploy. To deploy our web application we need to first create an OctoPack package. Here I am not going to details about creating Octopack as our focusing point is configuration transformation. In short… to create OctoPack package automatically first we need to add “OctoPack” package to our solution. Then we have to add “Octopus.TeamCity” plugin to our TeamCity. Finally we have to enable “Run OctoPack” from our build definition so that it creates OctoPack package and push it to nuget server.

*** Consideration while creating package and pushing source to source control
As we are going to transform our configuration file; it is important and necessary to add transformation file in source control. We are considering we have following configuration files
web.config
web.Debug.config
web.Release.config

When we are pushing the code in source control ensure web.Debug.config and web.Release.config are in there. If not then you have to do following from your project file. Just open  .csproj and do following 

01

By default they are not added always.

Transformation Requirement

Please consider “Problem/ Scenario” section from this post.

Configuration: Release mode
Environment: DEV Machine, QA Machine

We want to deploy our solution in release mode and that deployment will go to DEV machine and QA machine. Here DEV machine is not my development machine actually it is another machine where developer can do their dev related test in release mode.

We want connection string like following

connectionString="Data Source=local.txt"for local machine where developer code 
connectionString="Data Source=DEV.txt" for DEV environment [in release mode] 
connectionString="Data Source=QA.txt"for QA environment [in release mode]

Transforming configuration file using Octopus deploy

Please consider web.config file which is like following

<connectionStrings>
    <add name="MyDb" connectionString="Data Source=local.txt"/>
</connectionStrings>

Now consider web.Release.config file. Here we have one release configuration file but we want two different configuration value based on two different environment. So we can set this data source value when we go for deployment. But here problem is how we can replace release configuration value? Octopus deploy provides following transformation facilities.

  1. Configuration variable
  1. Substitute variable: Replace any values specified by the #{variable} syntax in any text-based file.
  1. Configuration Transformation : Transforms our XML configuration files for each environment or machine either based on conventions or explicitly

Here we will use Substitute variable and Configuration Transformation feature. Substitute variable is used to replace the configuration value where it is found “#{variable}”.
Let change the web.Release.config file so that we can use same release config to deploy two different environment.

<connectionStrings>
    <add name="MyDb" connectionString="Data Source=#{envSpecificDBPath}"/>
</connectionStrings>

Here #{envSpecificDBPath} will be replace with DEV.txt or QA.txt based on environment. To achieve this replacement we have to do following.

Creating envSpecificDBPath variable

This variable value will be set based on environment. To create variable we need to navigate Library > Variable sets. This will be something like following
02

Configure deployment Process steps
  1. Add Configuration Transforms and Substitute variable in files section from Configure features
    03_01
  2. In Configuration transforms check “CHECK” Automatically run configuration transformation files. It will transform web.config to realease.config.
    03_02
  3. Now we need to specify “Web.Release.config” where substitution will take place like following.This step will replace #{envSpecificDBPath} with environment specific value.
    03_03

Conclusion

Now we are done as we have created deployment process and variable based on environment. Here we just used Configuration transforms and Substitute variables in files feature from octopus deploy to transform configuration file.