Azure

Build and Publish NuGet Package in Azure

Most of the time we would like to have our common library used by many applications. Integrating the library as a part of all the solutions is not ideal when the requirement is to have the common implementation used in various projects. NuGet helps us to create a separate package that can be used globally which serves the purpose of implementing the common functionality across all major solutions. Thinking on those lines, today I would like to share the idea of how we can achieve of building and publishing a library as a NuGet package using VSTS in Azure.

In one of my post I have already discussed about how we can create a NuGet repository in Azure. Lets have a look into a step-by-step process of how we can create a NuGet feed in Azure and publish the NuGet package for the library so that it is available in the feed.

Step 1: Create a NuGet Feed

VSO can be used to host a private NuGet feed and in order to do that first you need to install Package Management to VSO. It provides an extension that enables continuous delivery mechanism by hosting packages which will be available for consumption and usage by team, builds and releases. Once installed you can see a Package link against each Team project in your Team Services dashboard. If you already have the Package link, then don’t bother to install it again.

Once you have installed the Package Manager in VSO, you can then create a Feed using New feed which includes a Feed name and description for the feed.

Once done your feed is ready to upload packages.  You can also add the required permissions for those who you would like to authorize to upload packages into the feed.

Step 2: Create NuGet Packages of your Library

Once you are good with the feed, it is time for you to generate NuGet package for your library and publish it in your private hosted feed. In order to do that, add a NuGet project in your solution which contains the Library project.

Typically the structure would look like this which will also include the nuspec file for the library that you would like to create the package for.

Open the nuspec file and update the id, version, title, etc. along with project Uri of your NuGet repository. That Uri is not mandatory but it is better to keep it. Add the release path of the DLL and PDB file of the class library project whose package we want to create.

You can create more than one nuspec file which can be specific and related to multiple class library projects in your solution that can generate multiple NuGet packages to be hosted in NuGet repository. However it depends on your requirement.

Open the NuGet.config file and add the api key and package source for the NuGet package explorer. Again its not mandatory to do that as we are trying to automate the NuGet package development using VSO team service but for future reference purpose you can keep it.

That’s all you need to do. One more thing that you might have to do is open the NuGet project file in notepad or notepad++ and verify the settings of AfterBuild are same as below.

Step 3: Prepare your Build definition in Visual Studio Team Services

Select the team project in VSO which contains the library project and solution. Select the Build section and add a new Build definition for your library.

Select the build definition template Visual Studio.

While creating the Build definition, you would be prompted to select Continuous Integration (CI) build which means whenever you check in the code to source control, immediately this build will be triggered. It is a best option to do that when you have a requirement to create and deploy the NuGet package as soon as you check in your code to source control.

Once the Build definition is created for the team project, you will see the sequential order of how the build will happen through VSO. The steps involved are:

  1. Restore the NuGet version
  2. Build the Solution
  3. Test the Assemblies and execute the Test projects
  4. Publish the files and index the source code
  5. Copy the published files to artifacts staging directory
  6. Publish the artifacts and drop it to the server.

What we need to include is another step to to publish the NuGet package to the private hosted NuGet feed as soon as the solution build is complete which means in between Step 2 and 3.

In order to do that we need to add a build step of publishing the NuGet package to the feed. Select Add Tasks and add NuGet Publisher from all the listed tasks available.

Once added, drag the NuGet Publisher just after the Build Solution step so that once the project is successfully build, it will publish the generated NuGet.

Once done, lets review the following definitions Build, Repository, Variables and Triggers.

Build Definition Tab

Select Feed Type as Internal Feed and add the Internal Feed Url that we have created in our Step 1 in our NuGet publisher build step.

Repository Tab

Verify the mapping information of the server path to the source location of the project of Team explorer is correct. This can be validated in Repository tab of Build definition.

Variables Tab

Add a variable which will represent the API key to access the NuGet feed and authorize the user to upload the package. This is important as we don’t want any unauthorized user to upload anything to the private feed.

Trigger Build

Verify that CI build include the repository path of the solution from where the NuGet packages are going to get created.

That’s all and we are done.
Its is time to validate and trigger a build to check if everything is working as per expectations.

Click on Queue build…. and verify how things are going.

Hope that this will help my fellow developers to Create and publish NuGet packages in Azure.

Programming, Visual Studio

Publishing NuGet Package using NuGet Packager Extension

In one of my last post http://idevexplorer.com/2015/06/create-and-publish-nuget-package-in-windows-azure/ I have explained how we can create a NuGet Package on every build and deploy it to NuGet server in Azure.

In this post I would like to emphasize on how we can use the NuGet Package Extension in Visual Studio and publish NuGet packages to the server.

First of all we need to install the extension from the following url https://visualstudiogallery.msdn.microsoft.com/daf5c6db-386b-4994-bdd7-b6cd52f11b72. Once you have installed the extension for Visual Studio you can get the project template to create NuGet Package as shown below.

image

Once you add the NuGet Packager project to your solution, you can find the project hierarchy as shown below.

image

