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:
- Restore the NuGet version
- Build the Solution
- Test the Assemblies and execute the Test projects
- Publish the files and index the source code
- Copy the published files to artifacts staging directory
- 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.
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.
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.
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.