Continuous delivery of Nuget packages to Package Management in TFS15 RC1

Datetime:2016-08-23 04:26:49          Topic:          Share

TFS 15 RC1 got released a couple of days ago and one of the most awaited features from on-prem customers was Package Management.

In this post, we will see how we can publish our common code as NuGet packages so that it can be shared across the organization. Also, we will see how we can keep these packages up to date by publishing them to our TFS15 package management server as part of our continuous delivery process.

Install Package Management extension

To use the TFS15 Package Management on on-prem, you first need to install the Package Management extension. The package management extension now comes as part of TFS15 RC1 installation. To install, you first need to go to your local marketplace.

On your on-prem server home ( http://localhost:8080/tfs for example) click on marketplace icon and then click Browse TFS extensions

You will be taken to your local extension marketplace.

Click on Package Management extension and install it on to your desired TFS collection.

Create a package feed in Package hub

A package feed is a container of your packages. Package clients similar to NuGet Package Manager of Visual Studio will be able to subscribe to this feed and receive packages.

Once you install the extension, you should see Package Feeds in your navigation bar under Build/Release menu. Click Package Feeds

You will be navigated to Package Feeds screen.

Click New Feed . This opens a new dialog. Provide the feed details.

Once you click OK, a new NuGet feed will be created.

Note down the feed URL from the feed details page on the right side. We will use it later in the post to publish our packages to this feed. Also, If you want to use your own instance of nuget.exe you can download it from here.

Manage feed Security

You can right click on the feed, click Edit - to manage its security, such as who can contribute packages, who can consume the packages etc.

Build and publish NuGet packages solution

Now we are in a position to build our solution and publish the nuget packages. For this post, I have two of my commonly used libraries which I would like to publish as NuGet packages.

My code structure is as below.

Now to build the solution, I have created a simple build definition with following steps.

Most of the steps are self-explanatory. However, I would like to briefly describe the following important steps.

  1. NuGet Packager : I use this task to package my code in to nupkg files. This task scans either csproj/nuspec file and packages in to nupkg file.
  2. In the next task I publish the generated nupkg file as artifacts of this build. This task also copies packages in to $(build.artifactstagingdirectory)
  3. NuGet Publisher : Finally, I use this task to find all the packages in the $(build.artifactstagingdirectory) and publish this to our feed.

Let’s see packaging and publishing task in detail.

NuGet Packager

As the name indicates, the first task is is used to pack the code as Nuget package. The details of the task configuration is as below.

NuGet Publisher

As the name indicates, this task is is used to publish the code to our Nuget feed. The details of the task configuration is as below.

Noticethat for publishing to local package feed you need to select feed type as Internal NuGet Feed .

The next parameter is to provide the feed URL generated during the feed creation step above. For me it was as below.

http://localhost:8080/tfs/DefaultCollection/_packaging/MyCompanyNuget/nuget/v3/index.json

Save the build definition and Queue a new build. If everything is set right, your build should be green and you should see your packages available in the feed.

Consuming the NuGet feed

You or your team is now ready to consume these NuGet packages. The details page, when you create the feed, already gives instructions on how to add this feed as a source to your package manager client (for example Visual Studio’s NuGet Package Manager). The command, to add this feed as a source is

nuget.exe sources Add -Name "MyCompanyNuget" -Source http://localhost:8080/tfs/DefaultCollection/_packaging/MyCompanyNuget/nuget/v3/index.json

Execute the above command on the client machine command prompt. This should update the Nuget.config file and you will see a new NuGet source.

If you are Visual Studio user, you can also add this source by going to Tools | Options | NuGet Package Manger | Package Sources and providing feed URL as below.

Summary

That’s it. You have integrated publishing your NuGet Packages to the local package feed within your TFS 15. So every time you update your package, you are publishing the latest packages to the NuGet feed, thus making sure your team always consumes latest packages.

Thanks for reading.