Automated Deployment for your Internal Nuget Packages using VSTS

How to automate nuget package deployment with VSTS

When working with a large enough project or Micro Services, you’re doubt to share some code between your solutions. The code could be events, generic services or utility code that you would like to reuse.

Note: It is important to have an product owner of the nuget package and someone who will be responsible for the changes, and the coordination of all the teams that are using the package.

What we are going to cover:

Creating a nuget package in DotNet Core

Setting information about your nuget package

Setting up automated nuget package deployement using VSTS

Prerequisites:

Knowledge of C#

You will need Visual Studio 2017 with the “.NET Core cross-platform development” workload installed

VSTS account (sign up at visualstudio.com)

Ensure you have the Package Manager Extension installed on VSTS

Our Nuget Package

We will create a simple package that allows you to publish events. There will be two parts to this, the first will be the Core project which will expose interfaces, and the Azure Queue implementation which implements this interface.

We are going to start off with a .NET Standard Class Library, called EventPublisher.Core in our EventPublisher Solution.

.NET Standard means that it will be accessible by both .NET Framework as well as .NET Core solutions.

Inside the Core project, let’s create our Services and Events folders.

Inside the Events folder, let’s create our IEvent interface.

IEvent stores the Id of events and when the event was created.

Now we create IEventPublisher in the services folder.

We are currently defining a single method Publish that takes an IEvent and queue name and returns a task (is awaitable).

Now we create EventPublisher.AzureQueue .NET Standard Class Library. Let’s add our EventPublisher.Core reference.

Let’s add WindowsAzure.Storage nuget package to this project.

Install-Package WindowsAzure.Storage

Let’s create our Services folder. Within in that we create EventPublisher within that folder.

We implement our IEventPublisher interface. Our constructor will take in a connection string, resolves our storage account and creates our queue client.

In our Publish method, we ensure that our queue exists (if not we create it) and we publish our message to it.

And that’s it! We have the core of our nuget package setup.

Testing it out

Let’s make sure our library actually works.

We’re going to create a console application that will use the following library.

This will be our EventPublisher.TestApp which we can use to sanity check that our code actually works.

Add the reference to EventPublisher.AzureQueue and the EventPublisher.Core projects.

Now we create our TestEvent which will implement IEvent . We added a new property to our event which is a message we will be sending to the queue.

Let’s put it all together in our Program.cs file.

We’re using the local Azure Storage Emulator to test our our queue. We construct a new TestEvent and a new instance of EventPublisher and then we publish our event.

There you have it!

Setting nuget package meta data

Right click on EventPublisher.AzureQueue project then go to Properties, and let’s set some package information.

Let’s do the same for EventPublisher.Core

You can add more meta data for you nuget package to your nuget package to inform users of its use.

Setting up automated Builds in VSTS

Ensure that you have your Visual Studio account connected to your VSTS account.

Before that, make sure that you have a VSTS project setup. Follow these instructions to creating a new project.

Whilst we’re here, we can set up the package manager for our nuget packages. Given that you have installed the Package Manager Extension (mentioned in the prerequisites), we can now head to the packaging section found under the Build and release heading.

Clicking on “New feed”, and name it something appropriate. I’m calling mine “Internal Repo”.

You can set the Visibility of this feed, i.e. who can access it and also whether you want access to public sources from this feed. I leave the defaults as they are.

That should be it.

The feed gives us access to (NuGet, npm, Maven and Gradle) but we’re only focusing on NuGet in this tutorial.

Let’s head back to our source control…

In the Teams Explorer, to publish this source to our VSTS project.

Choose publish Git Repo under the Teams Services.

Note: If you chose to, you can publish to any of the following and still be able to follow the rest of the article (VSTS Git, TFVC, GitHub, GitHub Enterprise, Subversion, Bitbucket Cloud, External Git repositories)

Under the advance settings, you should get a drop down of which team project you want to add it to. Choose the one you just created.

Now you can publish. Ensure all of your changes having been committed and once more, do a push.

Let’s head over to VSTS. In your Project, go to the code section, you should be presented with something like this.