Tuesday, 21 January 2014

Using Private NuGet Packages in Azure Web Sites

After wayyy too long – I’ve finally started breaking up my code into re-usable components and putting together NuGet feeds for them. In many case – I’m trying to put these together into a state that is suitable for others and releasing them as Open Source on GitHub.

However, there is some code that I don’t want to go out to the public – but I still want to be able to break them up into components and generate NuGet packages for them. Enter the awesome MyGet.

MyGet allows you to create your own private NuGet feeds amongst a whole load of other cool stuff – now the next challenge: how do I get Azure to build my site and restore the package from the private feed?

Enable NuGet Package Restore

This is super-easy, and in many cases you might have already done it .

Right click the Solution and click “Enable NuGet Package Restore”. This will create a “.nuget” directory within your solution folder. In here are a few key files:

  • NuGet.Config
  • NuGet.exe
  • NuGet.targets

Now – we just need to tell NuGet about our private package feed.

Update Your .targets File

The “.targets” file is the one we’re really interested in. Open it up in a decent text editor.

You will see something like the following:

<ItemGroup Condition=" '$(PackageSources)' == '' ">
  <!-- Package sources used to restore packages. By default, registered sources under %APPDATA%\NuGet\NuGet.Config will be used -->
  <!-- The official NuGet package source (https://www.nuget.org/api/v2/) will be excluded if package sources are specified and it does not appear in the list -->
  <!--
    <PackageSource Include="https://www.nuget.org/api/v2/" />
    <PackageSource Include="https://my-nuget-source/nuget/" />
  -->
</ItemGroup>

Go ahead and un-comment the “PackageSource elements and replace the “Include” for the second element with your private NuGet packages feed URL from MyGet.

This will be something like “https://www.myget.org/F/your-feed-name/”.

Push It!

If you’re sane – you’ve set up Azure to deploy via source control. Go ahead and git-add your new files to the repository and push them up to GitHub.

If all goes to plan, your site will be updated using the NuGet package from your private feed.

Happy hacking!

1 comment:

  1. Thanks! This solidified my conclusions after Googling around for the last hour. I was hoping that I could set this in a global setting on our CI box (Jenkins) and not have to set each nuget.config on every project. However, I know for sure how to force it now, thanks!

    ReplyDelete