Beginners guide to NuGet Package and Azure Artifacts
Emma was working in a company that created products for accounting. These products pertained to accounting processes such as billing and inventory management, maintaining accounts, etc. Not surprisingly, Emma’s company created all its products using a single programming language.
Emma found a lot of code redundancies across the products as some of the features were common across the products. For instance, simple functionalities such as addition, subtraction, multiplication, and division were common between these products. She badly wanted to reduce the code redundancies as it is one of the best practices to follow in the coding world.
To avoid these duplicate codes, Emma found a simple solution using NuGet and Azure Artifacts. This post is going to talk in detail about what is NuGet package and Azure Artifacts along with how they play a major role in a real-life application.
Table of content
- What is NuGet package?
- Introduction to Azure DevOps and Artifacts
- Artifacts and its key features
- Practical implementation of NuGet on Azure DevOps
- Wrapping Up
What is NuGet package?
In the modern development process, developers generally create, share, and consume reusable pieces of code regularly. This generally saves time by making changes at only one location and also increases the code standards by avoiding duplicate codes across projects.
Often such codes are consumed through packages (i.e compiled versions of the code). For .Net based applications, NuGet, a Microsoft supported platform contains these packages that are ready to be consumed in various projects.
NuGet package is a single zip file with .nupkg as a file extension. Developers with code that can solve a common issue can build the solution and publish the same to the NuGet. Package consumers can refer to the package in their code and make use of the NuGet package.
Versioning can be done and maintained for NuGet packages which help users to choose a version of their need. We will see in detail how to create a NuGet package later in the blog.
Introduction to Azure DevOps and Artifacts
Azure DevOps is a Software-as-a-Service (SaaS) offering from Microsoft. It provides end-to-end support to the DevOps model for developing and deploying software. It consists of a range of services such as Azure Boards for agile planning and tracking, Azure Pipelines for supporting Continuous Integration (CI)/ Continuous Deployment (CD), Azure Repos for git-based version controlling tools, etc.
Azure Artifacts is one such service offerings from Azure DevOps. It is a package management tool with support for Maven, npm, Python, and NuGet which can be shared publically or privately within an organization. Each package in the Artifacts will have a feed through which users will be able to access the package.
Artifacts and its key features
To have seamless delivery of software, it is necessary to address the dependencies correctly using package management solutions. Dependencies are the packages that are reused in a solution instead of having their logic inside the solution. If dependencies are not maintained properly, then over the time, it will become a blocker as it will increase the difficulties in versioning and testing.
So, there is always a need to make dependencies as manageable packages and Azure provides Artifacts for it. Here are its two key features -
Once a package is created and it's uploaded to a feed, the package gets a version number and it is permanently reserved for it. Also, users cannot upload a new package with the same version number or delete and upload it with the same version number. This feature of the package is called immutable.
Provides Integration with Azure Pipelines
It is available as an extension in Azure Pipelines. In the build pipeline, while building and publishing a solution that has a dependency package in an artifact, the package can be directly referred from the pipeline and the required dependencies can be downloaded and built along with the solution.
Practical implementation of NuGet on Azure Packages
As a first step, create a solution with a common operation that needs to be performed across different solutions. To achieve it, open Visual Studio, create a new project with only Class library projects in it.
Provide a valid name to the project.
Once created, rename the Class1.cs to a valid name and implement the required features in the class file.
Implement the functionality as required and build the project.
Now, download the nuget.exe from https://www.nuget.org/downloads and place it in the current project location. From that location, open command prompt or PowerShell and run the below command
nuget.exe pack PARTECH.Common.Library.csproj
Once the above command is run, it will result in creating the NuGet package for PARTECH.Common.Library with the version as 1.0.0
To update the version number, go to PARTECH.Common.Library.csproj file and add the below provided tags.
At this point, we have a common project created and it's built and packed as a NuGet package in our local. The next step of the process is to create a feed in the Azure DevOps Artifacts. The user has to provide a name for the feed and provide the visibility of the feed (i.e internal to an organization or public)
Once the feed is created, we are good to push the package to the feed with the help of the below command. It will prompt the user to enter credentials for Azure DevOps. Once entered, the package will be pushed to Artifacts in Azure DevOps.
nuget.exe push -source "feedname" -ApiKey az PARTECH.Common.Library.1.0.0.nupkg
Users can use the ‘connect to feed’ option to get the link of the feed and use the PARTECH.Common.Library project in other solutions. To refer to the above created NuGet package, users have to Go to Tools → NuGet Package Manager → Package Manager Settings.
In the dialog box, provide the feed link and appropriate name.
Once the feed is saved, go to Manage NuGet package for the solution and refer to the package in the solution.
Follow these steps to successfully create a common code and create a NuGet package out of it.
Azure Artifacts has a lot of great features and helps the DevOps team to deliver changes in a faster way with minimal human interventions. With the advent of NuGet packages, teams can save time in developing the same feature multiple times. This also means reduced workload for the developers and overall improved efficiency.