Deploy ASP.NET apps to Azure virtual machines

Last Update: 3/6/2017

Team Services | TFS 2017 | TFS 2015 | Previous version

This example is one of a growing collection that demonstrate using Release Management in Visual Studio Team Services and Team Foundation Server to deploy and manage your apps.

This example shows how you can deploy a new ASP.NET MVC app from Visual Studio Team Services or Microsoft Team Foundation Server to an Azure virtual machine.

Prepare

Follow these steps to establish a connection from Team Foundation Server or Visual Studio Team Services to Azure.

You need an Azure subscription to perform these steps.

  1. Open your Team Services or TFS team project in a web browser. Choose the Settings icon in the menu bar and select Services.

  2. In the Services tab, choose New Service Endpoint and select Azure Resource Manager.

  3. Enter a user-friendly name for the connection such as Fabrikam and select your Azure subscription.

    If the subscription you require is not shown in the service endpoint dialog, see Azure Resource Manager service endpoint for more information and Troubleshoot Azure Resource Manager service endpoints.

    Or, if you prefer to use an existing service principal, use the link near the bottom to open the full version of the dialog and follow these steps:

    • Download and run this PowerShell script in an Azure PowerShell window.
    • Enter a user-friendly name for the connection such as Fabrikam.
    • Copy these fields from the output of the PowerShell script into the Azure subscription dialog textboxes:
      • Subscription ID
      • Subscription Name
      • Service Principal ID
      • Service Principal Key
      • Tenant ID

  4. Choose OK to save the Azure service endpoint.

Follow these steps to create a new virtual machine in Azure.

  1. Sign into the Azure management portal and choose the +New icon in the left panel, then choose Compute. Select Windows Server 2012 R2 Datacenter rom the list.

  2. At the bottom of the Windows Server 2012 R2 Datacenter blade, in the Select a deployment model list, choose Resource Manager and then choose Create.

  3. In the Create virtual machine blade, under Basics, enter the name of the machine, such as helloworld-vm, username, password, and the name of the Resource Group, such as helloworld-vm. Leave all the other settings at their default values and choose Create.

    Username formats such as username, domain\username, machine-name\username, and .\username are supported. UPN formats such as username@domain.com and built-in system accounts such as NT Authority\System are not supported.

Code

Carry out the following steps to create a simple .NET MVC app to deploy.

  1. Create a new ASP.NET Web Application project in Visual Studio, targeting .NET Framework 4.5.

  2. Choose the MVC project type and leave the other settings at their defaults.

  3. Save the solution and commit it to your chosen repository.

