Setup VSTS YAML builds for your ASP.NET Core app

Picture: Microsoft

Setup VSTS YAML builds for your ASP.NET Core app

Idea

YAML builds are a very common way to define builds in AppVeyor, but in Visual Studio Team Services (VSTS) this feature is pretty new - and still in beta.

But it gives you the most powerful way to have your build definitions as close as possible to your source code.

Enable YAML builds

Because it is still in preview (June 2018), you have to enable YAML builds for your team (you need admin permissions!). VSTS: Enable preview features

Create your YAML definition.

VSTS automatically creates the definition, if you add a file to your master branch named .vsts-ci.yml

A simple definition for ASP.NET Core could look like:


queue:
  name: Hosted 2017

steps:

# Download the latest recommended NuGet exe

- task: NuGetToolInstaller@0
  displayName: "NuGet use 4.6.2"
  inputs:
    versionSpec: 4.6.2

# Restore NuGet packages

- task: NuGetCommand@2
  displayName: "NuGet Restore"
  inputs:
    restoreSolution: '**/*.csproj'
    feedsToUse: config
    nugetConfigPath: NuGet.config # you should always have a NuGet.config file!

# Build your .NET Core project (but avoid restore your packages again)

- task: DotNetCoreCLI@2
  displayName: ".NET build"
  inputs:
    projects: '**/*.csproj'
    arguments: --configuration $(BuildConfiguration) --no-restore

# Run your unit tests

- task: DotNetCoreCLI@2
  displayName: ".NET test"
  inputs:
    command: test
    projects: 'test\**.csproj'

# Create the deployment package for your web project

- task: DotNetCoreCLI@2
  displayName: ".NET publish package"
  inputs:
    command: publish
    arguments: '--configuration $(BuildConfiguration) --no-restore --output $(Build.ArtifactStagingDirectory)/app/pkg'

# Publish your web project deployment packages as named artifact 'app'

- task: PublishBuildArtifacts@1
  displayName: "Publish artifacts"
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)/app'
    ArtifactName: app
    publishLocation: Container

Use YAML exports

With the enabled YAML build preview feature you can export your existing VSTS build definitions as YAML builds. Just open the definition and click on View YAML

Right now, you get a warning about undefined variables in your YAML builds - but you can ignore this error, if you are just using the VSTS default variables.

Official documentation

YAML builds are still in prview, so some features of VSTS dont work with YAML build (like build badges): VSTS: How to use YAML builds

Abel Wang from Microsoft uploaded a great video about VSTS YAML builds on YouTube: