Create And Publish A NuGet Package Into Azure Artifacts Using CI/CD Pipeline

This article demonstrates how we can easily publish a class library project into Azure Artifacts using a simple CI/CD pipeline. If the project needs to store as a package in the private repository instead of a public repository like Nuget.org, we can go ahead and use Azure artifacts. From a security perspective, all the packages are basically local to the company/ organization itself.

In the artifacts, all the packages are stored in something known as feeds. So, to start publishing and consuming packages as your artifacts, the first thing that we must do is create a feed. Now, that feeds we can group packages together and can control the access onto the package itself.

Let’s go ahead and develop a class library project and let's create feed-in Azure artifacts into Azure DevOps.

Step 1 - Create a simple .NET Class Library project

Now, we will create a simple class library project in a visual studio. And add a simple method for addition and multiplication into the class.

Create And Publish A NuGet Package Into Azure Artifacts Using CI/CD Pipeline

Let’s check project properties and check package property. And over here we can check all the details about the package.

Create And Publish A NuGet Package Into Azure Artifacts Using CI/CD Pipeline

Step 2 – Push project to Azure Repos

Let’s push the changes into Azure DevOps repos. Click on git and remote URL. The assumption here is that we already created a new repository in Azure DevOps.

Create And Publish A NuGet Package Into Azure Artifacts Using CI/CD Pipeline

Step 3 - Create a new feed-in Azure Artifacts

Let’s create a new feed-in Artifacts. Just giving a name for the feed, and in terms of the visibility, we can go ahead and choose the visibility.

Create And Publish A NuGet Package Into Azure Artifacts Using CI/CD Pipeline

Step 4 – Create Nuget Package using Simple CI/CD Pipeline

Let’s create a simple build pipeline to pack and push the project into the target feed location that we created in earlier steps. Below is a sequence of tasks that we need to configure.

Create And Publish A NuGet Package Into Azure Artifacts Using CI/CD Pipeline

Detailed YAML for build pipeline that we created above.

# Variable 'BUILD_BUILDNUMBER' was defined in the Variables tab
# Variable 'BuildEnv' was defined in the Variables tab
trigger:
  branches:
    include:
    - refs/heads/main
name: 1.0.$(Rev:r)
jobs:
- job: Job_1
  displayName: Agent job 1
  pool:
    vmImage: windows-2019
  steps:
  - checkout: self
  - task: DotNetCoreCLI@2
    displayName: dotnet restore
    inputs:
      command: restore
      projects: '**/MyAwesomeLibray/*.csproj'
      feedRestore: 02cbe044-5ae8-44fb-ad6c-12b7f5569259/c84aad62-9d47-4d3d-a00a-716ed168d052
  - task: DotNetCoreCLI@2
    displayName: dotnet build
    inputs:
      projects: '**/MyAwesomeLibray/*.csproj'
      arguments: --configuration $(BuildConfiguration)
  - task: DotNetCoreCLI@2
    displayName: dotnet pack
    inputs:
      command: pack
      publishWebProjects: false
      projects: '**/*MyDemoApp.sln'
      arguments: --output $(Build.ArtifactStagingDirectory)/
      zipAfterPublish: True
      searchPatternPack: '**/MyAwesomeLibray/*.csproj'
      versioningScheme: byEnvVar
      versionEnvVar: BuildEnv
  - task: DotNetCoreCLI@2
    displayName: dotnet nuget push
    inputs:
      command: push
      feedPublish: 02cbe044-5ae8-44fb-ad6c-12b7f5569259/c84aad62-9d47-4d3d-a00a-716ed168d052
  - task: PublishBuildArtifacts@1
    displayName: publish artifact
    condition: succeededOrFailed()
    inputs:
      PathtoPublish: $(build.artifactstagingdirectory)
      TargetPath: '\\my\share\$(Build.DefinitionName)\$(Build.BuildNumber)'
...

Make sure that the build number format in the options tab is correct. This will make sure the version of your Nuget package.

Create And Publish A NuGet Package Into Azure Artifacts Using CI/CD Pipeline

In the variables tab, create these variables.

Create And Publish A NuGet Package Into Azure Artifacts Using CI/CD Pipeline

In the triggers tab, enable continuous integration should be checked.

Once the build pipeline setup is done, click on save and queue.

Let’s check Feed-in Azure artifacts… we can see that package got created.

Create And Publish A NuGet Package Into Azure Artifacts Using CI/CD Pipeline

Step 5 - Access and Install Package into Another Application

Now, if we click on Connect to feed over here, it will show the different ways we can connect to the feed. We will choose the Visual Studio option. Here we will see the name and source URL and we will use this value in subsequent steps.

On the Tools menu in visual studio, select Options > NuGet Package Manager > Package Sources. Select the green plus icon in the upper-right corner and enter the name and source URL.

Create And Publish A NuGet Package Into Azure Artifacts Using CI/CD Pipeline

Let’s add this package into the new application where we will use the package functionalities. While clicking on manage NuGet Package of a project.

Create And Publish A NuGet Package Into Azure Artifacts Using CI/CD Pipeline

Awesome! We are done with the demonstration and see that how easily we can easily create a NuGet package using CI/CD pipeline and push it into a private feed in Azure Artifacts. And lastly, we access that package and install it into a client app. Hope you find this detailed explanation useful.

Happy Learning!