How to build DotNet in Azure DevOps

In this article we will explain how to build dotnet core applications with Azure DevOps. Hope you have already went through the previous article on <a href="https://ciemasen.com/articles/how-to-build-angular-in-azure-devops" title="How to build Angular in Azure DevOps">How to build Angular in Azure DevOps</a>
In this article we will explain how to build dotnet core applications with Azure DevOps. Hope you have already went through the previous article on How to build Angular in Azure DevOps

Setting up the dotnet core project

If you are new to dotnet core(.net core), you can follow our previous article on Setting up .NET Core Local Environment article to setup the environment. In this article we will create simple dotnet core web application.

Create dotnet application

We will not show you how to create an application using visual studio here. You can create a starter project using default web API template in visual studio. Now you have the web applications created and you are ready to move on.

Configure azure pipeline

As explained in the previous article How to configure Azure DevOps CICD, lets create devops pipeline.

It will add a new file called azure-pipelines.yml into root folder of the project.

We can modify the content of the azure-pipelines.yml file to include following steps.
  • Restore dotnet packages
  • Build dotnet project
  • Publish dotnet project
  • Publish artifact

* You can always modify the displayName of these tasks

Let's have a detail look on how we can do those.

Restore dotnet packages

We can add DotNetCoreCLI task as a step and configure as following
        
  - task: DotNetCoreCLI@2
    displayName: "Restore dotnet packages"
    inputs:
      command: restore
      projects: "**/*.csproj"
      
      

Build dotnet project

Now we have nuget packages restored and we are ready to build the project. Same as the previous, lets add DotNetCoreCLI task as a step and configure as following.
        
  - task: DotNetCoreCLI@2
    displayName: "Build with $(buildConfiguration) configuration"
    inputs:
      projects: "**/*.csproj"
      arguments: "--configuration $(BuildConfiguration)"
      
      

Publish dotnet project

Once we confirm that the project doesnt have any build errors, we are ready to publish the output. Lets add DotNetCoreCLI task as a step one more time and configure as following
        
  - task: DotNetCoreCLI@2
    displayName: "Publish"
    inputs:
      command: publish
      arguments: "$(Build.SourcesDirectory)/$(DefaultProject)/$(DefaultProject).csproj -c $(BuildConfiguration) -o $(Build.ArtifactStagingDirectory)"
      workingDirectory: "$(DefaultProject)"
      zipAfterPublish: false
      publishWebProjects: false
      
      

Publish artifact

We have successfully published the output and let's keep it as a build artifact so that we can use that for deployment. Let's add PublishBuildArtifacts and configure as following.
        
    displayName: "Publish artifact: drop"
    inputs:
      pathtoPublish: "$(Build.ArtifactStagingDirectory)"
      artifactName: "drop"
      publishLocation: "Container"
      
      

Final yaml structure

        
trigger:
  - main
  - development
  - backlogs/*

variables:
  BuildConfiguration: "Release"
  DefaultProject: "build-dotnet-in-azure-devops"

steps:
  - task: DotNetCoreCLI@2
    displayName: "Restore dotnet packages"
    inputs:
      command: restore
      projects: "**/*.csproj"

  - task: DotNetCoreCLI@2
    displayName: "Build with $(buildConfiguration) configuration"
    inputs:
      projects: "**/*.csproj"
      arguments: "--configuration $(BuildConfiguration)"

  - task: DotNetCoreCLI@2
    displayName: "Publish"
    inputs:
      command: publish
      arguments: "$(Build.SourcesDirectory)/$(DefaultProject)/$(DefaultProject).csproj -c $(BuildConfiguration) -o $(Build.ArtifactStagingDirectory)"
      workingDirectory: "$(DefaultProject)"
      zipAfterPublish: false
      publishWebProjects: false

  - task: PublishBuildArtifacts@1
    displayName: "Publish artifact: drop"
    inputs:
      pathtoPublish: "$(Build.ArtifactStagingDirectory)"
      artifactName: "drop"
      publishLocation: "Container"

      
      

Now we have the valid output attached to azure devops pipeline as an artifact. We can use the files in the artifact to deploy dotnet application

Hope you enjoy the tutorial and see you soon in another very exiting tutorial