-
Notifications
You must be signed in to change notification settings - Fork 56
Tutorial
This page will show you how you can use the Replace Token
task in your deployment.
We have a web application with the following web.config
file:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="mydb" connectionString="Data Source=(localdb)\mssqllocaldb;Initial Catalog=mydb;Integrated Security=True" />
</connectionStrings>
<appSettings>
<add key="environment" value="LOCAL" />
</appSettings>
<system.web>
<compilation debug="true" />
</system.web>
</configuration>
We need to be able to run and deploy this web app with the following constraints:
- We build only one package and deploy to multiple environment (only the connection string and the environment app setting change between environment)
- We need to be able to launch and debug locally without any specific actions
For the first constraint we need to have a tokenized web.config
file so that we can replace at deployment time those token with values for the environment using the Replace Token
task.
The web.config
file must look like this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="mydb" connectionString="Data Source=#{database.server}#;Initial Catalog=#{database.name}#;User ID=#{database.user}#;Password=#{database.password}#" />
</connectionStrings>
<appSettings>
<add key="environment" value="#{environment}#" />
</appSettings>
<system.web>
<compilation />
</system.web>
</configuration>
And we will declare the following variables at the environment scope in the VSTS/TFS release definition:
- database.server
- database.name
- database.user
- database.password (don't forget to make it a secret variable 😄)
- environment
For the second constraint, to have an easy developer and debug experience the web.config
file must be configured with local values so that we can just F5 to launch the debugger and not tokenized as needed for the first constraint.
To resolve this dilemma we must use another tool: XDT transforms. We will use this to replace attributes values with our tokens. This can be done just after the build and before creating our deployment package or at deployment time just before replacing tokens. I would suggest doing it at deployment time so that you can do more advance stuff like different transformations based on the environment.
For our example we can use this web.release.config
file to do the transformation:
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings>
<add name="mydb"
connectionString="Data Source=#{database.server}#;Initial Catalog=#{database.name}#;User ID=#{database.user}#;Password=#{database.password}#" />
xdt:Transform="SetAttributes"
xdt:Locator="Match(name)" />
</connectionStrings>
<appSettings>
<add key="environment"
value="#{environment}#"
xdt:Transform="SetAttributes"
xdt:Locator="Match(key)" />
</appSettings>
<system.web>
<compilation xdt:Transform="RemoveAttributes(debug)" />
</system.web>
</configuration>
This transformation file will:
- replace the connection string
mydb
with a tokenized value - replace the environment app setting with a tokenized value
- remove the debug attribute.
To be able to apply XDT transformations during a build or release in VSTS/TFS you can use my XDT Transform task.
Let's see how this all fit together in a release definition assuming you have the web.config
and web.release.config
files in your build artifact.
- Create a release definition and add your build artifact.
- Create your first environment.
- Add the
XDT Transform
task and configure it to apply the transformation. - Add the
Replace Token
task and configure it to replace tokens in your newly updatedweb.config
. - Add the relevant variables and their value in the environment variables.
- Create your next environment by cloning the previous one and setup the variables.
Note that this can be applied to any XML file.