Deploying an ASP.NET Core website with VSTS and Octopus Deploy

Recently I've been trying out ASP.NET Core for a simple website, one of the first things I do when creating a website project is setup the build and deployment. I find it's much easier to do at the beginning of a project, rather than waiting until later.

In this post I'll describe how I have set this up with Visual Studio Team Services (VSTS) and Octopus Deploy. The post assumes a little familiarity with both tools.

This post uses these tools in particular, although you should be able to recreate using similar steps in most build/deployment tools.

Octopus has become my favourite deployment tool over the last couple of years. There are a lot of good build tools available (I've blogged about TeamCity and AppVeyor before), if you haven't looked at Team Services build in the last year or so you should! It has come on leaps and bounds and is now easy to work with, and has hosted build agents with a free allocation each month, making setup cheap and easy.

The Website

The website in this example is the standard site you get after doing File > New Project > ASP.NET Core Web Application (.NET Core). This has been committed and pushed to a Git repository in VSTS.

The site will be deployed to a Windows Server 2012 VM with IIS installed. In order for ASP.NET Core websites to run via IIS the Windows (Server Hosting) package must be deployed on the server.

Build Setup

The build setup is where most of the work will happen, I'm using the Hosted Pool, which already includes the dotnet SDK, if your using your own build agents you will need to install this first.

Navigate to the Build page in VSTS and create a new build, you can see a GIF of the process here.

  • Select the "Empty" template, then click "Next"
  • Choose your repository, select "Continuous Integration" if you would like the build to be run automatically when you push, then click "Create"
  • Add the following build steps:
    • Utility - Command Line (Twice)
    • Package - Package Application
    • Package - Push Package(s) to Octopus
    • Deploy - Create Octopus Release

You should now have a build definition setup with 5 non-configured steps. Before setting these up, check the other settings in the build definition, in particular set the build number format, under the General tab, I usually use something like 1.0.0$(rev:.r). I will generally set up labeling under the Repository tab as well, creating a label on successful builds based on the build number (Set Label Format to $(Build.BuildNumber)). The rest of the defaults are generally OK.

For each of the build steps configure them as below:

First Command Line Task - dotnet restore

The first step will run the dotnet CLI and restore the required nuget packages etc. needed to build the project.

Property Value
Tool dotnet
Arguments restore

Second Command Line Task - dotnet publish

The second step will build and publish the website to a folder

Property Value
Tool dotnet
Arguments publish src/SampleNetCoreWebsite -o $(build.artifactstagingdirectory)\published -c Release -f netcoreapp1.0

Replace src/SampleNetCoreWebsite with the path to the folder container your website project.

Create Octopus Package Step

This will create a nupkg package from your website publish output.

Property Value
Package ID YourPackageID
Package Format NuPkg
Package Version $(Build.BuildNumber)
Source Path $(build.artifactstagingdirectory)\published
Output Path $(build.artifactstagingdirectory)
Author Your Name/Your Company Name
Title Title of your website
Description A description of your website

Replace the values in bold with your own details. YourPackageID will be needed in later steps, replace it with the value you choose here.

Push Package to Octopus step

This will push the package to the built in Octopus feed. You will need to setup a link to the Octopus service, details on this are in the documentation.

Property Value
Octopus Deploy Serve Select your connection to Octopus
Package $(build.artifactstagingdirectory)\*.nupkg

Create Octopus Release Step

This will create a release in Octopus and deploy it to a specified environment.

Property Value
Octopus Deploy Server Select your connection to Octopus
Project Name The name of your project in Octopus. This will be created later.
Release Number $(Build.BuildNumber)
Include Changeset Comments Ticked
Include Work Items Ticked
To Environment The name of the Octopus environment to deploy to.
Show Deployment Progress Ticked

Replace the values in bold with appropriate values, these will be needed when setting up Octopus.

Octopus Setup

These steps assume you have already set up the tentacle (the machine to deploy to) and created a project in Octopus.

Create variables

I've only setup one variable for this example, HostName, this will be the host name for the website and will be used when setting up IIS etc.

I like to use Octopus' built in IIS setup feature. Having Octopus configure IIS for you can make adding extra servers and environments so much easier, and tends to be a lot more reliable at setting up SSL bindings than using the IIS interface manually (especially with older versions of IIS).

Setup the HostName variable for your various environments with the value you want.

Process

The process is pretty simple and just includes one Deploy a Package step

Once you've added the step navigate to the Configure Features link at the bottom of the page and select IIS web site and application pool, all other options can be disabled, although you may want to include one of the configuration features.

Usually I would also use the Custom installation directory feature as well, but have found this to cause issues with .NET Core websites. On the second deployment an error is thrown saying a file is locked by another process. This doesn't tend to happen with "normal" .NET websites, so I assume is something to do with the new way .NET core apps are run

Property Value
Step name Deploy Website
Run on targets in roles Enter the name of the machine role to deploy to
Package Feed Octopus Server (built-in)
Package ID Your package ID from the build steps
Web Site Checked
Web Site name #{HostName}
Relative home directory Leave blank
Application Pool name #{HostName}
.NET CLR Version No Managed Code
Identity Application Pool Identity
Bindings Protocol: http
Port: 80
Host: #{HostName}
Authentication modes Enable Anonymous authentication

Click Save and you should be ready to go!

Deploying

Go back to your build definition and click Queue Build, this will start the build on VSTS and automatically trigger a deployment to the environment specified!

If all goes well your VSTS Build should turn green, and if you check your project in Octopus you should see something like this showing your site has been deployed:

And browsing to the Host Name you setup and you should see your new .NET core website!

If there are any issues in VSTS or Octopus you should see some error messages to investigate. If your site does not load check IIS is configured correctly, and that the dotnet server hosting package has been correctly installed.


Hope this post helps! If you have any feedback or suggestions let me know in the comments below!


If your looking for help or assistance with your build and deployment setup, devops, development or Telligent installation email hello@ardourdigital.co.uk to find out how Ardour Digital can help!

  • Post