Optimising CI/CD workflows, especially for .NET Core projects, requires efficient build artifact management in Azure Pipelines. Build artifactstagingdirectory and the DotNetCoreCLI@2 task are two strong tools available in Azure DevOps, which also give flexibility. With these features, developers may define and build artifact output paths exactly, which helps to streamline deployment operations. This tutorial explains how to use Azure Pipelines in conjunction with these features to improve software delivery pipeline dependability and efficiency.

Make build task orchestration easier with Azure DevOps by using DotNetCoreCLI commands like restore, build, and publish. Developers can make sure that compiled apps are kept in specified locations and prepared for deployment by using the build artifact staging directory settings. The best practices for establishing these components within Azure Pipelines are outlined in this tutorial, enabling teams to accomplish flawless integration and deployment of .Net core applications.

Before getting started, ensure you have the following:

  • An Azure DevOps organization and project
  • A .NET Core project hosted in a repository (e.g., GitHub, Azure Repos)
  • Basic knowledge of YAML for defining pipelines

Step-by-Step Guide

Step 1: Define Your Pipeline

Start by creating a new YAML file for your pipeline, typically named azure-pipelines.yml, in the root of your repository.

Code Syntax :
trigger:
- main
pool: vmImage: 'ubuntu-latest'
variables: buildConfiguration: 'Release' outputFolder: '$(Build.ArtifactStagingDirectory)/output'
steps:
- task: UseDotNet@2 inputs: packageType: 'sdk' version: '6.x.x'
- task: DotNetCoreCLI@2 inputs: command: 'restore' projects: '**/*.csproj'
- task: DotNetCoreCLI@2 inputs: command: 'build' arguments: '--configuration $(buildConfiguration) --output $(outputFolder)'
- task: DotNetCoreCLI@2 inputs: command: 'publish' arguments: '--configuration $(buildConfiguration) --output $(outputFolder)'
- task: PublishBuildArtifacts@1 inputs: PathtoPublish: '$(outputFolder)' ArtifactName: 'drop'

Step 2: UseDotNet@2 Task

The UseDotNet@2 task is used to install the .NET SDK. Ensure you specify the appropriate version for your project.

Code Syntax :
- task: UseDotNet@2 inputs: packageType: 'sdk' version: '6.x.x'

Step 3: Restore Dependencies

The DotNetCoreCLI@2 task with the restore command restores the project dependencies.

Code Syntax :
- task: DotNetCoreCLI@2 inputs: command: 'restore' projects: '**/*.csproj'

Step 4: Build the Project

The build command compiles the project. Here, we specify the output folder using the –output argument.

Code Syntax :
- task: DotNetCoreCLI@2 inputs: command: 'build' arguments: '--configuration $(buildConfiguration) --output $(outputFolder)'

Step 5: Publish the Project

The publish command compiles and packages the project into a self-contained directory. Again, we specify the output folder.

Code Syntax :
- task: DotNetCoreCLI@2 inputs: command: 'publish' arguments: '--configuration $(buildConfiguration) --output $(outputFolder)'

Step 6: Publish Build Artifacts

Finally, the PublishBuildArtifacts@1 task publishes the output to Azure Pipelines. We specify the path to the output folder and name the artifact drop.

Code Syntax :
- task: PublishBuildArtifacts@1 inputs: PathtoPublish: '$(outputFolder)' ArtifactName: 'drop'

Explanation of Key Commands

  1. UseDotNet@2:
    • packageType: Specifies the type of package to install (SDK or runtime).
    • version: The version of the .NET SDK to install.
  2. DotNetCoreCLI@2:
    • command: Specifies the .NET CLI command to run (e.g., restore, build, publish).
    • projects: Specifies the project(s) to operate on. Using **/*.csproj targets all projects in the repository.
    • arguments: Additional arguments for the .NET CLI command. The –output argument specifies the output folder.
  3. PublishBuildArtifacts@1:
    • PathtoPublish: The path to the directory containing the build artifacts.
    • ArtifactName: The name of the artifact to create.

Conclusion

To optimize your CI/CD processes with .NET Core applications, you must become proficient with Azure Pipelines and the DotNetCoreCLI@2 task for building artifact configuration. You can make sure that your build artifacts are properly staged and prepared for deployment by learning how to use the build. artifact staging directory.

Consider working with CMARIX if you need professional help utilizing .NET Core and Azure DevOps to optimize your development processes. Hire .NET Core developers from CMARIX to improve your product delivery cycles and CI/CD processes.