.NET Core Yaml build on Azure DevOps

I happen to have an Azure DevOps project called KnoxDevOps. (It’s a public project, but you might have to open in incognito mode to see it.)

In this project I have this structure:

src/
    KnoxDevOps.API/
        KnoxDevOps.API.csproj
    KnoxDevOps.Core/
        KnoxDevOps.Core.csproj
    KnoxDevOps.Tests/
        KnoxDevOps.Tests.csproj
    knoxdevops.sln

I also have an Azure DevOps YAML build definition for an Azure Build Pipeline.

pool:
  vmImage: 'Ubuntu 16.04'

variables:
  buildConfiguration: 'Release'

steps:
- script: |
    dotnet build ./src/KnoxDevOps.API/ --configuration $(buildConfiguration)
    dotnet test ./src/KnoxDevOps.Tests/ --configuration $(buildConfiguration) --logger "trx;LogFileName=testresults.trx"
    dotnet test ./src/KnoxDevOps.Tests/ --configuration $(buildConfiguration) /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura /p:CoverletOutput=$(System.DefaultWorkingDirectory)/TestResults/Coverage/
    cd ./src/KnoxDevOps.Tests/
    dotnet reportgenerator "-reports:$(System.DefaultWorkingDirectory)/TestResults/Coverage/coverage.cobertura.xml" "-targetdir:$(System.DefaultWorkingDirectory)/TestResults/Coverage/Reports" "-reportTypes:htmlInline" "-tag:$(Build.BuildNumber)"
    cd ../../
    dotnet publish ./src/KnoxDevOps.API/ --configuration $(buildConfiguration) --output $BUILD_ARTIFACTSTAGINGDIRECTORY
  failOnStderr: true

- task: PublishTestResults@2
  inputs:
    testRunner: VSTest
    testResultsFiles: '**/*.trx'

- task: PublishCodeCoverageResults@1
  inputs:
    codeCoverageTool: cobertura
    summaryFileLocation: $(System.DefaultWorkingDirectory)/TestResults/Coverage/**/*.xml
    reportDirectory: $(System.DefaultWorkingDirectory)/TestResults/Coverage/Reports
    failIfCoverageEmpty: true

- task: PublishBuildArtifacts@1

This build definition depends upon the test project’s references which include these. I’ve added on references to the most fantastic Report Generator and Coverlet to make nice code coverage reports.

  <ItemGroup>  
    <DotNetCliToolReference Include="dotnet-reportgenerator-cli" Version="4.0.0-rc6" />
    <PackageReference Include="coverlet.msbuild" Version="2.3.0">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.1" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
    <PackageReference Include="xunit" Version="2.3.1" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
  </ItemGroup>

The build output gives me results that include tests & code coverage in output.

Build Shown


Tests

Build Shown


Code Coverage

Build Shown

Summary

The canonical example of Azure DevOps .NET Core build pipeline with yaml is here. I’m just adding on code coverage report for cross-platform.