My current client, just started to use .NET core as their main development platform. I felt this is the right time to revisit the build steps and document them. So in this blog post we will see how easy it us to build and publish a .NET Core app within VSTS.
For sample, we have two .NET core web applications, one hosting an Angular application and another hosting an Identity Server.
- First step we restore all the
npm
packages required for Angular application. For this we addnpm
task from the tasks library and run the commandinstall
We set the working folder because our
package.json
is nested in a folder within the repository.
- Our application is based on .NET Core SDK 2.1.300 and because we are running this build on a self hosted agent, we want to make sure the agent has .NET core 2.1.300 SDK is installed. To do that, we add
.NET Core Tool Installer
task to our build definition and install the required SDK.
- We need to tell this task the version of .NET Core SDK we need. We do that by specifying the correct version number in the version field.
Please see the version number of SDK (or runtime) should exactly match as in the document here
- Next we need to build the application. We add a .NET core task and select
build
option from thecommand
dropdown. We are looking for all*.csproj
files.
Note that, starting .NET Core 2.0, the application dependencies are automatically restored by commands
dotnet build
anddotnet publish
. You no longer required to rundotnet restore
command.
- Next we need to execute our unit tests. To do that we again add
dotnet core
task and this time selecttest
command. We are scanning tests in*.UnitTests.csproj
projects. Make sure you checkPublish tet results
checkbox so that test results can be published to VSTS. This task executes the tests and publishes the test results to agent’s temp directory
- Next we find the published test results file
(*.trx)
file to VSTS. We use thePublish task
and change the Search folder to$(Agent.TempDirectory)
dotnet test task currently has results directory hardcoded to agent’s temp directory, which can be accessed using $(Agent.TempDirectory)
- Next, we will publish the artifacts to VSTS. We add one more .NET Core task and select
publish
from the dropdown.
We specify additional arguments like --configuration
and set output directory as $(Build.ArtifactStagingDirectory)
using--output
argument. Notice, we also check `Add project name to publish path to publish artifacts in to their individual directories.
- Finally, we add
Publish Build Artifact
task to publish all the contents from$(Build.ArtifactStagingDirectory)
to VSTS.
Let’s queue a build and you should see all the artifacts published in nice structure within drop
directory.
This was a small post to get you started with .NET core build pipeline in VSTS. Hope you found this post useful.