In this post I will show you a number of ways in which you can create and publish NuGet packages to your own feeds. But the focus will be on a new Visual Studio extension that I have written during my assignment for Dime, who were gracious enough to share this utility with the rest of the world.
At this point I should say that this isn’t necessarily the best approach for packages that are meant to be consumed by external parties. For those looking for a great way to distribute NuGet packages, I suggest you go have a look at NServiceBus. The level of automation they have achieved is quite impressive.
In an early stage of our publishing process we used a private NuGet feed which was hosted on Azure. This meant we had to add our packages to the project and deploy the website.
As part of this process, we added the following XML to the .csproj file we were interested in:
This approach worked very well for a while (given the low frequency of publishing packages) but it doesn’t work with a rapidly growing code base and an ongoing migration to .NET Core and ASP.NET Core. With the good news that NuGet is fully integrated in MSBuild, it was time for a change.
When we want to create a NuGet package, we need to use the
nuget pack command:
You can still use the private NuGet feed without any problems, but you’re still stuck with a series of manual steps, which we need to get rid of as much as we can.
Besides the manual steps there is another major downfall to this approach: it takes a lot of time to discover updated packages. At times it takes more than 5 minutes for the feed to notice an update for a certain package and that’s just annoying. The solution to this is easy: use private feeds from MyGet or VSTS. For teams who use VSTS for source control, this makes perfect sense. It is easy to get started and the documentation is easy to follow. Basically all you have to do is to create a new feed, add the feed to Visual Studio and download the tools to your local machine – I will come back to this shortly.
There is a second command which removes the need for any manual work to publish a NuGet package to a VSTS feed:
The command is self-explanatory but note that you also need the VSTS Credential Provider executable, as is shown in the getting started guide:
This leaves us with one final issue: we still have to fire up the command line and pass in the right parameters. For this reason I created a Visual Studio Extension which you can find here. In the repo there are instructions to getting started but I will provide a quick recap:
- Install the Visual Studio Extension
- Go to Tools -> Options
- Find the ‘Dime’ submenu
- In the submenu there are two fields which you need to fill out:
- NuGet path: this is an absolute path to a
nuget.exefile. It doesn’t matter where it is as long as you have access to it.
- VSTS Source: this is the name of the NuGet feed on VSTS. Note that you need
CredentialProvider.VSS.exeto authenticate to VSTS.
- NuGet path: this is an absolute path to a
Once you’ve completed the setup, it’s time to try out the extension. Find a project that you want to publish and right click on the project file. You will find a menu item ‘Publish to VSTS’ which you only have to click.
During the process two command windows will be prompted. No action is required. The first window will execute the nuget pack command and looks something like this:
Note that there is a
.nuspec filed discovered and used by nuget.
After this window is closed, the extension will fire up the second command which executes nuget push:
When the second window has closed your package should be already available on your VSTS NuGet feed.
I initially created this tool out of curiosity for Visual Studio Extensions, so I didn’t take the time to do research on tools or commands that can already do this. All together it took me about two hours to develop it so it’s not a production-worthy tool yet. The next step for the extension is to use the
msbuild command to replace the first step and to get rid one of one more prerequisite, which is that the project is built in release mode. One thing I’d also do is to include nuget.exe and CredentialProvider.VSS.exe inside the extension (or download it somewhere locally).
Feedback or suggestions are always helpful to help improve this small utility. The code is open source so pull requests are welcome. Any issues should also be reported on the repo.