Use Visual Studio to automatically generate a CI/CD pipeline to deploy an ASP.NET Core web app with Docker to Azure

TFS 2018 - TFS 2015

ASP.NET Core is a lean and composable framework for building web and cloud applications, which can be deployed to Docker containers running on an Azure Container Service. Visual Studio 2017 RC.3 and newer with the Continuous Delivery Tools for Visual Studio extension provide an easy way to set up a full continuous integration (CI) and continuous deployment (CD) pipeline for such applications.

A typical release pipeline for web applications

Continuous integration means starting an automated build (and possibly running tests) whenever new code is committed to or checked into the project's source control repository. This gives you immediate feedback that the code builds and can potentially be deployed. Continuous delivery (or deployment) means starting an automated deployment pipeline whenever a new successful build is available. Together, CI and CD mean that any code changes you commit to your repository are quickly validated and deployed to a live web site without any manual intervention.

Creating the release pipeline from Visual Studio

  1. Make sure you have a Docker-enabled ASP.NET Core solution. See the FAQ below if you don't have one yet.
  2. If you haven't already done so, add the project to a source control repository in Azure Repos by clicking first on Add to Source Control in the Visual Studio status bar (creating a local repository), then following the instructions in the Team Explorer pane to publish to a remote repository in Azure Repos.
  3. Select the Tools > Extensions and Updates... command, then search for and install the "Continuous Delivery Tools for Visual Studio" extension that matches your version of Visual Studio. (Or install from the Visual Studio Gallery.)
  4. In Visual Studio Solution Explorer, right-click the project and select Configure Continuous Delivery....
  5. In the Configure Continuous Delivery dialog, select an Azure subscription that has Azure Container Service running. See the FAQ below for information on creating the service.
  6. Click Create and watch the output window for completion of the process, which will include direct links to the project, build pipeline, and release pipeline. A build will also have been started automatically, so you can monitor the progress of the build and deployment in the Azure Pipelines portal.

Note

For a detailed walkthrough of manually creating a release pipeline, including the necessary Azure Container Service, see the following article in the Azure documentation: Continuous Integration and Deployment of Multi-Container Docker Applications to Azure Container Service.

Explore the release pipeline

  1. Navigate to your project using the link in the Visual Studio output window after Configure Continuous Delivery... command.

  2. Click on the Pipelines tab.

  3. Click Builds, select the build pipeline shown there, and click Edit.

    1. The sequence of tasks that appear are what builds the application when code is committed to the repository, and includes tasks to run unit tests.
    2. At the end you'll see tasks that publish the build artifacts to a staging directory. This is the location that the release pipeline monitors, such that when new artifacts appear, the release pipeline will begin a new deployment.
    3. You can examine the artifacts yourself by clicking Artifacts and then Explore.
    4. You can always customize the build pipeline as desired.
  4. Click on Releases, select the release pipeline shown there, and click Edit.

    1. A release pipeline takes build artifacts as input and produces one or more deployments as output.
    2. On the left side of the editor you'll see three environments named Dev, Test, and Production. This is a typical arrangement for continuous deployment within which you can add additional tests in each environment as well as manual approvals.
  5. In the process of setting up the release pipeline, Azure Pipelines will have already queued a build that, when successful, triggered a release. You can examine the results through both the Builds and Releases tab. If the deployment was successful, you can visit the web site and see it running.

Commit a change and see it automatically go live

Now you get to see the magic of a release pipeline at work! You'll make a change in Visual Studio, commit it or check it in to the source repository, and then let the build and release pipelines take care of the rest.

  1. On your deployed web site, click About at the top, and examine the page. We'll change the "Use this area..." text to something different.

  2. In Visual Studio, switch to Solution Explorer, expand src > {project name} > Views > Home and open About.cshtml.

  3. Change the text within the <p> tag, for example, to Hello continuous integration and continuous deployment!

  4. Save the file, push/check-in the changes to the project:

    • If you're using Git in Visual Studio, click the changes icon on the status bar:

      Location of the changes button on the Visual Studio status bar

      Then enter a commit message, click Commit All, click the Sync link, then click Outgoing Commits > Push. This will push the changes to a repository in Azure Repos.

    • If you're using Team Foundation version control (TFVC), switch to the Team Explorer pane, then to Pending Changes where you should see About.cshtml listed. Enter a comment and click Check In.

  5. Once the push/check-in is complete, switch to Azure Pipelines in your browser.

  6. Navigate to Builds and click on your build pipeline. You should see a build in progress:

    A continuous integration build running in Azure Pipelines

  7. When the build has completed, navigate to Releases and you should see that a release has started for that build:

    A continuous deployment release running in Azure Pipelines

  8. When the release has completed, browse to your web site, refresh the page, and see that the change has been deployed.

FAQ

How do I create a Docker-enabled ASP.NET Core solution?

Note

Docker-enabled ASP.NET Core projects are supported only in Visual Studio 2017 RC.3 and higher.

  1. In Visual Studio, connect first to a project.

  2. On the Team Explorer home page under Solutions, click New. (By connecting to a project first and using the command in Team Explorer, the new project is automatically added to source control in the project.)

  3. In the New Project dialog box, search on "ASP.NET Core".

  4. Select ASP.NET Core Web Application (.NET Core)from the list and click OK.

  5. Select Web Application from the template list.

  6. Check that Authentication is set to No Authentication. If not, click Change Authentication to set it.

  7. Set Enable Docker Support

  8. Click the Docker for Windows link to install Docker.

  9. Click OK to complete creating the project.

How do I create an Azure Container Service?

Follow the instructions on Deploy an Azure Container Service cluster. Alternately, see Continuous Integration and Deployment of Multi-Container Docker Applications to Azure Container Service. Note that you'll always be asked to create a new resource group for the container service because the service is composed of many separate resources that you'll certainly want to manage as a group.

A key piece of information you need for the service is the public SSH key. Creating this is described in How to use SSH keys with Windows on Azure. In this topic, be sure to read both the "Create a private key" and "Create a private key for PuTTY" because the latter describes how to obtain the public SSH key that you need in the Azure portal.

We also recommend that for testing purposes you minimize costs by selecting a small Agent virtual machine size in step 3 of the configuration ("Azure Container service settings"). Deploying an Azure Container Service will create a virtual machine in the selected price tier (see pricing). Because this incurs hourly costs, you'll also want to stop the service after you've completed your evaluation and restart it when you're ready for ongoing use.

I created an Azure Container Service but it doesn't appear in Visual Studio's Configure Continuous Delivery dialog?

Try reselecting your User account to refresh the dialog, or close it and reopen it. A newly-created Azure Container Service should then appear.