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.

No alt text provided for this image

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

No alt text provided for this image

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.

No alt text provided for this image

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.

No alt text provided for this image

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.

No alt text provided for this image

Detailed YAML for build pipeline that we created above.


# Variable 'BUILD_BUILDNUMBER' was defined in the Variables ta
# 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: <Project_Name>/<Feed_Name>
? - 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: <Project_Name>/<Feed_Name>
? - task: PublishBuildArtifacts@1
? ? displayName: publish artifact
? ? condition: succeededOrFailed()
? ? inputs:
? ? ? PathtoPublish: $(build.artifactstagingdirectory)
? ? ? TargetPath: '\\my\share\$(Build.DefinitionName)\$(Build.BuildNumber)'
...b        

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

No alt text provided for this image

In the variables tab, create these variables.

No alt text provided for this image

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.

No alt text provided for this image

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

No alt text provided for this image

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.

No alt text provided for this image

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.

NOTE: Alterante YAML we can use to implement this using Nuget Task.


# Variable 'BuildEnv' was defined in the Variables tabname: 2.0.
jobs:
- job: Job_1
? displayName: Agent job 1
? pool:
? ? vmImage: windows-2019
? steps:
? - checkout: self
? - task: NuGetCommand@2
? ? displayName: NuGet restore
? ? inputs:
? ? ? solution: '**/Logging.csproj'
? ? ? feedRestore: <projectName>/<feed>;
? - task: DotNetCoreCLI@2
? ? displayName: dotnet build
? ? inputs:
? ? ? projects: '**/Logging.csproj'
? ? ? arguments: --configuration $(BuildConfiguration)
? - task: NuGetCommand@2
? ? displayName: NuGet pack
? ? inputs:
? ? ? command: pack
? ? ? searchPatternPack: '**/Logging.csproj'
? ? ? versioningScheme: byEnvVar
? ? ? versionEnvVar: BuildEnv
? - task: DotNetCoreCLI@2
? ? displayName: dotnet test
? ? inputs:
? ? ? command: test
? ? ? projects: '**/Logging.UnitTest.csproj'
? ? ? arguments: --configuration $(BuildConfiguration)
? - task: NuGetCommand@2
? ? displayName: NuGet push
? ? inputs:
? ? ? command: push
? ? ? searchPatternPush: $(Build.ArtifactStagingDirectory)/**/*.nupkg
? ? ? feedPublish: <projectName>/<feed>;
? - task: PublishBuildArtifacts@1
? ? displayName: publish artifact
? ? condition: succeededOrFailed()
? ? inputs:
? ? ? PathtoPublish: $(build.artifactstagingdirectory)
? ? ? TargetPath: '\\my\share\$(Build.DefinitionName)\$(Build.BuildNumber)'
...1        

要查看或添加评论,请登录

Harpreet S.的更多文章

  • K6 for API Performance Testing: A Modern Approach

    K6 for API Performance Testing: A Modern Approach

    K6 is a powerful open-source tool designed specifically for performance testing of APIs, applications, and…

    1 条评论
  • GraphQL API Automation Testing

    GraphQL API Automation Testing

    What is GraphQL? GraphQL is an API specification standard and an Open-Source data query and manipulation language. It…

    8 条评论
  • Mobile application automation testing using Appium

    Mobile application automation testing using Appium

    Due to the increasing volume of mobile applications in development, we’ve seen a great number of tools for testing them…

    1 条评论
  • Test Approach and Comparisons between TDD and BDD

    Test Approach and Comparisons between TDD and BDD

    Test Approach for ATDD ATDD usually involves establishing the criteria first, most often from a user perspective, and…

社区洞察

其他会员也浏览了