Auto-Parameter Connection String for Web Deploy Package
Scenario:
The connection string from the Visual Studio's project web.config over rides the connection string set in the web deploy package parameters file upon deployment of the package. Meaning, the connection string defined in the in the web.config is connectionString="Server=ServerA and web deploy package has a parameter value of connectionString="Server=ServerB and the end result of the deployment is connectionString="Server=ServerA appears in the web.config. The ServerA is used for testing by the development team and ServerB is the production server and is the expected value as it is setup in the deployment process for the environment.
It appears the Web Deploy process auto-adds the connection strings to the deployment package and additional steps are required to prevent this from happening during the deployment as it can lead to two connection strings with the same name in the web.config file and also for the production web applications to point to the dev servers.
The resolutions are described in this StackOverflow post and the easiest method to resolve the issue is to change the name of the connection string variable in the Parameters.xml file from name="SQLServerConn" (Original value) to name="SQLServerConn-Web.config Connection String"
This does not require an update to the Visual Studio project file and is the easiest to implement. You can stop reading this blog post if this makes sense. If not, please continue and I will try to explain in as much detail as possible.
The process described below uses the following deployment methodology:
- Web Deploy package is created for deployment and parameters are defined using the Parameters.xml file in the Visual Studio project
- Release Manager uses the tokenization process to replace values in the parameters.xml file with the variables to use for each environment
- Batch file is run on the local IIS server using the Web Deploy command to update the website and uses the values in the SetParameters.xml file to replace the values in the Web Deploy package with the parameters specified in the Release Manager deployment for the environment.
Visual Studio Project Configuration:
The Visual Studio project is called DemoWebFormsApp
Original connection string value in the Web.config file within Visual Studio:
<connectionStrings>
<add name="SQLServerConn" connectionString="Server=SQL2016\InstanceOne;Database=Shredder;User Id=myUsername;Password=myPassword;" />
Added Parameters.xml file and added to Visual Studio project.
Added parameter value to the Parameters.xml file in Visual Studio with the default value of _SQLServerConn_ with the prefix and suffix of _ in order for the tokenization process to find parameter and change value upon the Release Management deployment.
The other important difference is the addition of the -Web.config Connection String value in the parameter name. This prevents the Web Deploy command from appending the default value during the deployment process.
<parameter name="SQLServerConn-Web.config Connection String" description="doesn't matter" defaultvalue="__SQLServerConn__" tags="">
<parameterentry kind="XmlFile" scope="\\web.config$" match="/configuration/connectionStrings/add[@name='SQLServerConn']/@connectionString">
</parameterentry>
</parameter>
Build Definition:
Create the build definition and in the Visual Studio build, add the MSBuild argument /p:DeployOnBuild=true /p:PackageLocation=$(build.artifactstagingdirectory)
OR
/p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation=$(Build.ArtifactStagingDirectory) /p:DeployIisAppPath="Default Web site/FarmToken" /p:DeployOnBuild=true
The MSBuild argument will create the Web Deploy package and generate the Parameters file
Verify the SetParameters.xml file has the correct parameters created and values after the first build.
Release Management Definition:
Download the Colin's ALM Corner Build and Release extension and enable
The release definition will have the following steps:
- Replace tokens in the SetParameters.xml file with the correct values for the environment
- Copy the Web Deploy package to a location on the IIS server, like C:\Deployments\Latest
- Run the Web Deploy package command to update the website files
Replace Token Task:
Within the Replace Token task, select the folder in the Drop folder where the build artifacts are stored:
Change the search pattern to the SetParameters.xml file
The Token Regex pattern is __(\w+)__ in order to find the values in the SetParameter.xml to replace
Copy Files Task:
Set the location of the Web Deploy package on the IIS server. Can either copy to each machine in the farm. Or, copy to gold server and deploy. Test, and then use Web Deploy to copy to the other servers in the farm. This requires an additional batch file for the Web Deploy command.
The target folder is a location on the IIS server where the files are to be copied
Batch File Script to Deploy:
The batch file is created as part of the Web Deploy package creation and this task calls the command with the /Y switch to overwrite files in the web site.
This is the batch file created as part of the Web Deploy package and will have the same name as the web deploy package.
Add the Environment Variables:
For each variable in the Parameters.xml file, add a variable to the environment. The RM tokenizer will replace the value in the Parameters.xml file with the variable value.
Use Release scoped variables in order to reuse the variables for each of the environments:
SQLServerConn is the name of the connection string in the web.config file and the tokenizing task will find _SQLServerConn_ in the SetParameters.xml file and replace it with the value of Server=Server02\InstanceOne;Database=Shredder;User Id=myUsername;Password=myPassword; during the Web Deploy deployment.
Prepare SetParameters.xml:
The DemoWebFormsApp.deploy.cmd will perform the actual web.config updates and other areas of the website at the point of the deployment of the website. The SetParameters.xml file will contain the correct values to apply to the website.
Can use the command file created by the build process, or create own file to use to update the website.
Verify the Replacements in Parameter File was successful:
Check the Replace Tokens task in the logs
SetParameters.xml Final Values
The SetParameters.xml file on the IIS server will have the value from the RM variable.
Update Website w/Web Deploy Command:
The Batch Script executes the
C:\_Agent\LatestDeployments\FarmDemo_Token\LatestPackage\DemoWebFormsApp.deploy
WebSite Web.config Value
Verify the connection string in the web.config contains the correct value:
Fini!