Deploy an Azure Web App Container

You can automatically deploy your web app to an Azure Web App for Linux Containers after every successful build.

Before you begin

Based on the desired runtime, import (into Azure DevOps) or fork (into GitHub) the following repository.

If you already have an app in GitHub that you want to deploy, you can try creating a pipeline for that code.

However, if you are a new user, then you might get a better start by using our sample code. In that case, fork this repo in GitHub:

    https://github.com/MicrosoftDocs/pipelines-dotnet-core-docker

Build your app

Follow the Build, test, and push Docker container apps till push an image section to set up the build pipeline. When you're done, you'll have a YAML pipeline to build, test, and push the image to container registry.

We aren't yet advising new users to use YAML pipelines to deploy from Azure DevOps Server 2019. If you're an experienced pipeline user and already have a YAML pipeline to build your .NET Core app, then you might find the examples below useful.

YAML pipelines aren't available on TFS.

Now that the build pipeline is in place, you will learn a few more common configurations to customize the deployment of the Azure Container Web App.

Azure service connection

The Azure WebApp Container 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 Azure Pipelines or Azure DevOps Server to Azure.

You must supply an Azure service connection to the AzureWebAppContainer task. Add the following YAML snippet to your existing azure-pipelines.yaml file. Make sure you add the service connection details in the variables section as shown below-

variables: 
    ## Add this under variables section in the pipeline
    azureSubscription: <Name of the Azure subscription>
    appName: <Name of the Web App>
    containerRegistry: <Name of the Azure container registry>

## Add the below snippet at the end of your pipeline
- task: AzureWebAppContainer@1
  displayName: 'Azure Web App on Container Deploy'
  inputs:
    azureSubscription: $(azureSubscription)
    appName: $(appName)
    containers: $(containerRegistry)/$(imageRepository):$(tag)

YAML pipelines aren't available on TFS.

Configure registry credentials in web app

App Service needs information about your registry and image to pull the private image. In the Azure portal, go to Container settings from the web app and update the Image source, Registry and save.

container-settings

Deploy with Azure Web App for Container

The simplest way to deploy to an Azure Web App Container is to use the Azure Web App for Containers task.

To deploy to an Azure Web App container, add the following snippet at the end of your azure-pipelines.yml file:

trigger:
- master

variables:
  # Container registry service connection established during pipeline creation
  dockerRegistryServiceConnection: <Docker registry service connection>
  imageRepository: <Name of your image repository>
  containerRegistry: <Name of the Azure container registry>
  dockerfilePath: '$(Build.SourcesDirectory)/Dockerfile'
  tag: '$(Build.BuildId)'

  # Agent VM image name
  vmImageName: 'ubuntu-latest'

- task: AzureWebAppContainer@1 # Add this at the end of your file
  inputs:
    azureSubscription: '<Azure service connection>'
    appName: '<Name of the container web app>'
    containers: $(containerRegistry)/$(imageRepository):$(tag)

The snippet assumes that the build steps in your YAML file build and push the docker image to your Azure container registry. The Azure Web App on Container task will pull the appropriate docker image corresponding to the BuildId from the repository specified, and then deploys the image to the Linux App Service.

YAML pipelines aren't available on TFS.

Deploy to a slot

You can configure the Azure Web App container to have multiple slots. Slots allow you to safely deploy your app and test it before making it available to your customers.

The following YAML snippet shows how to deploy to a staging slot, and then swap to a production slot:

- task: AzureWebAppContainer@1
  inputs:
    azureSubscription: '<Azure service connection>'
    appName: '<Name of the web app>'
    containers: $(containerRegistry)/$(imageRepository):$(tag)
    deployToSlotOrASE: true
    resourceGroupName: '<Name of the resource group>'
    slotName: staging

- task: AzureAppServiceManage@0
  inputs:
    azureSubscription: '<Azure service connection>'
    WebAppName: '<name of web app>'
    ResourceGroupName: '<name of resource group>'
    SourceSlot: staging
    SwapWithProduction: true

YAML pipelines aren't available on TFS.