Azure DevOps NuGet Artifacts
NuGet is the Microsoft-supported way of sharing codes for .NET. Compiled codes (DLLs) are bundled into packages. In the case of NuGet, package is a ZIP file with .nupkg
extension. A NuGet package contains DLL files that include code, other related files and a descriptive manifest that contains details related to the package, which we discuss later.
Azure DevOps Artifacts allow you to consume and publish your own NuGet packages. The objective of this article is to briefly summarize steps of publishing and consuming NuGet packages with Azure DevOps private artifact feed. There are varies kinds of tools to work with NuGet,But for this article I used Nuget .exe CLI
.
The nuget.exe
CLI can be used with your .NET Framework projects and non-SDK-style projects. If you are using an SDK-style project, please use the dotnet CLI
instead.
Let’s go through the following steps to publish a NuGet package to Azure DevOps Artifacts.
Step 01: Setting up NuGet .exe CLI
You can download NuGet .exe CLI from nuget.org and save .exe
file to a suitable folder.
Then double click on the setup file and you’ll see a command prompt that appears automatically. Then it will take a few seconds to install and the command prompt will disappear.
Now add the folder that contains .exe
file to your PATH
environment variable.
Simply open cmd and type nuget
to check whether you have successfully installed Nuget .exe CLI
.
Step 02: Create a feed
If you don’t have an Artifact feed you can create a one using the following steps. If you already have one, you are welcome to skip.
Go to Azure Artifacts
Select +New feed
New feed
Then,
- Give the feed a name
- Choose who can read and contribute packages in your feed
- Choose the upstream sources for your feed
- select
Create
Why Upstream?
Upstream sources enable you to use a single feed to store both the packages from both public feeds and authenticated feeds.
If you enabled upstream, when you install a package from the remote feed a copy of it will be saved in the feed. Hence if you enable upstream, you will see many packages in your feed which are defined as dependencies of your own NuGet package.
Step 03: Connect to feed
Select Connect to feed
Then you’ll appear a window as follows.
Open the command prompt and paste the command given in Add this feed
section with your email and Personal Access Token. You must create a PAT with read and write access to packaging.
nuget.exe sources Add -Name “{feedName}” -Source “https://{organization}.pkgs.visualstudio.com/_packaging/{feedName}/nuget/v3/index.json” -username {email} -password {PAT token}
You can see that, It will update NuGet.config
file in the following folder.
C:\Users\{yourAccount}\AppData\Roaming\NuGet
Now you are ready to publish 😊
Step 04: Publish your package
First of all, build your project and go inside the root folder. Then open the command prompt inside the folder and type,
nuget spec
A file called {project name}.nuspec
will be created. It contains metadata about your package.
You should edit it by providing details of your package.
<?xml version="1.0" encoding="utf-8"?><package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd"><metadata><id>SpecificId</id><version>1.0.0</version><title>Title of Package</title><authors>Auther's name</authors><owners>Owner's name</owners><requireLicenseAcceptance>false</requireLicenseAcceptance><description>Description</description><copyright>Copyright © 2019</copyright><dependencies><dependency id="Autofac" version="4.6.2" /></dependencies></metadata></package>
You can ignore dependencies if you want. But if you mention dependencies in .nuspec
the file, they will also install automatically with your package when using.
Now execute the command,
nuget pack {projectname}.csproj
And it will create a package called {SpecificId}.{version}.nupkg
If you want to look inside of the package, just change the extension .nupkg
to .zip
Step 04: Publishing
You have to use push command given in connect to feed window
nuget.exe push -Source " feedName " -ApiKey AzureDevOps {SpecificId}.{version}.nupkg
then it will ask for credentials and give your email and PAT.
But giving PAT for both “UserName” and “Password” worked properly for me😉
Step 05: Consuming a package
If you are using NuGet .exe CLI execute the following command inside a certain folder.
nuget install {SpecificId} -version {version}
It will ask credentials and give them as same as above.
Thank you very much for reading. 😊