Posts Publish to an Internal NuGet Feed in Azure DevOps
Publish to an Internal NuGet Feed in Azure DevOps
Cancel

Publish to an Internal NuGet Feed in Azure DevOps

In my last posts, I showed how to create NuGet packages in Azure DevOps pipelines. To easily distribute them, you have to publish these NuGet packages to a NuGet feed. This can be done by using nuget.org which is publicly accessible or you can use a private one in Azure DevOps.

In this post, I will show how to create a private NuGet feed in Azure DevOps and how to automatically publish your NuGet packages there using an Azure DevOps pipeline.

Create a private NuGet Feed

To create a private NuGet Feed, open the Artifacts tab in your project in Azure DevOps and then click on + Create Feed.

Create a new NuGet feed

Create a new NuGet feed

This opens a flyout where you can configure the new feed. Provide a name, its visibility, and the scope. Click Create and your private feed gets created.

Create the NuGet feed

Create the NuGet feed

That’s it. Now let’s edit the pipeline to upload the previously created NuGet package to the new feed.

Upload NuGet Packages to a private Feed in an Azure DevOps Pipeline

You can find the code of the demo on Github.

I already created a pipeline that creates a NuGet package, in my last posts. I will extend this pipeline to upload the created NuGet package to the previously created NuGet feed. Publishing the NuGet package is pretty simple. I will create a new stage that will only run when the build stage was successful and also will only run if the build is not triggered by a pull request. This ensures that only builds from the master branch (or any feature branch) will be uploaded to the feed.

Next, I create a deployment where I download the previously published artifacts. These artifacts got published by the build and contain the NuGet packages. I have to download it because the stages are independent of each other. If the upload was in the build stage, I could directly reference the NuGet package without the need to download it.

Lastly, I use a DotNet Core task to push the NuGet package. Use push as the command, the path where you downloaded the NuGet package, and as feed type internal. Don’t care too much about the publish Vsts Feed id. This id is generated by Azure DevOps after you select your previously created feed.

That’s already everything you need to publish the NuGet package to the private feed. The whole stage looks as follows:

You can find the finished pipeline on Github.

Publish the Nuget Package

Run the pipeline and after the build is finished, open your NuGet feed in the Artifacts tab. You will see your NuGet published there.

The NuGet package got published

The NuGet package got published

If your pipeline doesn’t have enough permissions to publish the NuGet package, open the settings of your feed, and navigate to the Permissions tab. There, add the Build Service as Contributor.

Authorize the build service

Authorize the build service

Save the changes and run the pipeline again. Your NuGet package should get published now.

Install NuGet Packages from your Private NuGet Feed

To install a NuGet package from your private feed, right-click your project or solution in Visual Studio, and select Manage NuGet Packages… On the right top of the window, you should see Package source and the settings button on the right of it. Click the settings button and a new window opens. Click on the + symbol and then add a name and the URL to your feed.

Add the NuGet feed as package source

Add the NuGet feed as package source

Click OK and you can select your feed as your package source. Search for the previously uploaded NuGet package and install it.

Install the NuGet package

Install the NuGet package

Note: If you try to add my feed URL, you will get a 401 error because your user is not allowed to access my feed. Nevertheless, the demo will work because I also uploaded the NuGet package on nuget.org which is a public feed. I can access the feed because I am logged into Visual Studio and therefore my credentials are passed. If I tried to use Docker to restore the NuGet package from my private feed, I would also get the 401 error.

In my next post, I will show how to pass an access token to the Dockerfile to access the private NuGet feed.

Conclusion

Creating and using a private NuGet feed in Azure DevOps is easy. In this post, I showed how to create one and how to extend an Azure DevOps pipeline to automatically publish NuGet packages to the private feed. Then, I added the new feed to my project and used Visual Studio to install the NuGet package. In my next post, I will show how to pass an access token to the Dockerfile to access the private NuGet feed.

You can find the code of the demo on Github.

This post is part of “Microservice Series - From Zero to Hero”.

This post is licensed under CC BY 4.0 by the author.