Working with Web Deploy and Release Management


I’ve spent quite a bit of time working with WebDeploy and Release Management lately for some clients.  You should also be aware of the new tasks that were added to the Visual Studio Marketplace recently:

SNAG-3367

IIS Web App Deployment Using WinRM

The extension helps manage the following in IIS:

  • Create a new website or update an existing website.
  • Create a new application pool or update an existing application pool.
  • Deploy a Web Application to the IIS Server using Web Deploy**

The extension makes it so easy to deploy your web application vs. rolling your own PowerShell script.

** It should be noted there are a few caveats to this particular task as to what it will and won’t do.  If you need skip a folder from being deleted or take the app offline then you need to use the msdeploy command (via remote PowerShell).  I’ll show some examples later on where I explicitly define those options on the command-line.

Let’s assume you already have a Web Application you wish to publish and that you have some Application Settings, Connection Strings to set during your publish.  We’ll then configure a build definition and release.

My Sample Web Project

Here is a sample Web Application with a parameters.xml file with some settings that we will change for specific deployments in the release pipeline.

SNAG-3368

Sample Web Application and Parameters.xml

If you want to define a database connection string in this Parameters.xml file you can, by default the database connection strings and IIS Web Application Name are automatically added to your SetParameters.xml file during the packaging process in VS but you are able to override this behaviour if you’d like control over all of the settings.

Here is an example snippet of a connection string from the parameters.xml:

<parameter name="ESB-Web.config Connection String" 
           description="ESB Connection String"
           defaultvalue="__ESB__">
    <parameterentry  scope="\\web.config$" 
                     type="XmlFile" 
                     match="/configuration/connectionStrings/add[@name='ESB']/@connectionString">
    </parameterentry>
  </parameter>

NOTE:  The name attribute of the parameter has -Web.config Connection String appended to the name of your connection string, this is intentional and is required.

You’ll also notice in the above parameters.xml file that all the values specified for the attribute defaultvalue are named as __[name]__ these will be replaced during the release process using a task.

Build Definition

If you create a new Build Definition you can select the Visual Studio build template for this example:

SNAG-3374

Create new build definition – Visual Studio template

The next screen allows you to select your repository source and then click on Create

SNAG-3375

Create new build definition – Repository source

After selecting Create you will edit the following build steps

  • Build solution **\*.sln
  • Copy Files to: $(build.artifactstagingdirectory)
  • Publish Artifact: drop

 

Here are the MSBuild Arguments that are specified in the Visual Studio Build task/step field MSBuild Arguments:

SNAG-3369

Visual Studio Build Task – MSBuild Arguments

/p:DeployOnBuild=true
/p:WebPublishMethod=Package
/p:PackageAsSingleFile=true
/p:SkipInvalidConfigurations=true
/p:PackageLocation=$(build.artifactstagingdirectory) /p:DisableAllVSGeneratedMSDeployParameter=true

Here is the Publish Build Artifact task/step that copies the Web Deploy Pacakage files created by the Visual Studio Build step.

SNAG-3371

Publish Build Artifact

If you created a new build definition your build steps should resemble the following image below.  Note: I deleted the Copy files to… build step from the template in this example, it is not required because in the MSBuild arguments above we’re specfying the PackageLocation as $(Build.ArtififactStagingDirectory) which is used by the Publish Artifact: Web Deploy step to get our files.  I’ve specified the Artifact Name as Web Deploy.

SNAG-3377

New build definition – Web Deploy Package

Save your Build Definition by clicking Save in the toolbar and then select Queue build… to test out the generation of your Web Deploy Artifacts.

If your build is marked as failed because of the Test Assemblies build step, fix your tests or edit the Test Assemblies step Control Options and select Continue on error to have your build marked as Build Partially succeeded.

If you trigger a build you can take a look at the Web Deploy Artifacts that are generated

SNAG-3372

Build – Artifacts Explorer

Here is what the contents of the *.SetParameters.xml file looks like:

SNAG-3373

Contoso.Web.SetParameters.xml

 

Release Definition

If you were able to view the Web Deploy Artifacts in the Artifacts Explorer produced by your build, navigate over to the Release tab by clicking on Release.

SNAG-3379

Click the large green plus sign under the Explorer to create a new release definition

SNAG-3381

Create new release definition

Select Empty and click Next

SNAG-3383

Choose a source that publishes the artifacts to be deployed

Click Create and you will be brought to your release definition

SNAG-3384

New Empty Definition

We need to get an extension from the VisualStudio Marketplace which has a Tokenizer task to replace the tokens in the SetParameters.xml file that is part of our build artifacts.  Here is a screen capture and link to the Release Management Utility tasks extension by Microsoft DevLabs

SNAG-3385

Release Management Utility tasks

If you are using Visual Studio Team Services (VSTS) click Install or Download for TFS 2015 Update 2 or higher to add it to your Team Project Collection.  I’m not walking through those steps but once the tasks are added to your collection you can select the required task (shown below)

SNAG-3388

Release Management | Environment | Add tasks

Add the Tokenize with XPath/Regular expressions to your Release Definition

SNAG-3389

Configure Tokenizer Utility task

 

You will be brought back to your list of Tasks for the environment.  If we check the documentation in the VSMarketplace for this task we know that the task will find the pattern __<pattern>__ and replaces the same with the value from the variable with name<pattern>.  The destination filename and configuration Json filename fields are optional.

You will notice the menu options have changed in my screenshots as I have started using the new interface on VSTS to finish the post.

snag-3492

Tokenizer: Transform Source Filename

We will need to copy our WebDeploy package to the VM and so we need to click Add Tasks and select the Azure File Copy task, if you are doing this on-premises you would select the Windows Machine File Copy task.

snag-3493

Add tasks | Deploy | Azure File Copy

You need to complete several fields for the Azure File Copy task.  For the location I have selected the artifact that was created by the build linked to my release.

snag-3494

Azure File Copy | Source | Select File Or Folder 

In the example above I am targetting Azure VMs, Classic storage account and a Cloud Service.  The Admin login and password are written in the task as variables and they are created under the variables tab.  The destination folder I have entered c:\temp .  You also have a checkbox to select if you want to Clean Target (delete the files in the destination before the copy).  I have chosen Test Certificate so I can use WinRM over HTTPS and Copy in Parallel as I have two machines in an Availability Group behind the Cloud Service.

snag-3495

Azure VMs File Copy

Now we can add the WinRM – IIS Web App Deployment task

snag-3496

Add tasks | Deploy | WinRM – IIS Web App Deployment

If you want to configure IIS before you do a deploy of the webdeploy package you can add the WinRM – IIS Web App Management task to manage the website and Application Pool settings.

Here is the task filled out and we’re ready to roll!  If I am targetting more than one machine I can specify multiple machines comma separated with ports opened for WinRM connections.

snag-3498

Deploy IIS App

If you have any questions please feel free to leave a comment.  I encourge you to check out all the awesome free and paid extensions on the Visual Studio Marketplace to help you extend your VSTS/TFS environment.

About wesmacdonald

Wes MacDonald is a Visual Studio ALM MVP, PSD, MCT and a Principal Consultant for LIKE 10 INC., a SharePoint Server, Visual Studio and Team Foundation Server Consulting Firm providing premium support and guidance services.

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: