How to host a NuGet v3 feed on Azure storage

NuGet v3 for Visual Studio 2015 introduced a new type of feed designed to work with static json files. Package information is retrieved constructing URLs based on the id of a package and parsing the information on the client. This allows for the feed server to be as simple as a static file host. For this post I'll show you how to use a basic Azure storage container to host the feed, and a tool I've created to build the json files that the NuGet client will read.

Setting up Sleet

Sleet is a tool for generating NuGet v3 feeds and copying them to either a local folder or directly to an Azure storage container. To get started download latest release of Sleet from github here.

Sleet commands rely on a sleet.json settings file which contains information about the feed it is publishing to. A basic sleet.json template file for an azure feed can be created using the createconfig command with --azure.

sleet.exe createconfig --azure

This command will create a sleet.json file next to sleet.exe. Next edit sleet.json and modify it to match your own storage container. This should be a new storage container that does not have any other files in it.

The default output looks like this:

{
  "username": "",
  "useremail": "",
  "sources": [
    {
      "name": "myAzureFeed",
      "type": "azure",
      "path": "https://yourStorageAccount.blob.core.windows.net/myFeed/",
      "container": "myFeed",
      "connectionString": "DefaultEndpointsProtocol=https;AccountName=;AccountKey=;BlobEndpoint="
    }
  ]
}

The name will be used to specify a source from the command line later, this should be a simple name that you can remember.

Set the path to the URL for your own storage account and include the storage container in the path.

Specify the container name itself, the same as the one in the above path URL in the container property.

The connectionString should be the full Azure storage connection string, this will be used to connect to Azure. Write access will be needed to upload the json files.

username and useremail are optional fields that can be filled out to help track feed history.

Initializing a new feed

New feeds need to be initialized, this creates the root template files and uploads them to Azure. You can do this by running the init command.

sleet.exe init --source myAzureFeed

Use the name specified in sleet.json for the source parameter. If everything worked you should see messages about pushing the new json files to your Azure container. You now have a complete NuGet v3 feed with zero packages.

Adding NuGet packages

A feed without packages isn't very useful, so lets add some. This can be done with the push command. The push command can take either the path to a nupkg or a path to a folder of nupkgs. For this example I am going to use a folder containing several nupkgs that I want to push to the new feed.

sleet.exe push d:\nupkgsToPush\ --source myAzureFeed

Running this command will sync the v3 feed files to your local disk, modify them, and then upload all files that have changed back to your Azure storage container.

Configuring NuGet

Now that the feed contains packages it can be used the same as any other NuGet feed. NuGet v3 feed URLs must contain the path full path to index.json, so for an Azure storage account it will look like this:

https://yourStorageAccount.blob.core.windows.net/myFeed/index.json

The URL can be added to your NuGet.Config file, either for your project or to the machine wide NuGet config at %appdata/NuGet/NuGet.Config

Example NuGet.Config

<?xml version="1.0" encoding="utf-8"?>  
<configuration>  
  <packageSources>
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
    <add key="myFeed" value="https://yourStorageAccount.blob.core.windows.net/myFeed/index.json" />
  </packageSources>
</configuration>  

Using the NuGet v3 feed

To try out the feed launch Visual Studio, open a solution or project, and bring up the NuGet Package Manager UI. Once you have selected your feed from the source drop down list you should see the packages you pushed under the Browse tab. Note that these feeds are stored in Azure with gzip compression, a feature added to NuGet in 3.4.0. If you have an older version of NuGet you can find the update on dist.nuget.org.

This new feed can also be used with dotnet restore in dotnet CLI.

To find out more about Sleet check out the Sleet github repository.