Azure DevOps CI CD Pipeline using Visual Studio Team Services – Part 2

Azure CI CD Pipeline using DevOps for Visual Studio Team Services
Share this

In the previous article Azure CI/CD Pipeline using DevOps for Visual Studio Team Services, you learned how to create CI build, create CD release and deploy the code to Azure webapp as soon as the code is checked in Visual Studio Team Services (VSTS).

In this article Azure DevOPS CI CD Pipeline using Visual Studio Team Services, you will learn how to apply web.config transformations and variable substitution using parameters.xml file. CI build created in the previous article remains unchanged. Changes need to be done in the CD Release created in the previous article and in Visual Studio solution which I will explain in detail with screenshots in this article.

File Transformation and Variable Substitution:

Azure App Service Deploy task allows users to configure the package based on the environment specified. These tasks use msdeploy.exe, which supports the overriding of values in the web.config file with values from the parameters.xml file.

Configuration substitution is specified in the File Transform and Variable Substitution Options section of the settings for the tasks used in CD Release pipeline which I will cover in detail later in the article. The transformation and substitution options are:

  • XML transformation
  • XML variable substitution
  • JSON variable substitution

When the task runs, it first performs XML transformation, XML variable substitution, and JSON variable substitution on configuration and parameters files. Next, it invokes msdeploy.exe, which uses the parameters.xml file to substitute values in the web.config file.

Step (1): Change Publish Folder structure

MSBuild packages the files to be published in a zip archive. The zip file name is <projectname>.zip where project name is the name of your visual studio project file (.csproj) for the web app. This zip file is created as it was configured in Step (8) of CI Pipeline.

If you unzip this zip file and try to navigate the folder structure to view the actual published content, you will find a long path which in my case looks as below.

\Content\F_C\workingfolder_agent\135\s\Web.UI\Web.UI\obj\Release\Package\PackageTmp

You can choose either not to do anything about this deep nested path or to shorten this published file path. Whatever you choose to do, you need to update the parameters.xml file’s scope element’s value which I will describe in detail further below when I explain about parameters.xml file.

I choose to shorten this path. For this, right click on the .csproj file of you web app to edit it.

You will find the below commented code where Microsoft hints how to modify the build process.

Paste the code provided as follows the commented section.

Save the changes and reload your project file. When the CI build is fired, the artifacts (published files) are dropped to the folder location: \Content\Website. Please note that in the above code, ‘PackagePath’ is specified as ‘WebSite’.

The long path \F_C\workingfolder_agent\135\s\Web.UI\Web.UI\obj\Release\Package\PackageTmp has been replaced by a short path\Website.

Step (2): XML Transformation

XML transformation supports transforming the configuration files (*.config files) by following Web.config Transformation Syntax and is based on the environment to which the web package will be deployed. This option is useful when you want to add, remove or modify configurations for different environments. Transformation will be applied for other configuration files including Console or Windows service application configuration files

Using Visual Studio 2017 and creating an ASP.NET Web App by default created two config transformation environments namely Debug and Release. You can add environment specific config transformation files like web.test.config, web.prod.config.

XML transformation will be run on the *.config file for transformation configuration files named *.Release.config or *.<Environment>.config and will be executed in the following order:

  1. *.Release.config
  2. *.<Environment>.config

For example, if your package contains the following files :-

  • Debug.config
  • Release.config
  • Production.config

and your environment name is Production, the transformation is applied for Web.config with Web.Release.config followed by Web.Production.config.

In this article, I will keep the default web.Debug.config and web.Release.config

In both the Debug and Release config files, I will use two keys with blank values in both the files. The value for the web.config keys will be read from the CD Release Management variables which I will explain in the article further.

Both Web.Debug.config and Web.Release.config files looks as follows :-

Step (3): Add Parameters.xml file

Add a xml file to your Visual Studio solution named parameters.xml. Add a root element named parameters to the file as shown follows.

For each parameter that you want to add, under parameters root element, create a parameter element with name, description, defaultValue and tags attributes, as shown in the follows:

The description of various tags used above are as follows: –

nameProvided the parameter a human readable name like SMTP
defaultValueYou can specify a default value to use if there is none in SetParameters.xml file for it. Leaving it      empty will enforce adding a value to the SetParameters.xml file.
matchThe location of the value to set using the value we provide. Match is based on key’s name defined in the web config transformation files – Web.Debug.config, Web.Release.config eg: SMTP, MailFrom