Now all you need to do is configure the NuGet project in order to take the libraries that you would like to publish. Open the NuGet.config file and provide the server url where the package is going to get published. Add the ApiKey which will validate the publishing of the library.

image

Copy your libraries that you would like to publish in the lib folder of the NuGet project.

image

Rename package.nuspec file to a name that you would like to distinguish for and update the project url in the nuspec file pointing to the publishing server

image

Change the description and other parameters as you want. You can even provide the dependencies to get installed or validate during the installation of the package

Now all we need to do is build the Package project. You might encounter some issues during building the package if you have renamed the nuspec file. Open NuGetPackage.ps1 file and change the following statements to point to correct NuSpec name

image

Build it again. Now you will not see any error. When you are building the NuGet project in Debug mode it will create the package *.nupkg file locally. In order to deploy it to the server through build process, you need to build it in Release mode.

Now if you go to the NuGet feed url of your server you can view the package over there.

image

You can even view through NuGet package manager the package installed.

image

If you want to delete the package from the server then you need to use the following command in Command Prompt.

nuget delete <Package Name> <Version> <API Key> –Source <NuGet Server Url>

image

That’s it you are good to go.

MVC, Programming, Visual Studio

Create and Publish NuGet Package in Windows Azure

Recently I came up with a situation where I need to re-use few libraries in many projects. One way to do this is to refer the latest library in every projects of your existing or any other solutions which I am not comfortable with at all. The other way to avoid this traditional approach of directly adding references of your library to the project is to use NuGet Packager which I like the most. Well we might all know that having the library located in a common repository of NuGet Server makes more sense in order to re-use it multiple places. Also you can keep multiple versions of the library and use it as per client requirement. If client requires the latest package or a specific old versioned package, its relatively a lot easier to refer the library through NuGet Package Manager or Console.

Well keeping all these in mind, my situation was that I need to deploy this library in a NuGet Server that can be accessible by resources globally. I also know I do not have a global server that can be used. Hence the only option I have is to deploy the package in Windows Azure as I have a subscription on it and see how it works.

Create an Empty Web Application

First I started creating an empty Web Project from VS2013. While creating the website, it will also provide me an option to select the Web Host where the site would be hosted. I had selected that option so that my Web Host repository gets created.

Once an empty Website is created use the package manager console to add NuGet.Server library to the web project in order to provide the facility to create your web server.

PM> Install-Package NuGet.Server

This will add few libraries and folders in your empty web project as shown below.

image1

 

Open the web.config file and validate that you have requireApiKey value set to True and have added an apiKey so that no unauthorized user can have permission to create the package and publish it to repository.

image_3

 

Run the solution pressing F5 and you would see the following web page in your explorer.

image_4

 

In order to push any package to the server you need to run the following command from Command Prompt.

nuget push {package file} –s http://localhost:61342/ {apikey}

Well our agenda is to push the package to Azure web app instead of localhost. Hence our next step is to configure our Web Publishing activity to create the NuGet Web Server in Azure.

 

Update the settings for publishing to your Windows Azure repository

While creating the web project we have selected the Web Host location where the files need to be deployed. In this step you need to update the parameters for publishing and creating the NuGet Web Server in Azure. Select the settings option of Web Publish Activity to update the parameters.

image_5

 

 

Remember to select Microsoft Azure Websites from Publish Target section.

image_6

 

Once you have selected the Target location, you need to validate the connection to the Target location for the Web Publishing method.

image_7

 

Next step is to publish it. Make sure that you are publishing the website in Release Mode and also no databases are required.

image_8

 

Once the web site is published in your Azure account, you can review the website using the Url created after the publishing activity is complete.

 

image_9

 

You can even verify from your Azure account that the web server has been created through http://manage.windowsazure.com

image_10

 

Well we are all set to go for publishing our packages to the web server.

 

Publish your package to the web server

Select the Project you want to publish as a NuGet package to your NuGet Server.

Use the package manager console to add CreateNewNuGetPackageFromProjectAfterEachBuild library to the project in order to create the NuGet package *.nupkg file on every build.

PM> Install-Package CreateNewNuGetPackageFromProjectAfterEachBuild

The following files will get generated in your project.

image_11

 

Now right click on the Project and Build it. This will create your NuGet Package.

image_12

 

Once your package is generated, copy the package and place it in a folder where NuGet.exe resides. NuGet.exe will help to publish the package to the new NuGet Server that has been created in your Azure account.

You can download NuGet.exe from the following location http://nuget.codeplex.com/releases/view/121838

Now open your Command Prompt and run the following command nuget push {package file} –s http://{Azure NuGet Web Server address}/ {apikey}

image_13

 

Your package is now deployed in NuGet Web Server.

 

Apply your package to a project pointing to NuGet Web Server hosted in Azure

Next step is to consume your NuGet Package from your newly created NuGet Server location.

Add you package source in NuGet Package Manager interface.

image_14

 

Now you can install the package using Manage NuGet Packages option while right-clicking on the project where you want the package to get installed.

image_15

 

That’s it you are good to go with NuGet package….