Creating NuGet packages with TeamCity

I wanted to create NuGet packages for some internal libraries that I use.  I came across this post that describes how to create a NuGet package with MSBuild, which got me the majority of the way.  I did have to make a few modifications to make it fit in nicely with my TeamCity CI server.

To begin, I installed the MSBuild Community Tasks to my build server.

After that, I grabbed the NuGet.MSBuild assembly from their CI server and copied it to the C:Program Files (x86)MSBuildNuGet folder.

Following Mark’s instructions, I created a NuGet folder for my project.  In that folder, I’m able to put my nuspec file and any transforms.  With current versions of NuGet, you need to make sure that you have proper namespaces added to the package and metadata elements:

<package xmlns:xsd="" xmlns:xsi="">
	<metadata xmlns="">

I then added the following MSBuild file to my project:

<Project xmlns="">
	<Import Project="$(MSBuildExtensionsPath)MSBuildCommunityTasksMSBuild.Community.Tasks.Targets"/>
	<UsingTask AssemblyFile="$(MSBuildExtensionsPath)NuGetNuGet.MSBuild.dll" TaskName="NuGet.MSBuild.NuGet" />

		<BuildConfiguration Condition="'$(teamcity_version)' == ''">Debug</BuildConfiguration>
		<BuildConfiguration Condition="'$(teamcity_version)' != ''">Release</BuildConfiguration>
		<build_number Condition="'$(teamcity_version)' == ''"></build_number>

	<Target Name="CreatePackage">
			<PackageBuildPath Condition="'$(teamcity_version)' == ''">buildNuGet</PackageBuildPath>
			<PackageBuildPath Condition="'$(teamcity_version)' != ''">$(teamcity_build_tempDir)buildNuGet</PackageBuildPath>

			<PackageSourceFiles Include="$(PackageSourcePath)**" />
			<PackageLibFiles Include="bin$(BuildConfiguration)$(AssemblyName).dll;bin$(BuildConfiguration)$(AssemblyName).pdb" />
			<OldDestinationFiles Include="$(PackagePath)$(AssemblyName).1.*.nupkg" Exclude="$(PackagePath)$(AssemblyName).$(build_number).nupkg" />

		<RemoveDir Directories="$(PackageBuildPath)" ContinueOnError="true" />
		<Message Text="Setting up the $(PackageBuildPath) directory will all the necessary files to create our package"/>
		<Copy SourceFiles="@(PackageSourceFiles)"  DestinationFiles="@(PackageSourceFiles->'$(PackageBuildPath)%(RecursiveDir)%(Filename)%(Extension)')" />
		<Copy SourceFiles="@(PackageLibFiles)"  DestinationFiles="@(PackageLibFiles->'$(PackageBuildPath)lib%(RecursiveDir)%(Filename)%(Extension)')" />

		<FileUpdate Files="$(PackageBuildPath)package.nuspec" Regex="version&gt;([^&quot;&lt;]*)&lt;/version" ReplacementText="version&gt;$(build_number)&lt;/version" />

		<Message Text="Creating the package"/>

		<NuGet PackageDir="$(PackagePath)" SpecFile="$(PackageBuildPath)package.nuspec" />
		<Message Text="Deleting previous package"/>
		<Delete Files="@(OldDestinationFiles->'$(PackagePath)%(Filename)%(Extension)')" ContinueOnError="true" />

There are some conditions added to some of the properties so that I can run the build file locally (for testing) and so that it works as I want when TeamCity runs it.  Additionally, I am only copying the project assemblies from the bin folder, rather than all files, since I handle any dependencies via the nuspec file.

Next, I upgraded my Team City installation to version 6 so that I could take advantage of the multiple build runner feature.  I use the Visual Studio build runner to build my solution and just created a MSBuild runner that will create the NuGet package:

This pretty much sums up the changes I needed to make to have TeamCity produce a NuGet package every time it builds my library.  I’m fairly happy with this solution and so far it seems to be working well.  I want to thank Mark for his post on building NuGet files via MSBuild.  I hope this post helps those of you who want to incorporate this process with TeamCity.

3 thoughts on “Creating NuGet packages with TeamCity

  1. Did you ever get “.cs file transformations” working in NuGet? I noticed your comment on Haack’s blog, which he didn’t respond to 🙂

    I’d really like to have a NuGet package add some code to app_start, just like you mentioned… 🙂

    Or, more specifically, add some javascript to a site.master 🙂

    1. Unfortunately, I did not get my package to add code to the Application_Start method. I doubt that feature will be available in the 1.x release. Hopefully they can add it in the future, though.

Leave a Reply

Your email address will not be published. Required fields are marked *