Step (4): Add variables to CD Release Pipeline

Open the CD Release definition in edit mode and click on Variables tab/section. Click on + Add button to add variables. I have added two variables ‘MailFrom’ and ‘smtp’ as can be seen follows.

CD Release Pipeline Variables

[NOTE]: I have used lower case variable name as ‘smtp’ while I have used upper case as parameter name ‘SMTP’ in parameters.xml file and in Web.Debug.config and Web.Release.config files. The variable name in CD Release Management is case insensitive.

Step (5): Add ‘Copy Files’ Task to CD Release Pipeline

In the previous article, only one task ‘Azure App Service Deploy’ was added to the CD Release pipeline.

Add a new ‘Copy Files’ task to the CD Release pipeline and drag it to the top to make it the first step of the pipeline.

This step has been added to copy the SetParameters.xml file to a new folder named ‘DeployParam’ at the drop location and leave the original SetParameter.xml file at the drop location unchanged. This is needed when there are multiple environments like DEV, TEST, PRE-PROD, PROD etc, SetParameters.xml is changed each time when the release is deploying to an environment.

CD Release Pipeline CopyFiles Task

 

The various steps in this task are described as follows: –

SourceFolderUNC folder path where SetParameters.xml is created by MSBuild at the artifacts drop location
ContentsFully qualified name of SetParameters.xml file which is in the format <ProjectName>.SetParameters.xml where <ProjectName> is the name of your ASP.NET WebApp/ASP.NET MVC/WebAPI project’s name
TargeFolderUNC folder path where SetParameters.xml is to be copied from Source Folder

Step (6): Add ‘Replace Tokens’ Task to CD Release Pipeline

Add a new ‘Replace Tokens’ task to the CD Release pipeline and drag it to the top to make it the second step of the pipeline.

This step replaces the tokenized variables in the Parameters.xml file to create the SetParameters.xml file.

The various steps in this task are described as follows :-

CD Release Pipeline Replace Tokens Task

Root directoryUNC folder path where SetParameters.xml is created by MSBuild at the artifacts drop location
Target filesContents from Step (5) above

CD Release Pipeline Replace Tokens Prefix Suffix

 

Token prefixPrefix double underscore ‘__’ used in parameter.xml file’s defaultValue
Token suffixSuffix double underscore ‘__’ used in parameter.xml file’s defaultValue

Execute CI/CD Pipeline

As explained in my previous article <TODO:Add URL>, queue a new build. Open the drop location to view the files created as shown follows at location:

\\<network shared folder>\SOP\drops\Service Order Portal\SOP\20180605.8\WebPackage

This path is specified in Step (8) of previous article <TODO:Add URL> under File Share Path property as :

\\<network shared folder>\SOP\drops\$(System.TeamProject)\$(Build.DefinitionName)\

$(Build.BuildNumber)\WebPackage

and Artifact name property as WebPackage.

CI Build Artifacts Web Package

Open DeployParam folder. This folder was configured in Step (5) of this article under Copy Files task having Target Folder property. Here you will find the SetParameters.xml file with the tokenized values from Parameter.xml file replaced as shown follows.

CI Build Artifacts SetPatameters.xml

Next, unzip the zip file created by name in the format <solution-name>.zip. The folder contents look as follows.

CI Build Artifacts Web Package Unzipped

Navigate to /Content/Website folder. Here you will see the published artifacts.

CI Build Artifacts Web Package Published Website

After successful completion of the build, a new CD Release definition will be created automatically. Finally, the Azure website contents will be deployed as described in my previous article : Azure CI/CD Pipeline using DevOps for Visual Studio Team Services.

This concludes the article – Azure DevOps CI CD Pipeline using Visual Studio Team Services – Part 2. I hope you liked this article. If you have any comments, questions or suggestions, please post them using the comments section below this article. I will try to respond at my earliest or somebody else reading the article and your comment will try to respond.

Please subscribe to my blog via email to get regular updates on the latest articles and also share this article over social networks like Facebook, Twitter etc. You can use the social sharing buttons and social sharing bar provided to share this on social media.


Share this

Leave a Reply