Add code to deploy the web app into a virtual machine by creating a new folder called Deploy in your solution, and copying the following script as a file named ConfigureWebserver.ps1 into the Deploy folder.

    Configuration Main
    {
      Node ('localhost')
      {
        WindowsFeature WebServerRole
        {
          Name = "Web-Server"
          Ensure = "Present"
        }

        WindowsFeature WebAspNet45
        {
          Name = "Web-Asp-Net45"
          Ensure = "Present"
          Source = $Source
          DependsOn = "[WindowsFeature]WebServerRole"
        }

        #script block to download WebPI MSI from the Azure storage blob
        Script DownloadWebPIImage
        {
          GetScript = {
            @{
              Result = "WebPIInstall"
            }
          }

          TestScript = {
            Test-Path "C:\temp\wpilauncher.exe"
          }

          SetScript ={
            $source = "http://go.microsoft.com/fwlink/?LinkId=255386"
            $destination = "C:\temp\wpilauncher.exe"
            Invoke-WebRequest $source -OutFile $destination
          }
        }

        Package WebPi_Installation
            {
          Ensure = "Present"
                Name = "Microsoft Web Platform Installer 5.0"
                Path = "C:\temp\wpilauncher.exe"
                ProductId = '4D84C195-86F0-4B34-8FDE-4A17EB41306A'
                Arguments = ''
          DependsOn = @("[Script]DownloadWebPIImage")
            }

        Package WebDeploy_Installation
            {
                Ensure = "Present"
                Name = "Microsoft Web Deploy 3.5"
                Path = "$env:ProgramFiles\Microsoft\Web Platform Installer\WebPiCmd-x64.exe"
                ProductId = ''
          Arguments = "/install /products:ASPNET45,ASPNET_REGIIS_NET4,WDeploy  /AcceptEula"
          DependsOn = @("[Package]WebPi_Installation")
            }

        Script DeployWebPackage
        {
          DependsOn = @("[Package]WebDeploy_Installation")
          GetScript = {
            @{
              Result = ""
            }
          }

          TestScript = {
            $false
          }

          SetScript = {
            $MSDeployPath = (Get-ChildItem "HKLM:\SOFTWARE\Microsoft\IIS Extensions\MSDeploy" | Select -Last 1).GetValue("InstallPath") + "msdeploy.exe"
                    cmd.exe /C $("`"{0}`" -verb:sync -source:package={1} -dest:auto,ComputerName=localhost 2> C:\temp\err.log" -f $MSDeployPath, "C:\temp\your-project-name.zip")
          }
        }
      }
    }

    Main

Note: In the line starting SetScript at the end of the script, change your-project-name in the parameter C:\temp\your-project-name.zip to the name of your Visual Studio project.

Build

Carry out the following steps to build the sample web application.

  1. Open the Builds tab of the Build & Release hub and choose + New to create a new build definition.

  2. In the Deployment tab of the Create new build definition dialog, select Azure WebApp and choose Next. This template is used to get started; you will remove the default Azure Web App Deployment task later.

  3. Select the repository where you committed your app and choose Create.

  4. In the new build definition, remove the Azure Web App Deployment task step by selecting the X next to the task.

  5. Select the NuGet restore task and use the ellipses (...) in the Path to solution... parameter to navigate to and select the solution file from your build.

  6. Edit the parameters of the Publish Artifact task to publish the deployment script as an artifact. Set the following values:

    • Path to Publish: Path to your build artifacts, such as $(build.artifactstagingdirectory).
    • Artifact Name: deploy
    • Artifact Type: Server

  7. Save the build definition.

  8. Queue a new build and verify that the artifact containing the zip file and the website is correctly published.

Deploy

Carry out the following steps to deploy the app to an Azure virtual machine.

  1. Open the Releases tab of the Build & Release hub and choose the plus icon to create a new release definition.

  2. In the Create release definition dialog, select the Empty template and choose Next.

  3. In the next page, select the build definition you created earlier and choose Create. This creates a new release definition with one default environment.

  4. In the new release definition, choose Link artifact source and link to your source repository.

  5. Select plus-icon Add tasks and add an Azure File Copy task. Configure the task as follows:

    • Select the ConfigureWebserver.ps1 script in the Source field.
    • Change the Azure Connection Type to Azure Resource Manager.
    • Select the name of the Azure RM Subscription endpoint you defined earlier, such as Fabrikam.
    • Set the Destination Type to Azure VMs.
    • Select a Storage Account from the Azure subscription to be used as a staging location for the files.
    • Select the name of Resource Group that you created earlier, such as helloworld-vm.
    • Provide the administrator credentials for the virtual machine. Note the use of ".\" in the Admin Login parameter.
    • Specify the path to a Destination Folder on the target servers for the script.
    • Select the Enable Copy Pre-requisites checkbox.

  6. Select plus-icon Add tasks and add an Azure Resource Group Deployment task. Configure the task as follows:

    • Select the name of the Azure Subscription endpoint you defined earlier, such as Fabrikam.
    • Set the Action to Select Resource Group.
    • Select the Resource Group that you created earlier, such as helloworld-vm.
    • Select the Enable Deployment Pre-requisites checkbox.
    • In the Output section set the Resource Group value so that it can be used as input to further deployment tasks.

  7. Select plus-icon Add tasks and add an Azure File Copy task. Configure the task as follows:

    • Select all the files to copy to the VM in the Source field.
    • Select the name of the Azure RM Subscription endpoint you defined earlier, such as Fabrikam.
    • Set the Destination Type to Azure VMs.
    • Select a Storage Account from the Azure subscription to be used as a staging location for the files.
    • Select the name of Resource Group that you created earlier, such as helloworld-vm.
    • Provide the administrator credentials for the virtual machine. Note the use of ".\" in the Admin Login parameter.
    • Specify the path to a Destination Folder on the target servers for the script.
    • Select the Enable Copy Pre-requisites checkbox.

  8. Select plus-icon Add tasks and add a Run PowerShell on Target Servers task. Configure the task as follows:

    • For Machines, select the Output Resource Group name you specified earlier for the Azure Resource Group task. This will ensure that the output of that task is passed as input to this task.
    • Provide the administrator credentials for the virtual machine. Note the use of ".\" in the Admin Login parameter.
    • Ensure that Protocol is set to HTTPS.
    • Check (set) the Test Certificate option so that a certificate that does not come from a known certificate authority is accepted.
    • For the PowerShell Script, enter the location of the ConfigureWebserver.ps1 script on the target server (after it has been copied by the Azure File Copy task).

  9. Enter a name for the release definition, and optionally change the name of the environment from Default Environment to Dev.

  10. Save the new release definition.

  11. Create a new release, select the latest build, and deploy it to the single environment in the definition.

Go back Examples index

Help and support

Send suggestions on UserVoice, follow us on Twitter @vsreleasemgmt

or send a smile from the Send-a-Smile link on the menu bar.