Azure web app deployment

Note

Build and release pipelines are called definitions in TFS 2018 and in older versions. Service connections are called service endpoints in TFS 2018 and in older versions.

You can automatically deploy your web app to an Azure App Services web app after every successful build. Before you read this topic, you should understand the type of pipeline that you're creating: designer or YAML.

Note

This guidance applies to TFS version 2017.3 and later.

Note

To use YAML you must have the Build YAML definitions preview feature enabled on your account.

Example

If you want some sample code that works with this guidance, import (into VSTS or TFS) or fork (into GitHub) this repo:

https://github.com/adventworks/dotnetcore-sample

Follow the guidance in .NET Core to build the sample code.

After you have a build, create a release pipeline and select the Azure App Service Deployment template for your environment. This automatically adds the necessary tasks. Link the build as an artifact to this release pipeline. Save the pipeline and create a release to see it in action. Then read through the rest of this topic to learn some of the more common changes that people make to customize an Azure web app deployment.

Azure App Service Deploy task

The simplest way to deploy to an Azure web app is to use the Azure App Service Deploy task. This task is automatically added to the release pipeline when you select one of the pre-built deployment templates for Azure app service deployment. Templates exist for apps developed in various programming languages. If you cannot find a template for your language, select the generic Azure App Service Deployment template.

When you link the artifact in your release pipeline to a build that compiles and publishes the web package, it is automatically downloaded and placed into the $(System.ArtifactsDirectory) folder on the agent as part of the release. This is where the task picks up the web package for deployment.

Azure service connection

The Azure App Service Deploy task, similar to other built-in Azure tasks, requires an Azure service connection as an input. The Azure service connection stores the credentials to connect from VSTS or TFS to Azure.

The easiest way to get started with this task is to be signed in as a user that owns both the VSTS account and the Azure subscription. In this case, you won't have to manually create the service connection. Otherwise, to learn how to create an Azure service connection, see Create an Azure service connection.

To learn how to create an Azure service connection, see Create an Azure service connection.

Deploy to a virtual application

By default, your deployment happens to the root application in the Azure web app. If you want to deploy to a specific virtual application, type its name in the Virtual Application property of the Azure App Service Deploy task.

Deploy to a slot

You can configure the Azure web app to have multiple slots. Slots allow you to safely deploy your app and test it before making it available to your customers. Use the option Deploy to Slot in the Azure App Service Deploy task to specify the slot to deploy to. You can swap the slots by using the Azure App Service Manage task.

Deploy to multiple web apps

If you want to deploy to multiple web apps, add environments to your release pipeline. You can control the order of deployment. To learn more, see Environments.

Configuration changes

You may want to apply a specific configuration for your web app target before deploying to it. This is particularly useful when you deploy the same build to multiple web apps in a pipeline. For example, if your Web.config file contains a connection string named connectionString, you can change its value before deploying to each web app. You can do this either by applying a web config transformation or by substituting variables in your Web.config file.

To change the connectionString using variable substitution:

  1. Create a release pipeline with two environments.
  2. Link the artifact of the release to the build that produces the web package.
  3. Define connectionString as a variable in each of the environments. Set the appropriate value.
  4. Select the XML variable substitution option under the File Transforms and Variable Substitution Options of the Azure App Service Deploy task.

Deploying conditionally

You may choose to deploy only certain builds to your Azure web app.

In your release pipeline you can implement various checks and conditions to control the deployment.

  • Set branch filters to configure the continuous deployment trigger on the artifact of the release pipeline.
  • Set pre-deployment approvals as a pre-condition for deployment to an environment.
  • Configure gates as a pre-condition for deployment to an environment.
  • Specify conditions for a task to run.

To learn more, see Release, branch, and environment triggers, Release deployment control using approvals, Release deployment control using gates, and Specify conditions for running a task.

Deploy to a Government cloud or Azure Stack

Do this by creating a suitable service connection:

Deployment mechanisms

The examples above rely on the built-in Azure App Service Deploy task, which provides simplified integration with Azure.

If you use a Windows agent, this task uses Web Deploy technology to interact with the Azure web app. Web Deploy provides several convenient deployment options such as renaming locked files, excluding files from the App_Data folder during deployment, and so on.

If you use the Linux agent, the task relies on the Kudu REST APIs.

The Azure App Service Manage task is another task that's useful for deployment. You can use this task to start, stop, or restart the web app prior to or after deployment. This task can also be used to swap slots, install site extensions, or enable monitoring of the web app.

If the built-in tasks do not meet your needs, you can use other methods to script your deployment. View the YAML snippets in each of the following tasks for some examples: