Correct way to set connection strings in ci during msbuild / deploy

I have a CI server (Bamboo, but I donโ€™t think it is important) to create and automatically deploy my application. During local development, I use localdb ( <connectionStrings> node in my web.config )

  <add name="MyApp" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDB)\v11.0;Integrated Security=True;Connect Timeout=30;Initial Catalog=MyApp" /> <add name="MyApp_Patients" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDB)\v11.0;Integrated Security=True;Connect Timeout=30;Initial Catalog=AppleHms_MyApp" /> ... 

When deploying, it is obvious that he should not use this. It should use the deployment sql server connection string.

I know that I can write a web.config conversion for this, and I even know that I can encrypt the web.config file, but I'm not sure if the db connection string should get there properly. What makes sense to me is that the CI server should overwrite each connectionString - so the configuration conversion should look something like this.

 <?xml version="1.0" encoding="utf-8"?> <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> <connectionStrings> <add name="MyApp" providerName="System.Data.SqlClient" connectionString="${main-db-connectionstring}" xdt:Transform="SetAttributes" xdt:Locator="Match(name)" /> <add name="MyApp_Patients" providerName="System.Data.SqlClient" connectionString="${patients-db-connectionstring}" xdt:Transform="SetAttributes" xdt:Locator="Match(name)" /> ... </connectionStrings> <system.web> <compilation xdt:Transform="RemoveAttributes(debug)" /> </system.web> </configuration> 

With overrides coming from variables on my ci server (presumably some part of msbuild or deployment)?

Is it right that this is the โ€œrightโ€ way to do it? What exactly do I need to do with msbuild / deploy for this to happen?

+5
source share
2 answers

We also use bamboo for CI and deployment, and what we do:

  • A package project containing the default Parameters.xml file as the msdeploy parameter declaration file.
  • Maintaining SetParameters.xml files for our diverse environement.
  • Packaging projects at night assembly.
  • Release night packs as bamboo artifacts.
  • Using msdeploy to apply parameterization for these packages when deploying for a particular nightly build.

Basically, the deployment with respect to msdeploy is as follows:

 msdeploy -verb:sync -source:package="NightlyPackage.zip" -dest:iisApp="YourIISHost/YourIISSite" -declareParamFile="YourEnvironementSetParameters.xml" 

Your Parameters.xml package for each project will look like this:

 <parameters> <parameter name="ConnectionString1-Web.config Connection String" description="" defaultValue="localhost"> <parameterEntry kind="XmlFile" scope="\\web.config$" match="/configuration/connectionStrings/add[@name='ConnectionString1']/@connectionString" /> </parameter> </parameters> 

Where ConnectionString1 is the name of connectionString.

And your YourEnvironementSetParameters.xml will look like this:

 <parameters> <setParameter name="ConnectionString1-Web.config Connection String" value="Your parametrized connection string value " /> </parameters> 

There are conventions when using parameterization in some fields with webdeploy. Connection strings are interested, therefore, it is recommended that you observe the following parameter names when you need to access web.config connection strings:

 %NameOfYourConnectionStringAsInWebConfig%-Web.config Connection String 
+6
source

Using an XMLPoke task would be the right way to do this through an MSBuild script

XMLPoke on MSDN

Also, to get a really good link, check out the Sayed Ibrahim Hashimi blog , always helpful.

+2
source

All Articles